#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MX = 1<<19;
vector<ll> smx(MX), smn(MX), st(MX);
void upd(int i, int s, int e, int t, ll v) {
if (s==e) {
smx[i]=max(0ll, v);
smn[i]=min(0ll, v);
st[i]=v;
return ;
}
int m=(s+e)/2;
if (t<=m) upd(i*2, s, m, t, v);
else upd(i*2+1, m+1, e, t, v);
st[i]=st[i*2]+st[i*2+1];
smx[i]=max(smx[i*2], st[i*2]+smx[i*2+1]);
smn[i]=min(smn[i*2], st[i*2]+smn[i*2+1]);
}
ll get(int i, int s, int e, ll c) {
if (s==e) return min(max(st[i], 0ll), c);
int m=(s+e)/2;
if (smx[i*2+1]-smn[i*2+1]>c) return get(i*2+1, m+1, e, c);
ll r=get(i*2, s, m, c);
if (r+smx[i*2+1]>c) return c-(smx[i*2+1]-st[i*2+1]);
if (r+smn[i*2+1]<0) return st[i*2+1]-smn[i*2+1];
return r+st[i*2+1];
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int N=c.size(), Q=l.size();
vector<int> rt;
vector<vector<int>> q(N+1);
for (int i=0; i<Q; i++) q[l[i]].emplace_back(i+1), q[r[i]+1].emplace_back(-(i+1));
for (int i=0; i<N; i++) {
for (auto &j:q[i]) {
if (j>0) j--, upd(1, 0, Q-1, j, v[j]);
else j=-j-1, upd(1, 0, Q-1, j, 0);
}
rt.emplace_back(get(1, 0, Q-1, c[i]));
}
return rt;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12620 KB |
Output is correct |
2 |
Correct |
6 ms |
12620 KB |
Output is correct |
3 |
Correct |
7 ms |
12620 KB |
Output is correct |
4 |
Correct |
8 ms |
12640 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
35124 KB |
Output is correct |
2 |
Correct |
425 ms |
34360 KB |
Output is correct |
3 |
Correct |
445 ms |
34216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12632 KB |
Output is correct |
2 |
Correct |
277 ms |
22664 KB |
Output is correct |
3 |
Correct |
85 ms |
22332 KB |
Output is correct |
4 |
Correct |
442 ms |
36264 KB |
Output is correct |
5 |
Correct |
459 ms |
36544 KB |
Output is correct |
6 |
Correct |
464 ms |
36972 KB |
Output is correct |
7 |
Correct |
438 ms |
36340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12620 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
125 ms |
21736 KB |
Output is correct |
4 |
Correct |
72 ms |
21200 KB |
Output is correct |
5 |
Correct |
210 ms |
30244 KB |
Output is correct |
6 |
Correct |
202 ms |
30844 KB |
Output is correct |
7 |
Correct |
201 ms |
31536 KB |
Output is correct |
8 |
Correct |
198 ms |
30008 KB |
Output is correct |
9 |
Correct |
210 ms |
31500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12620 KB |
Output is correct |
2 |
Correct |
6 ms |
12620 KB |
Output is correct |
3 |
Correct |
7 ms |
12620 KB |
Output is correct |
4 |
Correct |
8 ms |
12640 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
6 |
Correct |
446 ms |
35124 KB |
Output is correct |
7 |
Correct |
425 ms |
34360 KB |
Output is correct |
8 |
Correct |
445 ms |
34216 KB |
Output is correct |
9 |
Correct |
6 ms |
12632 KB |
Output is correct |
10 |
Correct |
277 ms |
22664 KB |
Output is correct |
11 |
Correct |
85 ms |
22332 KB |
Output is correct |
12 |
Correct |
442 ms |
36264 KB |
Output is correct |
13 |
Correct |
459 ms |
36544 KB |
Output is correct |
14 |
Correct |
464 ms |
36972 KB |
Output is correct |
15 |
Correct |
438 ms |
36340 KB |
Output is correct |
16 |
Correct |
6 ms |
12620 KB |
Output is correct |
17 |
Correct |
6 ms |
12628 KB |
Output is correct |
18 |
Correct |
125 ms |
21736 KB |
Output is correct |
19 |
Correct |
72 ms |
21200 KB |
Output is correct |
20 |
Correct |
210 ms |
30244 KB |
Output is correct |
21 |
Correct |
202 ms |
30844 KB |
Output is correct |
22 |
Correct |
201 ms |
31536 KB |
Output is correct |
23 |
Correct |
198 ms |
30008 KB |
Output is correct |
24 |
Correct |
210 ms |
31500 KB |
Output is correct |
25 |
Correct |
6 ms |
12620 KB |
Output is correct |
26 |
Correct |
76 ms |
21316 KB |
Output is correct |
27 |
Correct |
276 ms |
22264 KB |
Output is correct |
28 |
Correct |
463 ms |
34832 KB |
Output is correct |
29 |
Correct |
431 ms |
35264 KB |
Output is correct |
30 |
Correct |
444 ms |
35432 KB |
Output is correct |
31 |
Correct |
457 ms |
35640 KB |
Output is correct |
32 |
Correct |
441 ms |
35816 KB |
Output is correct |