#include<bits/stdc++.h>
#include "candies.h"
using namespace std;
const long long N=1000000000000000000LL,S=800003,T=800004;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,C[200009],L[200009],R[200009],V[200009],f[200009],pi,F[200009],mn,mx,l,r,z,za,seg[800009],segmn[800009],segmx[800009],lef,rig,mid;
pair <long long, long long> p[800009];
vector <int> ans;
void upd(int rr){
if(rr==0) return;
seg[rr]=seg[rr*2]+seg[rr*2+1];
segmn[rr]=min(segmn[rr*2],seg[rr*2]+segmn[rr*2+1]);
segmx[rr]=max(segmx[rr*2],seg[rr*2]+segmx[rr*2+1]);
upd(rr/2);
}
void UPD(int q){
seg[q+za-1]=f[q];
segmn[q+za-1]=f[q];
segmx[q+za-1]=f[q];
upd((q+za-1)/2);
}
void read(int q, int w, int rr){
if(q>r||w<l) return;
if(q>=l&&w<=r){
seg[T]=seg[S]+seg[rr];
segmn[T]=min(segmn[S],seg[S]+segmn[rr]);
segmx[T]=max(segmx[S],seg[S]+segmx[rr]);
swap(seg[S],seg[T]);swap(segmn[S],segmn[T]);swap(segmx[S],segmx[T]);
return;
}
read(q,(q+w)/2,rr*2);
read((q+w)/2+1,w,rr*2+1);
}
vector<int> distribute_candies(vector<int> Cc, vector<int> Ll, vector<int> Rr, vector<int> Vv) {
a=Cc.size();tes=Ll.size();ans.resize(a);
for(i=1; i<=a; i++){
C[i]=Cc[i-1];
}
L[1]=1;R[1]=a;V[1]=N;
L[2]=1;R[2]=a;V[2]=-N;tes+=2;
for(t=3; t<=tes; t++){
L[t]=Ll[t-3]+1;R[t]=Rr[t-3]+1;V[t]=Vv[t-3];
}
for(t=1; t<=tes; t++){
pi++;p[pi]={L[t],t};pi++;p[pi]={R[t]+1,-t};
}
sort(p+1,p+pi+1);
za=1;
while(za<tes) za*=2;
int ee=1;
for(i=1; i<=a; i++){
while(ee<=pi&&p[ee].first<=i){
ii=p[ee].second;
if(ii>0){
f[ii]=V[ii];
UPD(ii);
}else{
f[-ii]=0;
UPD(-ii);
}
ee++;
}
mn=N;mx=-N;
lef=0;rig=tes+1;
while(1){
if(lef+1>=rig) break;
mid=(lef+rig)/2;
l=mid;r=tes;seg[S]=0;segmn[S]=N;segmx[S]=-N;
read(1,za,1);mn=segmn[S];mx=segmx[S];
if(mx-mn>=C[i]){
lef=mid;
}else{
rig=mid;
}
}
mid=lef;
l=mid;r=tes;seg[S]=0;segmn[S]=N;segmx[S]=-N;
read(1,za,1);mn=segmn[S];mx=segmx[S];
//
zx=seg[1];
l=1;r=mid;seg[S]=0;segmn[S]=N;segmx[S]=-N;
read(1,za,1);xc=seg[S];
e=xc-f[mid];
mn+=e;mx+=e;
//
if(mx-mn>=C[i]){
if(xc==mn){
ans[i-1]=C[i]+(zx-mx);
}else{
ans[i-1]=zx-mn;
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
937 ms |
31844 KB |
Output is correct |
2 |
Correct |
990 ms |
35812 KB |
Output is correct |
3 |
Correct |
1029 ms |
35652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
197 ms |
27132 KB |
Output is correct |
3 |
Correct |
354 ms |
8628 KB |
Output is correct |
4 |
Correct |
1046 ms |
35220 KB |
Output is correct |
5 |
Correct |
1006 ms |
38104 KB |
Output is correct |
6 |
Correct |
865 ms |
38436 KB |
Output is correct |
7 |
Correct |
838 ms |
37776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
93 ms |
27148 KB |
Output is correct |
4 |
Correct |
321 ms |
5464 KB |
Output is correct |
5 |
Correct |
862 ms |
31928 KB |
Output is correct |
6 |
Correct |
799 ms |
31884 KB |
Output is correct |
7 |
Correct |
730 ms |
31940 KB |
Output is correct |
8 |
Correct |
849 ms |
31888 KB |
Output is correct |
9 |
Correct |
942 ms |
31940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
937 ms |
31844 KB |
Output is correct |
7 |
Correct |
990 ms |
35812 KB |
Output is correct |
8 |
Correct |
1029 ms |
35652 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
197 ms |
27132 KB |
Output is correct |
11 |
Correct |
354 ms |
8628 KB |
Output is correct |
12 |
Correct |
1046 ms |
35220 KB |
Output is correct |
13 |
Correct |
1006 ms |
38104 KB |
Output is correct |
14 |
Correct |
865 ms |
38436 KB |
Output is correct |
15 |
Correct |
838 ms |
37776 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
93 ms |
27148 KB |
Output is correct |
19 |
Correct |
321 ms |
5464 KB |
Output is correct |
20 |
Correct |
862 ms |
31928 KB |
Output is correct |
21 |
Correct |
799 ms |
31884 KB |
Output is correct |
22 |
Correct |
730 ms |
31940 KB |
Output is correct |
23 |
Correct |
849 ms |
31888 KB |
Output is correct |
24 |
Correct |
942 ms |
31940 KB |
Output is correct |
25 |
Correct |
0 ms |
296 KB |
Output is correct |
26 |
Correct |
336 ms |
6516 KB |
Output is correct |
27 |
Correct |
175 ms |
29660 KB |
Output is correct |
28 |
Correct |
968 ms |
36404 KB |
Output is correct |
29 |
Correct |
922 ms |
36784 KB |
Output is correct |
30 |
Correct |
917 ms |
36820 KB |
Output is correct |
31 |
Correct |
864 ms |
37076 KB |
Output is correct |
32 |
Correct |
834 ms |
37264 KB |
Output is correct |