이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |