#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
struct T {
ll sum = 0;
ll max_pre = 0;
ll min_pre = 0;
};
constexpr T unit;
T from_int(ll t) {
T res;
res.sum = t;
res.max_pre = max<ll>(t,0);
res.min_pre = min<ll>(t,0);
return res;
};
T f(T a, T b) {
T res;
res.sum = a.sum + b.sum;
res.max_pre = max(a.max_pre, a.sum + b.max_pre);
res.min_pre = min(a.min_pre, a.sum + b.min_pre);
return res;
}
struct Tree {
vector<T> s; int n;
Tree(int n_ = 0, T def = unit) {
n = 1;
while(n < n_) n*=2;
s.assign(2*n, def);
}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
int walk(ll cap) {
int x = 1;
T right = unit;
while(x < n) {
T go_right = f(s[2*x+1], right);
if(go_right.max_pre - go_right.min_pre < cap) {
right = go_right;
x = 2*x;
}
else x = 2*x+1;
}
return x-n;
}
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = sz(c);
int q = sz(l);
vi s(n);
vector<vi> at_l(n+1), at_r(n+1);
rep(i,0,q) {
at_l[l[i]].emplace_back(i);
at_r[r[i]+1].emplace_back(i);
}
int Q = q+10;
Tree segtree(Q);
segtree.update(0,from_int(1e18));
segtree.update(1,from_int(-1e18));
rep(i,0,n) {
for(int qi : at_l[i]) segtree.update(qi+10, from_int(v[qi])); // activate
for(int qi : at_r[i]) segtree.update(qi+10, unit); // deactivate
segtree.update(2, from_int(0));
ll cap = c[i];
int t = segtree.walk(cap);
T v = segtree.query(t,Q);
T vv = segtree.query(t+1,Q);
ll ans = 0;
if(vv.sum > v.sum) {
ans = v.sum - v.min_pre;
}
else if(vv.sum < v.sum) {
ans = v.sum - v.max_pre + cap;
}
else assert(false);
s[i] = ans;
}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
503 ms |
36316 KB |
Output is correct |
2 |
Correct |
521 ms |
38400 KB |
Output is correct |
3 |
Correct |
564 ms |
38352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
190 ms |
20292 KB |
Output is correct |
3 |
Correct |
112 ms |
12216 KB |
Output is correct |
4 |
Correct |
467 ms |
36228 KB |
Output is correct |
5 |
Correct |
491 ms |
38344 KB |
Output is correct |
6 |
Correct |
466 ms |
38472 KB |
Output is correct |
7 |
Correct |
463 ms |
38424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
91 ms |
19148 KB |
Output is correct |
4 |
Correct |
101 ms |
12200 KB |
Output is correct |
5 |
Correct |
218 ms |
30880 KB |
Output is correct |
6 |
Correct |
222 ms |
32020 KB |
Output is correct |
7 |
Correct |
215 ms |
32108 KB |
Output is correct |
8 |
Correct |
229 ms |
32100 KB |
Output is correct |
9 |
Correct |
224 ms |
31988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
503 ms |
36316 KB |
Output is correct |
7 |
Correct |
521 ms |
38400 KB |
Output is correct |
8 |
Correct |
564 ms |
38352 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
190 ms |
20292 KB |
Output is correct |
11 |
Correct |
112 ms |
12216 KB |
Output is correct |
12 |
Correct |
467 ms |
36228 KB |
Output is correct |
13 |
Correct |
491 ms |
38344 KB |
Output is correct |
14 |
Correct |
466 ms |
38472 KB |
Output is correct |
15 |
Correct |
463 ms |
38424 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
91 ms |
19148 KB |
Output is correct |
19 |
Correct |
101 ms |
12200 KB |
Output is correct |
20 |
Correct |
218 ms |
30880 KB |
Output is correct |
21 |
Correct |
222 ms |
32020 KB |
Output is correct |
22 |
Correct |
215 ms |
32108 KB |
Output is correct |
23 |
Correct |
229 ms |
32100 KB |
Output is correct |
24 |
Correct |
224 ms |
31988 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
102 ms |
13484 KB |
Output is correct |
27 |
Correct |
183 ms |
22864 KB |
Output is correct |
28 |
Correct |
482 ms |
40912 KB |
Output is correct |
29 |
Correct |
489 ms |
41168 KB |
Output is correct |
30 |
Correct |
454 ms |
41372 KB |
Output is correct |
31 |
Correct |
488 ms |
41504 KB |
Output is correct |
32 |
Correct |
441 ms |
41680 KB |
Output is correct |