#include <bits/stdc++.h>
using namespace std;
const int n_bits = 19;
const long long inf = 1e18;
long long minseg[1 << (n_bits + 1)];
long long maxseg[1 << (n_bits + 1)];
long long lazyadd[1 << (n_bits + 1)];
//we will use lazy propogation segment tree
//must support min and max, so basically 2 put together
struct segtree {
long long last_value = 0;
long long small = inf;
long long big = -inf;
segtree() {}
void update(int node, int change) {
last_value += change;
node += (1 << n_bits);
lazyadd[node] += change;
while (node > 1) {
if (node % 2 == 0) {
lazyadd[node + 1] += change;
}
minseg[node / 2] = min(minseg[node] + lazyadd[node], minseg[node ^ 1] + lazyadd[node ^ 1]);
maxseg[node / 2] = max(maxseg[node] + lazyadd[node], maxseg[node ^ 1] + lazyadd[node ^ 1]);
node /= 2;
}
}
//returns largest index i such that range >= capacity
int solve(int capacity) {
int node = 1;
small = inf;
big = -inf;
long long lz = 0;
while (node < (1 << n_bits)) {
lz += lazyadd[node];
node *= 2;
if (max(big, maxseg[node + 1] + lazyadd[node + 1] + lz) - min(small, minseg[node + 1] + lazyadd[node + 1] + lz) > capacity) {
node++;
}
else {
big = max(big, maxseg[node + 1] + lazyadd[node + 1] + lz);
small = min(small, minseg[node + 1] + lazyadd[node + 1] + lz);
}
}
if (minseg[node] + lazyadd[node] + lz < last_value) {
return capacity - (big - last_value);
}
else {
return last_value - small;
}
}
};
vector<pair<int, int>> toggle[(int)6e5];
//this tells you what you need to toggle on/off when moving across the boxes
//stores a pair indicating the query id and the change in the number of candies
vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) {
int n = C.size();
int q = L.size();
segtree s;
for (int i = 0; i < q; i++) {
toggle[L[i]].push_back({i, V[i]});
toggle[R[i] + 1].push_back({ i, -V[i] });
}
vector<int> ans;
ans.resize(n);
for (int i = 0; i < n; i++) {
for (auto p : toggle[i]) {
s.update(p.first + 2, p.second);
}
if (maxseg[1] - minseg[1] < C[i]) {
ans[i] = s.last_value - (minseg[1] + lazyadd[1]);
}
else {
ans[i] = s.solve(C[i]);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
7 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
25180 KB |
Output is correct |
4 |
Correct |
7 ms |
25192 KB |
Output is correct |
5 |
Correct |
7 ms |
27736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
51648 KB |
Output is correct |
2 |
Correct |
211 ms |
50512 KB |
Output is correct |
3 |
Correct |
232 ms |
47032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21136 KB |
Output is correct |
2 |
Correct |
99 ms |
42236 KB |
Output is correct |
3 |
Correct |
59 ms |
27000 KB |
Output is correct |
4 |
Correct |
197 ms |
48820 KB |
Output is correct |
5 |
Correct |
191 ms |
52492 KB |
Output is correct |
6 |
Correct |
198 ms |
53244 KB |
Output is correct |
7 |
Correct |
200 ms |
53332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
6 ms |
25180 KB |
Output is correct |
3 |
Correct |
69 ms |
39532 KB |
Output is correct |
4 |
Correct |
49 ms |
28992 KB |
Output is correct |
5 |
Correct |
122 ms |
42796 KB |
Output is correct |
6 |
Correct |
113 ms |
43296 KB |
Output is correct |
7 |
Correct |
115 ms |
43808 KB |
Output is correct |
8 |
Correct |
110 ms |
42420 KB |
Output is correct |
9 |
Correct |
151 ms |
43900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
7 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
25180 KB |
Output is correct |
4 |
Correct |
7 ms |
25192 KB |
Output is correct |
5 |
Correct |
7 ms |
27736 KB |
Output is correct |
6 |
Correct |
224 ms |
51648 KB |
Output is correct |
7 |
Correct |
211 ms |
50512 KB |
Output is correct |
8 |
Correct |
232 ms |
47032 KB |
Output is correct |
9 |
Correct |
5 ms |
21136 KB |
Output is correct |
10 |
Correct |
99 ms |
42236 KB |
Output is correct |
11 |
Correct |
59 ms |
27000 KB |
Output is correct |
12 |
Correct |
197 ms |
48820 KB |
Output is correct |
13 |
Correct |
191 ms |
52492 KB |
Output is correct |
14 |
Correct |
198 ms |
53244 KB |
Output is correct |
15 |
Correct |
200 ms |
53332 KB |
Output is correct |
16 |
Correct |
5 ms |
25180 KB |
Output is correct |
17 |
Correct |
6 ms |
25180 KB |
Output is correct |
18 |
Correct |
69 ms |
39532 KB |
Output is correct |
19 |
Correct |
49 ms |
28992 KB |
Output is correct |
20 |
Correct |
122 ms |
42796 KB |
Output is correct |
21 |
Correct |
113 ms |
43296 KB |
Output is correct |
22 |
Correct |
115 ms |
43808 KB |
Output is correct |
23 |
Correct |
110 ms |
42420 KB |
Output is correct |
24 |
Correct |
151 ms |
43900 KB |
Output is correct |
25 |
Correct |
5 ms |
25180 KB |
Output is correct |
26 |
Correct |
49 ms |
26828 KB |
Output is correct |
27 |
Correct |
93 ms |
40788 KB |
Output is correct |
28 |
Correct |
208 ms |
51104 KB |
Output is correct |
29 |
Correct |
192 ms |
51380 KB |
Output is correct |
30 |
Correct |
220 ms |
50312 KB |
Output is correct |
31 |
Correct |
190 ms |
52872 KB |
Output is correct |
32 |
Correct |
185 ms |
48648 KB |
Output is correct |