#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);
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]);
}
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;
}
// All updates are in range
if(pivot==-1) {
rs[i]=S[0];
continue;
}
// Find the last time touching the upper wall
if(Smax[pivot]==S[pivot]) {
l=pivot+1,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;
while(l<=r) {
int mid=(l+r)/2;
if(Smax[mid]>c[i]) {
pivot=mid;
l=mid+1;
} else
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;
while(l<=r) {
int mid=(l+r)/2;
if(Smin[mid]<0) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
rs[i]=S[pivot];
}
}
return rs;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
163 ms |
11992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
65 ms |
9680 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |