#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
int C,Kof[200100];
VI ans;
struct segtree{
struct DATA{
long long prfmn,prfmx,sum;
DATA(long long k){
prfmn=min(0ll,k),
prfmx=max(0ll,k),sum=k;
}
DATA(){
prfmn=prfmx=sum=0;
}
DATA(DATA a,DATA b){ sum=a.sum+b.sum;
prfmn=min(a.prfmn,b.prfmn+a.sum);
prfmx=max(a.prfmx,b.prfmx+a.sum);
}
} T[1<<19];
void Add(int i,int l,int r,int p,int v){
if(l==r) return void(T[i]=DATA(T[i].sum+v));
if(l+r>>1>=p) Add(i*2,l,l+r>>1,p,v);
else Add(i*2+1,l+r+2>>1,r,p,v);
T[i]=DATA(T[i*2],T[i*2+1]);
}
int getans(DATA k,int b){
return (b?k.prfmx>C:k.prfmn>=-C)?C+k.sum-k.prfmx:k.sum-k.prfmn;
}
int Qry(int i,int l,int r,DATA prv){
if(l==r) return getans(DATA(T[i],prv),T[i].sum>=0);
DATA c=DATA(T[i*2+1],prv);
if(c.prfmx-c.prfmn<=C)
return Qry(i*2,l,l+r>>1,c);
return Qry(i*2+1,l+r+2>>1,r,prv);
}
} seg;
vector<pair<int,int>> stuf[200100];
VI distribute_candies(VI c,VI l,VI r,VI v) {
int n=c.size(),q=l.size();
for(int i=0;i<q;i++)
stuf[l[i]].push_back({i+1,v[i]}),
stuf[r[i]+1].push_back({i+1,-v[i]});
for(int i=0;i<n;i++){
for(auto[a,b]:stuf[i])
seg.Add(1,0,q,a,b);
C=c[i];
ans.push_back(seg.Qry(1,0,q,seg.T[0]));
}
return ans;
}
Compilation message
candies.cpp: In member function 'void segtree::Add(int, int, int, int, int)':
candies.cpp:25:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | if(l+r>>1>=p) Add(i*2,l,l+r>>1,p,v);
| ~^~
candies.cpp:25:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | if(l+r>>1>=p) Add(i*2,l,l+r>>1,p,v);
| ~^~
candies.cpp:26:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | else Add(i*2+1,l+r+2>>1,r,p,v);
| ~~~^~
candies.cpp: In member function 'int segtree::Qry(int, int, int, segtree::DATA)':
candies.cpp:36:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | return Qry(i*2,l,l+r>>1,c);
| ~^~
candies.cpp:37:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | return Qry(i*2+1,l+r+2>>1,r,prv);
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
17244 KB |
Output is correct |
2 |
Correct |
3 ms |
17244 KB |
Output is correct |
3 |
Correct |
2 ms |
17500 KB |
Output is correct |
4 |
Correct |
3 ms |
17616 KB |
Output is correct |
5 |
Correct |
3 ms |
17496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
36552 KB |
Output is correct |
2 |
Correct |
157 ms |
35760 KB |
Output is correct |
3 |
Correct |
167 ms |
35632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17244 KB |
Output is correct |
2 |
Correct |
91 ms |
29524 KB |
Output is correct |
3 |
Correct |
36 ms |
24088 KB |
Output is correct |
4 |
Correct |
178 ms |
37832 KB |
Output is correct |
5 |
Correct |
168 ms |
37916 KB |
Output is correct |
6 |
Correct |
158 ms |
38364 KB |
Output is correct |
7 |
Correct |
159 ms |
37828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
17244 KB |
Output is correct |
2 |
Correct |
2 ms |
17244 KB |
Output is correct |
3 |
Correct |
57 ms |
28068 KB |
Output is correct |
4 |
Correct |
34 ms |
21984 KB |
Output is correct |
5 |
Correct |
82 ms |
31936 KB |
Output is correct |
6 |
Correct |
85 ms |
32712 KB |
Output is correct |
7 |
Correct |
85 ms |
32952 KB |
Output is correct |
8 |
Correct |
82 ms |
31884 KB |
Output is correct |
9 |
Correct |
84 ms |
33076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
17244 KB |
Output is correct |
2 |
Correct |
3 ms |
17244 KB |
Output is correct |
3 |
Correct |
2 ms |
17500 KB |
Output is correct |
4 |
Correct |
3 ms |
17616 KB |
Output is correct |
5 |
Correct |
3 ms |
17496 KB |
Output is correct |
6 |
Correct |
158 ms |
36552 KB |
Output is correct |
7 |
Correct |
157 ms |
35760 KB |
Output is correct |
8 |
Correct |
167 ms |
35632 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
91 ms |
29524 KB |
Output is correct |
11 |
Correct |
36 ms |
24088 KB |
Output is correct |
12 |
Correct |
178 ms |
37832 KB |
Output is correct |
13 |
Correct |
168 ms |
37916 KB |
Output is correct |
14 |
Correct |
158 ms |
38364 KB |
Output is correct |
15 |
Correct |
159 ms |
37828 KB |
Output is correct |
16 |
Correct |
2 ms |
17244 KB |
Output is correct |
17 |
Correct |
2 ms |
17244 KB |
Output is correct |
18 |
Correct |
57 ms |
28068 KB |
Output is correct |
19 |
Correct |
34 ms |
21984 KB |
Output is correct |
20 |
Correct |
82 ms |
31936 KB |
Output is correct |
21 |
Correct |
85 ms |
32712 KB |
Output is correct |
22 |
Correct |
85 ms |
32952 KB |
Output is correct |
23 |
Correct |
82 ms |
31884 KB |
Output is correct |
24 |
Correct |
84 ms |
33076 KB |
Output is correct |
25 |
Correct |
2 ms |
17244 KB |
Output is correct |
26 |
Correct |
35 ms |
21868 KB |
Output is correct |
27 |
Correct |
92 ms |
29248 KB |
Output is correct |
28 |
Correct |
156 ms |
36812 KB |
Output is correct |
29 |
Correct |
182 ms |
37316 KB |
Output is correct |
30 |
Correct |
163 ms |
37576 KB |
Output is correct |
31 |
Correct |
159 ms |
37572 KB |
Output is correct |
32 |
Correct |
150 ms |
37832 KB |
Output is correct |