-
Notifications
You must be signed in to change notification settings - Fork 4
/
BIT_Range.cpp
71 lines (65 loc) · 1.53 KB
/
BIT_Range.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* LightOJ 1164: Horrible Queries
* given an array of length n initially all zero and q queries.
* 0 x y v: add v to all elements in the interval [x, y]
* 1 x y: print sum of all elements in the interval [x, y]
* array is 0 indexed
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 25;
int n;
ll bit1[ N ], bit2[ N ];
void reset() {
memset(bit1, 0, sizeof bit1);
memset(bit2, 0, sizeof bit2);
}
void update(ll bit[], int i, ll val) {
while( i <= n ) {
bit[ i ] += val;
i += (i & (-i));
}
}
void update(int l, int r, ll val) {
update(bit1, l, val);
update(bit1, r + 1, -val);
update(bit2, l, val * (l - 1));
update(bit2, r + 1, -val * r);
}
ll query(ll bit[], int i) {
ll ret = 0;
while( i > 0 ) {
ret += bit[ i ];
i -= (i & (-i));
}
return ret;
}
ll query(int l, int r) {
l -= 1;
ll p = query(bit1, l) * l - query(bit2, l);
ll q = query(bit1, r) * r - query(bit2, r);
return q - p;
}
int main() {
int testcase, caseno = 1;
scanf("%d", &testcase);
while( testcase-- ) {
int q;
cin >> n >> q;
reset();
printf("Case %d:\n", caseno++);
while( q-- ) {
int op, x, y;
scanf("%d %d %d", &op, &x, &y);
if( op == 0 ) {
ll v;
scanf("%lld", &v);
update(x + 1, y + 1, v);
} else {
printf("%lld\n", query(x + 1, y + 1));
}
}
}
return 0;
}