#include "candies.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define ll long long
#define all(s) s.begin(), s.end()
using namespace std;
const int nmax = 200005;
int n, q;
struct item{
ll maxi, mini, sum;
item(){
maxi = mini = sum = 0;
}
};
vector<item>tree(4*nmax);
vector<pair<int,int>>RangeL[nmax], RangeR[nmax];
void update(int l, int r, int indx, ll val, int nod){
if(l == r){
tree[nod].maxi = tree[nod].mini = tree[nod].sum = val;
}
else{
int mid = l + (r - l) / 2;
if(indx <= mid){
update(l, mid, indx, val, 2*nod);
}
else{
update(mid+1, r, indx, val, 2*nod + 1);
}
tree[nod].sum = tree[2*nod].sum + tree[2*nod+1].sum;
tree[nod].maxi = max(tree[2*nod+1].maxi, tree[2*nod].maxi + tree[2*nod+1].sum);
tree[nod].mini = min(tree[2*nod+1].mini, tree[2*nod].mini + tree[2*nod+1].sum);
}
}
pair<pair<ll, ll>,int> query(int l, int r, ll sufmin, ll sufmax, ll sufsum, ll val, int nod){
if(l == r){
pair<pair<ll,ll>,ll>it;
it.fr.sc = min(tree[nod].mini + sufsum, sufmin);
it.fr.fr = max(tree[nod].maxi + sufsum, sufmax);
it.sc = tree[nod].sum;
return it;
}
else{
int mid = l + (r - l) / 2;
pair<pair<ll,ll>,ll>it;
it.fr.fr = max(tree[2*nod+1].maxi + sufsum, sufmax);
it.fr.sc = min(tree[2*nod+1].mini + sufsum, sufmin);
if(it.fr.fr - it.fr.sc >= val){
return query(mid+1, r, sufmin, sufmax, sufsum, val, 2*nod+1);
}
else{
sufmin = min(sufmin, tree[2*nod+1].mini + sufsum);
sufmax = max(sufmax, tree[2*nod+1].maxi + sufsum);
sufsum += tree[2*nod+1].sum;
return query(l, mid, sufmin, sufmax, sufsum, val, 2*nod);
}
}
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = (int)c.size();
q = (int)l.size();
for(int i=0;i<q;i++){
RangeL[l[i]+1].push_back({i+1, -v[i]});
RangeR[r[i]+1].push_back({i+1, 0});
}
vector<int> s(n);
for(int i=1;i<=n;i++){
for(auto it : RangeL[i]){
update(1, q+1, it.fr, it.sc, 1);
}
auto it = query(1, q+1, 0, 0, 0, c[i-1], 1);
if(it.fr.fr - it.fr.sc < c[i-1]){
s[i-1] = -it.fr.sc;
}
else{
if(it.sc < 0){
if(it.fr.fr > c[i-1]) s[i-1] = 0;
else s[i-1] = c[i - 1] - it.fr.fr;
}
else{
if(-it.fr.sc > c[i-1]) s[i-1] = c[i-1];
else s[i-1] = -it.fr.sc;
}
}
for(auto it : RangeR[i]){
update(1, q+1, it.fr, it.sc, 1);
}
}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28364 KB |
Output is correct |
2 |
Correct |
16 ms |
28364 KB |
Output is correct |
3 |
Correct |
18 ms |
28492 KB |
Output is correct |
4 |
Correct |
17 ms |
28584 KB |
Output is correct |
5 |
Correct |
19 ms |
28620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
504 ms |
43544 KB |
Output is correct |
2 |
Correct |
485 ms |
43376 KB |
Output is correct |
3 |
Correct |
474 ms |
43384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
28492 KB |
Output is correct |
2 |
Correct |
263 ms |
38780 KB |
Output is correct |
3 |
Correct |
90 ms |
32052 KB |
Output is correct |
4 |
Correct |
479 ms |
43464 KB |
Output is correct |
5 |
Correct |
495 ms |
43456 KB |
Output is correct |
6 |
Correct |
531 ms |
43468 KB |
Output is correct |
7 |
Correct |
510 ms |
43476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28364 KB |
Output is correct |
2 |
Correct |
16 ms |
28492 KB |
Output is correct |
3 |
Correct |
169 ms |
36460 KB |
Output is correct |
4 |
Correct |
87 ms |
31036 KB |
Output is correct |
5 |
Correct |
237 ms |
38676 KB |
Output is correct |
6 |
Correct |
245 ms |
38672 KB |
Output is correct |
7 |
Correct |
237 ms |
38672 KB |
Output is correct |
8 |
Correct |
230 ms |
38564 KB |
Output is correct |
9 |
Correct |
233 ms |
38616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28364 KB |
Output is correct |
2 |
Correct |
16 ms |
28364 KB |
Output is correct |
3 |
Correct |
18 ms |
28492 KB |
Output is correct |
4 |
Correct |
17 ms |
28584 KB |
Output is correct |
5 |
Correct |
19 ms |
28620 KB |
Output is correct |
6 |
Correct |
504 ms |
43544 KB |
Output is correct |
7 |
Correct |
485 ms |
43376 KB |
Output is correct |
8 |
Correct |
474 ms |
43384 KB |
Output is correct |
9 |
Correct |
19 ms |
28492 KB |
Output is correct |
10 |
Correct |
263 ms |
38780 KB |
Output is correct |
11 |
Correct |
90 ms |
32052 KB |
Output is correct |
12 |
Correct |
479 ms |
43464 KB |
Output is correct |
13 |
Correct |
495 ms |
43456 KB |
Output is correct |
14 |
Correct |
531 ms |
43468 KB |
Output is correct |
15 |
Correct |
510 ms |
43476 KB |
Output is correct |
16 |
Correct |
20 ms |
28364 KB |
Output is correct |
17 |
Correct |
16 ms |
28492 KB |
Output is correct |
18 |
Correct |
169 ms |
36460 KB |
Output is correct |
19 |
Correct |
87 ms |
31036 KB |
Output is correct |
20 |
Correct |
237 ms |
38676 KB |
Output is correct |
21 |
Correct |
245 ms |
38672 KB |
Output is correct |
22 |
Correct |
237 ms |
38672 KB |
Output is correct |
23 |
Correct |
230 ms |
38564 KB |
Output is correct |
24 |
Correct |
233 ms |
38616 KB |
Output is correct |
25 |
Correct |
15 ms |
28368 KB |
Output is correct |
26 |
Correct |
89 ms |
30916 KB |
Output is correct |
27 |
Correct |
251 ms |
38760 KB |
Output is correct |
28 |
Correct |
460 ms |
43472 KB |
Output is correct |
29 |
Correct |
465 ms |
43460 KB |
Output is correct |
30 |
Correct |
461 ms |
43496 KB |
Output is correct |
31 |
Correct |
471 ms |
43468 KB |
Output is correct |
32 |
Correct |
474 ms |
43480 KB |
Output is correct |