-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathHORRIBLE - Horrible Queries.cpp
74 lines (59 loc) · 1.21 KB
/
HORRIBLE - 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
#include <stdio.h>
#include <algorithm>
#include <memory.h>
using namespace std;
int const N = 1e5 + 1;
int n, q;
long long seg[4 * N], lazy[4 * N];
int s, e, v;
void pro(int idx, int l, int r) {
seg[idx] += lazy[idx] * (r - l + 1);
if(l != r)
lazy[idx << 1] += lazy[idx],
lazy[(idx << 1) + 1] += lazy[idx];
lazy[idx] = 0;
}
void update(int idx, int l, int r) {
if(lazy[idx] != 0)
pro(idx, l, r);
if(s > r || e < l)
return;
if(l >= s && r <= e) {
lazy[idx] += v;
pro(idx, l, r);
return;
}
int m = (l + r) >> 1;
update(idx << 1, l, m);
update((idx << 1) + 1, m + 1, r);
seg[idx] = seg[idx << 1] + seg[(idx << 1) + 1];
}
long long get(int idx, int l, int r) {
if(lazy[idx] != 0)
pro(idx, l, r);
if(s > r || e < l)
return 0;
if(l >= s && r <= e)
return seg[idx];
int m = (l + r) >> 1;
return get(idx << 1, l, m) + get((idx << 1) + 1, m + 1, r);
}
int main() {
int t, type;
scanf("%d", &t);
while(t-- != 0) {
memset(seg, 0, sizeof seg);
memset(lazy, 0, sizeof lazy);
scanf("%d %d", &n, &q);
while(q-- != 0) {
scanf("%d %d %d", &type, &s, &e);
if(type == 0) {
scanf("%d", &v);
update(1, 1, n);
} else {
printf("%lld\n", get(1, 1, n));
}
}
}
return 0;
}