forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1164-horrible_queries.cpp
113 lines (96 loc) · 2.76 KB
/
1164-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
struct sam {
ll sum;
ll todown;
};
sam nodes[1000100];
void update(int id, int start, int end, int qstart, int qend, int val)
{
if (qstart > end || qend < start || start > end ) {
nodes[id].sum += (end - start + 1) * (nodes[id].todown );
nodes[id * 2 + 1].todown += nodes[id].todown;
nodes[id * 2 + 2].todown += nodes[id].todown;
nodes[id].todown = 0;
return;
}
if (start >= qstart and end <= qend) {
nodes[id].sum += (end - start + 1) * (nodes[id].todown + val);
nodes[id * 2 + 1].todown += nodes[id].todown + val;
nodes[id * 2 + 2].todown += nodes[id].todown + val;
nodes[id].todown = 0;
return;
}
int mid = start + end;
mid /= 2;
nodes[id].sum += (end - start + 1) * nodes[id].todown;
nodes[id * 2 + 1].todown += nodes[id].todown;
nodes[id * 2 + 2].todown += nodes[id].todown;
nodes[id].todown = 0;
update(id * 2 + 1, start, mid, qstart, qend, val);
update(id * 2 + 2, mid + 1, end, qstart, qend, val);
nodes[id].sum = nodes[id * 2 + 1].sum + nodes[id * 2 + 2].sum;
}
ll query(int id, int start, int end, int qstart, int qend)
{
if (qstart > end || qend < start || start > end) {
nodes[id].sum += (end - start + 1) * (nodes[id].todown);
nodes[id * 2 + 1].todown += nodes[id].todown;
nodes[id * 2 + 2].todown += nodes[id].todown;
nodes[id].todown = 0;
return 0 ;
}
if (start >= qstart and end <= qend) {
nodes[id].sum += (end - start + 1) * (nodes[id].todown);
nodes[id * 2 + 1].todown += nodes[id].todown;
nodes[id * 2 + 2].todown += nodes[id].todown;
nodes[id].todown = 0;
return nodes[id].sum;
}
int mid = start + end;
mid /= 2;
//nodes[id].sum += (end -start+1)* nodes[id].todown;
nodes[id * 2 + 1].todown += nodes[id].todown;
nodes[id * 2 + 2].todown += nodes[id].todown;
nodes[id].todown = 0;
ll hq1 = query(id * 2 + 1, start, mid, qstart, qend);
ll hq2 = query(id * 2 + 2, mid + 1, end, qstart, qend);
nodes[id].sum = nodes[id * 2 + 1].sum + nodes[id * 2 + 2].sum;
return hq1 + hq2 ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/sameer/sam.sam", "r", stdin);
#endif
// ios_base::sync_with_stdio(false);
ll i, j, k, n, m, t, cont, a, b, c;
//cin >> t;
scanf("%lld", &t);
ll cases = t;
//t = 1;
while (t--) {
memset(nodes, 0, sizeof nodes);
//cin >> n >> m;
scanf("%lld %lld", &n, &m);
// cout << "Case " << cases - t << ": " << endl;
printf("Case %lld:\n", cases - t );
for (i = 0; i < m; i++) {
// cin >> k ;
scanf("%lld", &k);
if (k == 0) {
// cin >> a >> b >> c;
scanf("%lld %lld %lld", &a, &b, &c);
update(0, 0, n - 1, a, b, c);
} else {
// cin >> a >> b;
scanf("%lld %lld", &a, &b);
ll sae = query(0, 0, n - 1, a, b) ;
printf("%lld\n", sae);
}
}
}
return 0;
}