#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n=c.size(),m=v.size();
vector<long long> S(m+1),Smin(m+1),Smax(m+1),SS(m),SSmin(m),SSmax(m);
for(int i=m-1;i>=0;i--) {
S[i]=S[i+1]+v[i];
Smin[i]=min(Smin[i+1],S[i]);
Smax[i]=max(Smax[i+1],S[i]);
}
SS[0]=v[0];
for(int i=1;i<m;i++)
SS[i]=SS[i-1]+v[i];
SSmin.back()=SSmax.back()=SS.back();
for(int i=m-2;i>=0;i--) {
SSmin[i]=min(SSmin[i+1],SS[i]);
SSmax[i]=max(SSmax[i+1],SS[i]);
}
vector<int> rs(n);
for(int i=0;i<n;i++) {
int l=0,r=m-1,pivot=-1;
while(l<=r) {
int mid=(l+r)/2;
if(Smax[mid]-Smin[mid]>c[i]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
// If all updates are in range
if(pivot==-1) {
l=pivot-1,r=m-1,pivot=0;
while(l<=r) {
int mid=(l+r)/2;
if(SSmin[mid]<0) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
rs[i]=S[pivot];
continue;
}
// Find the last time touching the upper wall
if(Smax[pivot]==S[pivot]) {
l=pivot,r=m;
while(l<=r) {
int mid=(l+r)/2;
if(Smin[mid]==Smin[pivot]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
l=pivot,r=m-1;
int limit=SS[pivot];
while(l<=r) {
int mid=(l+r)/2;
if(SSmax[mid]>limit)
l=mid+1;
else {
pivot=mid;
r=mid-1;
}
}
rs[i]=c[i]+S[pivot];
} else { // Find the last time touching the lower wall
l=pivot,r=m;
while(l<=r) {
int mid=(l+r)/2;
if(Smax[mid]==Smax[pivot]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
l=pivot,r=m-1;
int limit=SS[pivot];
while(l<=r) {
int mid=(l+r)/2;
if(SSmin[mid]<limit)
l=mid+1;
else {
pivot=mid;
r=mid-1;
}
}
rs[i]=S[pivot];
}
}
return rs;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
163 ms |
16824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
74 ms |
14424 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |