import java.util.Arrays;
class Main {
static final int MAX = 100000;
static int[] tree = new int[4 * MAX];
static int[] lazy = new int[4 * MAX];
// Function to update the segment tree
static void updateRange(int node, int start, int end, int l, int r, int val) {
// If lazy[node] is non-zero, then there are some
// pending updates. So we need to make sure the node is
// updated
if (lazy[node] != 0) {
// Updating the node
tree[node] = (end - start + 1) * lazy[node];
// Passing the update information to its children
if (start != end) {
lazy[node * 2] = lazy[node];
lazy[node * 2 + 1] = lazy[node];
}
// Resetting the lazy value for the current node
lazy[node] = 0;
}
// Out of range
if (start > end || start > r || end < l)
return;
// Current segment is fully in range
if (start >= l && end <= r) {
// Update the node
tree[node] = (end - start + 1) * val;
// Pass the update information to its children
if (start != end) {
lazy[node * 2] = val;
lazy[node * 2 + 1] = val;
}
return;
}
// If not completely in range but overlaps, recur for children,
int mid = (start + end) / 2;
updateRange(node * 2, start, mid, l, r, val);
updateRange(node * 2 + 1, mid + 1, end, l, r, val);
// Use the result of children calls to update this node
tree[node] = tree[node * 2] + tree[node * 2 + 1];
}
// Function to calculate the sum on a given range
static int queryRange(int node, int start, int end, int l, int r) {
// Out of range
if (start > end || start > r || end < l)
return 0;
// If there are pending updates
if (lazy[node] != 0) {
// Updating the node
tree[node] = (end - start + 1) * lazy[node];
// Passing the update information to its children
if (start != end) {
lazy[node * 2] = lazy[node];
lazy[node * 2 + 1] = lazy[node];
}
// Resetting the lazy value for the current node
lazy[node] = 0;
}
// At this point, we are sure that pending lazy updates
// are done for the current node. So we can return the value
if (start >= l && end <= r)
return tree[node];
// If not completely in range but overlaps, recur for children,
int mid = (start + end) / 2;
int p1 = queryRange(node * 2, start, mid, l, r);
int p2 = queryRange(node * 2 + 1, mid + 1, end, l, r);
// Use the result of children calls to update this node
return (p1 + p2);
}
public static void main(String[] args) {
// Hardcoded input
int n = 5, q = 3;
int[][] queries = { { 1, 0, 3, 5 }, { 2, 1, 4 }, { 1, 2, 4, 10 } };
for (int i = 0; i < q; i++) {
int type = queries[i][0];
if (type == 1) {
int l = queries[i][1], r = queries[i][2], v = queries[i][3];
updateRange(1, 0, n - 1, l, r - 1, v);
} else {
int l = queries[i][1], r = queries[i][2];
System.out.println(queryRange(1, 0, n - 1, l, r - 1));
}
}
}
}
// This code is contributed by shivamgupta310570