#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#define ll long long
using namespace std;
int main(){
ll n,q;
cin>>n>>q;
vector<ll> vec(n);
for(int i=0;i<n;i++) cin>>vec[i];
vector<ll> wind(q);
for(int i=0;i<q;i++) cin>>wind[i];
for(int i=1;i<q;i++) wind[i]+=wind[i-1];
vector<ll> newind;
bool sign=(wind[0]>0);
if(wind[0]==0){
for(int i=1;i<q;i++){
if(wind[i]!=0){
sign=(wind[i]>0);
break;
}
}
}
ll curmax=wind[0];
for(int i=1;i<q;i++){
if(sign==1&&wind[i]>=0){
curmax=max(curmax,wind[i]);
}
else if(sign==0&&wind[i]<=0){
curmax=min(curmax,wind[i]);
}
else{
newind.push_back(curmax);
sign=(!sign);
curmax=wind[i];
}
}
if(curmax!=0) newind.push_back(curmax);
ll maxpos=-1,maxneg=0,tmp=newind.size();
vector<pair<ll,ll>> fin;
ll curind=-1;
for(int i=0;i<tmp;i++){
if(newind[i]<0){
maxneg=min(maxneg,newind[i]);
}
else{
if(newind[i]>maxpos){
if(curind>=0&&fin[curind].second==maxneg) fin[curind].first=newind[i];
else fin.push_back(make_pair(newind[i],maxneg)),curind++;
maxpos=newind[i];
}
}
}
ll len=fin.size(),curr,curl=vec[0]+maxneg;
bool flag=1;
for(auto x:wind) if(x>0) flag=0;
if(flag){
for(int i=0;i<n;i++){
cout<<vec[i]-curl<<"\n";
if(i!=n-1) curl=max(vec[i],vec[i+1]+maxneg);
}
return 0;
}
//for(auto x:fin) cout<<x.first<<" "<<x.second<<"\n";
for(int i=0;i<n-1;i++){
ll dist=vec[i+1]-vec[i];
curl=max(curl,vec[i]+maxneg);
if(fin[0].first>=dist+fin[0].second){
cout<<max(dist+fin[0].second,(ll)0)+vec[i]-curl<<"\n";
curl=max(dist+fin[0].second,(ll)0)+vec[i];
continue;
}
ll l=0,r=len-1,ans=0;
if(l==r){
ans=max(ans,min(fin[0].first,dist+fin[0].second));
}
while(l<r){
ll mid=(l+r)>>1;
if(min(fin[mid].first,dist+fin[mid].second)>min(fin[mid+1].first,dist+fin[mid+1].second)){
r=mid;
ans=max(ans,min(fin[mid].first,dist+fin[mid].second));
}
else if(min(fin[mid].first,dist+fin[mid].second)==min(fin[mid+1].first,dist+fin[mid+1].second)){
l=mid,r=mid;
ans=max(ans,min(fin[mid].first,dist+fin[mid].second));
}
else{
l=mid+1;
ans=max(ans,min(fin[mid+1].first,dist+fin[mid+1].second));
}
//cout<<l<<" "<<r<<" "<<ans<<"\n";
}
cout<<ans+vec[i]-curl<<"\n";
//cout<<ans+vec[i]<<" "<<curl<<"\n";
curl=ans+vec[i];
}
cout<<vec[n-1]+maxpos-max(curl,vec[n-1]+maxneg);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:55:23: warning: unused variable 'curr' [-Wunused-variable]
55 | ll len=fin.size(),curr,curl=vec[0]+maxneg;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
280 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
356 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
280 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
356 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
85 ms |
6312 KB |
Output is correct |
21 |
Correct |
77 ms |
6080 KB |
Output is correct |
22 |
Correct |
75 ms |
5924 KB |
Output is correct |
23 |
Correct |
69 ms |
5768 KB |
Output is correct |
24 |
Correct |
75 ms |
5868 KB |
Output is correct |
25 |
Correct |
189 ms |
10408 KB |
Output is correct |
26 |
Correct |
186 ms |
10308 KB |
Output is correct |
27 |
Correct |
180 ms |
9908 KB |
Output is correct |
28 |
Correct |
187 ms |
10020 KB |
Output is correct |
29 |
Correct |
181 ms |
9648 KB |
Output is correct |
30 |
Correct |
157 ms |
8240 KB |
Output is correct |
31 |
Correct |
140 ms |
7468 KB |
Output is correct |
32 |
Correct |
146 ms |
6988 KB |
Output is correct |
33 |
Correct |
20 ms |
1292 KB |
Output is correct |
34 |
Correct |
185 ms |
9932 KB |
Output is correct |
35 |
Correct |
177 ms |
9392 KB |
Output is correct |
36 |
Correct |
186 ms |
10408 KB |
Output is correct |
37 |
Correct |
185 ms |
9908 KB |
Output is correct |
38 |
Correct |
236 ms |
9900 KB |
Output is correct |
39 |
Correct |
182 ms |
10152 KB |
Output is correct |
40 |
Correct |
167 ms |
10144 KB |
Output is correct |
41 |
Correct |
95 ms |
4536 KB |
Output is correct |
42 |
Correct |
140 ms |
6960 KB |
Output is correct |
43 |
Correct |
199 ms |
13484 KB |
Output is correct |
44 |
Correct |
100 ms |
9008 KB |
Output is correct |
45 |
Correct |
171 ms |
10252 KB |
Output is correct |
46 |
Correct |
211 ms |
13644 KB |
Output is correct |
47 |
Correct |
205 ms |
10548 KB |
Output is correct |
48 |
Correct |
209 ms |
10848 KB |
Output is correct |