-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1164.cpp
More file actions
113 lines (104 loc) · 2.62 KB
/
Copy path1164.cpp
File metadata and controls
113 lines (104 loc) · 2.62 KB
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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 1e9
#define eps 1e-9
#define NN 1050
struct data {
long long sum;
long long xtra;
} tree[300010];
void update(int node, int low, int high, int rlow, int rhigh, int value) {
if (low >= rlow && high <= rhigh) {
tree[node].sum += (high - low + 1) * value;
tree[node].xtra += value;
return;
}
int left = node * 2;
int right = left + 1;
int mid = (low + high) / 2;
if (rhigh <= mid)
update(left, low, mid, rlow, rhigh, value);
else if (rlow > mid)
update(right, mid + 1, high, rlow, rhigh, value);
else {
update(left, low, mid, rlow, rhigh, value);
update(right, mid + 1, high, rlow, rhigh, value);
}
tree[node].sum = tree[left].sum + tree[right].sum + tree[node].xtra * (high - low + 1);
}
long long query(int node, int low, int high, int rlow, int rhigh, long long carry) {
if (low >= rlow && high <= rhigh) {
return tree[node].sum + carry * (high - low + 1);
}
int left = node * 2;
int right = left + 1;
int mid = (low + high) / 2;
long long p1 = 0, p2 = 0;
if (rhigh <= mid)
p1 = query(left, low, mid, rlow, rhigh, carry + tree[node].xtra);
else if (rlow > mid)
p2 = query(right, mid + 1, high, rlow, rhigh, carry + tree[node].xtra);
else {
p1 = query(left, low, mid, rlow, rhigh, carry + tree[node].xtra);
p2 = query(right, mid + 1, high, rlow, rhigh, carry + tree[node].xtra);
}
return p1 + p2;
}
int main() {
ios_base::sync_with_stdio(false);
int tc, t = 1;
cin >> tc;
while (tc--) {
int n, q;
cin >> n >> q;
printf("Case %d:\n", t++);
mem(tree, 0);
while (q--) {
int i, j, k, l;
cin >> i;
if (i == 0) {
cin >> j >> k >> l;
update(1, 1, n, j + 1, k + 1, l);
} else if (i == 1) {
cin >> j >> k;
long long ans = query(1, 1, n, j + 1, k + 1, 0);
printf("%lld\n", ans);
}
}
}
return 0;
}
/*
Input:
2
10 5
0 0 9 10
1 1 6
0 3 7 2
0 4 5 1
1 5 5
20 3
0 10 12 1
1 11 12
1 19 19
Output:
Case 1:
60
13
Case 2:
2
0
*/