#include "candies.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N_MAX = 2e5 + 11;
int n, q;
struct node{
int pref_max, pref_min, sum;
node() : pref_max(0), pref_min(0), sum(0){}
node(int v) : pref_max(max(0LL, v)), pref_min(min(0LL, v)), sum(v){};
};
struct update{
int t, pos, val;
};
node operator+ (const node& a, const node& b){
node res;
res.pref_max = max(a.pref_max, a.sum + b.pref_max);
res.pref_min = min(a.pref_min, a.sum + b.pref_min);
res.sum = a.sum + b.sum;
return res;
}
node t[N_MAX << 2];
int a[N_MAX];
void st_update(int qi, int qv, int v = 0, int l = 0, int r = q - 1){
if(l == r) t[v] = node(qv), a[qi] = qv;
else {
int m = (l + r) / 2;
if(qi <= m) st_update(qi, qv, v * 2 + 1, l, m);
else st_update(qi, qv, v * 2 + 2, m + 1, r);
t[v] = t[v * 2 + 1] + t[v * 2 + 2];
}
}
node st_suffix(int qi, int v = 0, int l = 0, int r = q - 1){
if(qi > r) return 0;
if(l == r) return t[v];
else {
int m = (l + r) / 2;
if(qi <= m) return st_suffix(qi, v * 2 + 1, l, m) + t[v * 2 + 2];
else return st_suffix(qi, v * 2 + 2, m + 1, r);
}
}
int st_position(int c, node s = 0, int v = 0, int l = 0, int r = q - 1){
if(l == r) return l;
else {
int m = (l + r) / 2;
node s2 = t[v * 2 + 2] + s;
if(s2.pref_max - s2.pref_min < c) return st_position(c, s2, v * 2 + 1, l, m);
else return st_position(c, s, v * 2 + 2, m + 1, r);
}
}
int f(node v, int x, int c, bool strict = false){
if(v.pref_max - v.pref_min < c){
if(x > c - v.pref_max) x = c - v.pref_max;
if(x < -v.pref_min) x = -v.pref_min;
return x + v.sum;
}else{
x += v.sum;
if(x < 0) x = 0;
if(x > c) x = c;
return x;
}
}
vector<int32_t> distribute_candies(vector<int32_t> c, vector<int32_t> l, vector<int32_t> r, vector<int32_t> v) {
n = c.size(), q = l.size();
vector<update> Q;
for(int i = 0; i < q; i++){
Q.push_back({l[i], i, v[i]});
Q.push_back({r[i] + 1, i, 0});
}
sort(Q.begin(), Q.end(), [](update a, update b){
return a.t < b.t;
});
int qid = 0;
vector<int32_t> s(n);
for(int i = 0; i < n; i++){
while(qid < (int) Q.size() && Q[qid].t <= i) st_update(Q[qid].pos, Q[qid].val), qid++;
int pos = st_position(c[i]);
s[i] = f(node(a[pos++]), s[i], c[i]);
s[i] = f(st_suffix(pos), s[i], c[i], true);
}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19284 KB |
Output is correct |
4 |
Correct |
9 ms |
19284 KB |
Output is correct |
5 |
Correct |
10 ms |
19356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
37828 KB |
Output is correct |
2 |
Correct |
274 ms |
37824 KB |
Output is correct |
3 |
Correct |
281 ms |
37816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
214 ms |
39172 KB |
Output is correct |
3 |
Correct |
79 ms |
24956 KB |
Output is correct |
4 |
Correct |
316 ms |
43692 KB |
Output is correct |
5 |
Correct |
312 ms |
44220 KB |
Output is correct |
6 |
Correct |
291 ms |
44476 KB |
Output is correct |
7 |
Correct |
254 ms |
43832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
104 ms |
36148 KB |
Output is correct |
4 |
Correct |
60 ms |
23012 KB |
Output is correct |
5 |
Correct |
176 ms |
41272 KB |
Output is correct |
6 |
Correct |
170 ms |
41924 KB |
Output is correct |
7 |
Correct |
164 ms |
42552 KB |
Output is correct |
8 |
Correct |
170 ms |
41144 KB |
Output is correct |
9 |
Correct |
191 ms |
42676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19284 KB |
Output is correct |
4 |
Correct |
9 ms |
19284 KB |
Output is correct |
5 |
Correct |
10 ms |
19356 KB |
Output is correct |
6 |
Correct |
265 ms |
37828 KB |
Output is correct |
7 |
Correct |
274 ms |
37824 KB |
Output is correct |
8 |
Correct |
281 ms |
37816 KB |
Output is correct |
9 |
Correct |
8 ms |
19028 KB |
Output is correct |
10 |
Correct |
214 ms |
39172 KB |
Output is correct |
11 |
Correct |
79 ms |
24956 KB |
Output is correct |
12 |
Correct |
316 ms |
43692 KB |
Output is correct |
13 |
Correct |
312 ms |
44220 KB |
Output is correct |
14 |
Correct |
291 ms |
44476 KB |
Output is correct |
15 |
Correct |
254 ms |
43832 KB |
Output is correct |
16 |
Correct |
8 ms |
19028 KB |
Output is correct |
17 |
Correct |
8 ms |
19028 KB |
Output is correct |
18 |
Correct |
104 ms |
36148 KB |
Output is correct |
19 |
Correct |
60 ms |
23012 KB |
Output is correct |
20 |
Correct |
176 ms |
41272 KB |
Output is correct |
21 |
Correct |
170 ms |
41924 KB |
Output is correct |
22 |
Correct |
164 ms |
42552 KB |
Output is correct |
23 |
Correct |
170 ms |
41144 KB |
Output is correct |
24 |
Correct |
191 ms |
42676 KB |
Output is correct |
25 |
Correct |
7 ms |
19028 KB |
Output is correct |
26 |
Correct |
73 ms |
22868 KB |
Output is correct |
27 |
Correct |
220 ms |
38824 KB |
Output is correct |
28 |
Correct |
295 ms |
42292 KB |
Output is correct |
29 |
Correct |
299 ms |
42760 KB |
Output is correct |
30 |
Correct |
293 ms |
42936 KB |
Output is correct |
31 |
Correct |
296 ms |
43064 KB |
Output is correct |
32 |
Correct |
287 ms |
43316 KB |
Output is correct |