/*
https://oj.uz/problem/view/IOI21_candies
Distributing Candies
*/
#include <bits/stdc++.h>
using namespace std;
#define N 200000
#define Q 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N)) */
long long sum[N_ * 2], mn[N_ * 2], mx[N_ * 2]; int n_;
void pul(int i) {
int l = i << 1 | 0, r = i << 1 | 1;
sum[i] = sum[l] + sum[r];
mn[i] = min(mn[r], mn[l] + sum[r]);
mx[i] = max(mx[r], mx[l] + sum[r]);
}
void update(int i, int x) {
i |= n_;
sum[i] = x;
mn[i] = min(x, 0);
mx[i] = max(x, 0);
while (i > 1)
pul(i >>= 1);
}
int query(int c) {
long long s = 0, mn_ = 0, mx_ = 0;
int i = 1;
if (mx[1] - mn[1] <= c)
return mx[1];
while (i < n_)
if (max(mx_, mx[i << 1 | 1] + s) - min(mn_, mn[i << 1 | 1] + s) <= c) {
i = i << 1 | 0;
mn_ = min(mn_, mn[i ^ 1] + s);
mx_ = max(mx_, mx[i ^ 1] + s);
s += sum[i ^ 1];
} else
i = i << 1 | 1;
return min(max(sum[i] + s, mx_), c + mn_);
}
using vi = vector<int>;
vi distribute_candies(vi aa, vi ll, vi rr, vi vv) {
vector<array<int, 3>> ee;
int n, q, e;
vi ans;
n = aa.size(), q = ll.size();
for (int h = 0; h < q; h++) {
int l = ll[h], r = rr[h], v = vv[h];
ee.push_back({l, h, v});
ee.push_back({r + 1, h, 0});
}
n_ = 1;
while (n_ < q)
n_ <<= 1;
e = 0, sort(ee.begin(), ee.end());
for (int i = 0; i < n; i++) {
while (e < q * 2 && ee[e][0] <= i) {
update(ee[e][1], ee[e][2]);
e++;
}
ans.push_back(query(aa[i]));
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
27432 KB |
Output is correct |
2 |
Correct |
280 ms |
26524 KB |
Output is correct |
3 |
Correct |
276 ms |
26436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
218 ms |
22336 KB |
Output is correct |
3 |
Correct |
53 ms |
6344 KB |
Output is correct |
4 |
Correct |
303 ms |
28544 KB |
Output is correct |
5 |
Correct |
262 ms |
28820 KB |
Output is correct |
6 |
Correct |
301 ms |
29252 KB |
Output is correct |
7 |
Correct |
263 ms |
28592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
128 ms |
22076 KB |
Output is correct |
4 |
Correct |
49 ms |
4416 KB |
Output is correct |
5 |
Correct |
178 ms |
26036 KB |
Output is correct |
6 |
Correct |
183 ms |
26764 KB |
Output is correct |
7 |
Correct |
173 ms |
27312 KB |
Output is correct |
8 |
Correct |
212 ms |
25868 KB |
Output is correct |
9 |
Correct |
172 ms |
27404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
314 ms |
27432 KB |
Output is correct |
7 |
Correct |
280 ms |
26524 KB |
Output is correct |
8 |
Correct |
276 ms |
26436 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
218 ms |
22336 KB |
Output is correct |
11 |
Correct |
53 ms |
6344 KB |
Output is correct |
12 |
Correct |
303 ms |
28544 KB |
Output is correct |
13 |
Correct |
262 ms |
28820 KB |
Output is correct |
14 |
Correct |
301 ms |
29252 KB |
Output is correct |
15 |
Correct |
263 ms |
28592 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
128 ms |
22076 KB |
Output is correct |
19 |
Correct |
49 ms |
4416 KB |
Output is correct |
20 |
Correct |
178 ms |
26036 KB |
Output is correct |
21 |
Correct |
183 ms |
26764 KB |
Output is correct |
22 |
Correct |
173 ms |
27312 KB |
Output is correct |
23 |
Correct |
212 ms |
25868 KB |
Output is correct |
24 |
Correct |
172 ms |
27404 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
53 ms |
4456 KB |
Output is correct |
27 |
Correct |
219 ms |
21936 KB |
Output is correct |
28 |
Correct |
276 ms |
27112 KB |
Output is correct |
29 |
Correct |
276 ms |
27400 KB |
Output is correct |
30 |
Correct |
303 ms |
27720 KB |
Output is correct |
31 |
Correct |
262 ms |
27856 KB |
Output is correct |
32 |
Correct |
299 ms |
28080 KB |
Output is correct |