#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
const int MAXN = 2e5+10;
using namespace std;
ll smx[MAXN*4], smn[MAXN*4], soma[MAXN*4];
int m(int l, int r){
return (l+r)>>1;
}
void update(int pos, int l, int r, int id, ll newVal){
if(l == r){
smx[pos] = smn[pos] = soma[pos] = newVal;
smx[pos] = max(smx[pos], 0LL);
smn[pos] = min(smn[pos], 0LL);
return;
}
if(id <= m(l,r))
update(pos<<1 , l , m(l,r), id, newVal);
else update(pos<<1|1, m(l,r)+1, r, id, newVal);
smx[pos] = max(smx[pos<<1], soma[pos<<1]+smx[pos<<1|1]);
smn[pos] = min(smn[pos<<1], soma[pos<<1]+smn[pos<<1|1]);
soma[pos] = soma[pos<<1]+soma[pos<<1|1];
}
ll qry(int pos, int l, int r, ll c){
if(l == r){
return min( max(0LL, soma[pos]), c );
}
if(smx[pos<<1|1]-smn[pos<<1|1] >= c)
return qry(pos<<1|1, m(l,r)+1, r, c);
ll rr = qry(pos<<1 , l , m(l,r), c);
if(rr+smx[pos<<1|1] > c)
return c+(soma[pos<<1|1]-smx[pos<<1|1]);
else if(rr+smn[pos<<1|1] < 0)
return (soma[pos<<1|1]-smn[pos<<1|1]);
return rr+soma[pos<<1|1];
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
vector<vector<int> > sweep(n+1);
for(int i = 0; i < q; i++){
sweep[l[i]].push_back(i+1);
sweep[r[i]+1].push_back(-i-1);
}
vector<int> ret;
for(int i= 0; i < n; i++){
for(auto &e : sweep[i]){
int id = e < 0 ? -e-1 : e-1;
update(1,0,q-1, id, e < 0 ? 0 : v[id]);
}
ret.push_back(qry(1,0,q-1, c[i]));
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
322 ms |
30316 KB |
Output is correct |
2 |
Correct |
340 ms |
30288 KB |
Output is correct |
3 |
Correct |
308 ms |
30312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
172 ms |
22584 KB |
Output is correct |
3 |
Correct |
55 ms |
10160 KB |
Output is correct |
4 |
Correct |
326 ms |
36152 KB |
Output is correct |
5 |
Correct |
314 ms |
36564 KB |
Output is correct |
6 |
Correct |
309 ms |
36932 KB |
Output is correct |
7 |
Correct |
302 ms |
36376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
88 ms |
19148 KB |
Output is correct |
4 |
Correct |
53 ms |
7880 KB |
Output is correct |
5 |
Correct |
131 ms |
26620 KB |
Output is correct |
6 |
Correct |
138 ms |
26936 KB |
Output is correct |
7 |
Correct |
135 ms |
26940 KB |
Output is correct |
8 |
Correct |
136 ms |
27060 KB |
Output is correct |
9 |
Correct |
155 ms |
26956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
576 KB |
Output is correct |
6 |
Correct |
322 ms |
30316 KB |
Output is correct |
7 |
Correct |
340 ms |
30288 KB |
Output is correct |
8 |
Correct |
308 ms |
30312 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
172 ms |
22584 KB |
Output is correct |
11 |
Correct |
55 ms |
10160 KB |
Output is correct |
12 |
Correct |
326 ms |
36152 KB |
Output is correct |
13 |
Correct |
314 ms |
36564 KB |
Output is correct |
14 |
Correct |
309 ms |
36932 KB |
Output is correct |
15 |
Correct |
302 ms |
36376 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
88 ms |
19148 KB |
Output is correct |
19 |
Correct |
53 ms |
7880 KB |
Output is correct |
20 |
Correct |
131 ms |
26620 KB |
Output is correct |
21 |
Correct |
138 ms |
26936 KB |
Output is correct |
22 |
Correct |
135 ms |
26940 KB |
Output is correct |
23 |
Correct |
136 ms |
27060 KB |
Output is correct |
24 |
Correct |
155 ms |
26956 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
60 ms |
9196 KB |
Output is correct |
27 |
Correct |
181 ms |
22304 KB |
Output is correct |
28 |
Correct |
307 ms |
34936 KB |
Output is correct |
29 |
Correct |
327 ms |
35368 KB |
Output is correct |
30 |
Correct |
335 ms |
35524 KB |
Output is correct |
31 |
Correct |
327 ms |
35624 KB |
Output is correct |
32 |
Correct |
307 ms |
35780 KB |
Output is correct |