-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path8002. Horrible Queries.cpp
81 lines (76 loc) · 1.88 KB
/
8002. Horrible Queries.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
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long int Type;
#define MAX_ELEMENTS 100000
#define step(x) ((x)& (-x))
// Range update Range Query BIT
struct Bit{
int N;
Type ft[MAX_ELEMENTS+1];
//sf[x] contains scaling factor to be subtracted from ft[x]*x to get cumulative sum at index x
Type sf[MAX_ELEMENTS+1];
Bit(int n): N(n){
memset(ft, 0, sizeof(Type)*(N+1));
memset(sf, 0, sizeof(Type)*(N+1));
}
void init(int n){
N = n;
memset(ft, 0, sizeof(Type)*(N+1));
memset(sf, 0, sizeof(Type)*(N+1));
}
void add(Type* tree, int idx, Type v){
while(idx <= N){
tree[idx] += v;
idx += step(idx);
}
}
//Returns value at index idx
Type query(Type* tree, int idx){
Type s = 0;
while(idx > 0){
s += tree[idx];
idx -= step(idx);
}
return s;
}
//Returns cumulative sum in rnage [1...idx]
Type cms(int idx){
return query(ft, idx)*idx - query(sf,idx);
}
Type query(int a, int b){
return cms(b) - cms(a-1);
}
void update(int a, int b, Type v){
add(ft, a, v);
add(ft, b+1, -v);
add(sf, a, v*(a-1));
add(sf, b+1, -v*b);
}
};
int main(){
int op;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int from, to, w,q, n, test;
Bit bit = Bit(MAX_ELEMENTS);
scanf("%d", &test);
while( test-- ){
scanf("%d %d", &n, &q);
bit.init(n);
while(q--){
scanf("%d", &op);
if(op){
scanf("%d %d", &from, &to);
printf("%lld\n", bit.query(from, to));
}
else{
scanf("%d %d %d", &from, &to, &w);
bit.update(from, to,w);
}
}
}
return 0;
}