Question - 7
World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr. Horrible, who has a PhD in horribleness (but not in Computer Science). You are given an array of N elements, which are initially all 0. After that you will be given C commands. They are -
0 p q v - you have to add v to all numbers in the range of p to q (inclusive), where p and q are two indexes of the array.
1 p q - output a line containing a single integer which is the sum of all the array elements between p and q (inclusive)
Input
In the first line you'll be given T, number of test cases.
Each test case will start with N (N <= 100 000) and C (C <= 100 000). After that you'll be given C commands in the format as mentioned above. 1 <= p, q <= N and 1 <= v <= 10^7.
Output
Print the answers of the queries.
Input:
1
8 6
0 2 4 26
0 4 8 80
0 4 5 20
1 8 8
0 5 7 14
1 4 8
Output:
80
508
public class Main {
public static void update(long[] tree, long[] lazy, int start, int end, int index, int l, int r, long value) {
if (lazy[index] != 0) {
tree[index] += lazy[index] * (end - start + 1);
if (start != end) {
lazy[2 * index] += lazy[index];
lazy[2 * index + 1] += lazy[index];
}
lazy[index] = 0;
}
// no overlap
if (start > r || end < l)
return;
// complete overlap
if (start >= l && end <= r) {
tree[index] += value * (end - start + 1);
if (start != end) {
lazy[2 * index] += value;
lazy[2 * index + 1] += value;
}
return;
}
// partial overlap
int mid = (start + end) / 2;
update(tree, lazy, start, mid, 2 * index, l, r, value);
update(tree, lazy, mid + 1, end, 2 * index + 1, l, r, value);
tree[index] = tree[2 * index] + tree[2 * index + 1];
}
public static long printQuery(long[] tree, long[] lazy, int start, int end, int index, int l, int r) {
// no overlap
if (start > r || end < l)
return 0;
if (lazy[index] != 0) {
tree[index] += lazy[index] * (end - start + 1);
if (start != end) {
lazy[2 * index] += lazy[index];
lazy[2 * index + 1] += lazy[index];
}
lazy[index] = 0;
}
// complete overlap
if (start >= l && end <= r)
return tree[index];
// partial overlap
int mid = (start + end) / 2;
long ans1 = printQuery(tree, lazy, start, mid, 2 * index, l, r);
long ans2 = printQuery(tree, lazy, mid + 1, end, 2 * index + 1, l, r);
long temp = ans1 + ans2;
return temp;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
long tree[] = new long[4 * n];
long lazy[] = new long[4 * n];
// no need to build seprately as all the values are going to be 0 in the
// starting
int c = s.nextInt();
while (c-- > 0) {
int type = s.nextInt();
if (type == 0) {
int p = s.nextInt() - 1;
int q = s.nextInt() - 1;
long v = s.nextInt();
update(tree, lazy, 0, n - 1, 1, p, q, v);
} else {
int p = s.nextInt() - 1;
int q = s.nextInt() - 1;
System.out.println(printQuery(tree, lazy, 0, n - 1, 1, p, q));
}
}
}
}
}
Last updated