제출 #1286907

#제출 시각아이디문제언어결과실행 시간메모리
1286907Faisal_SaqibSnowball (JOI21_ho_t2)C++20
100 / 100
1651 ms10080 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const ll inf=2e18,N=2e5+10; ll a[N],pre[N],suf[N]; pair<ll,ll> qry[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // ll tp=9e18; // cout<<tp<<endl; ll n,q; cin>>n>>q; a[0]=-inf; a[n+1]=inf; for(int i=1;i<=n;i++) { cin>>a[i]; } qry[0].first=0; qry[0].second=0; for(int i=1;i<=q;i++) { cin>>qry[i].first; qry[i].second=i; if(i>1)qry[i].first+=qry[i-1].first; } sort(qry,qry+q+1); pre[0]=qry[0].second; for(int i=1;i<=q;i++) { pre[i]=min(pre[i-1],qry[i].second); } suf[q]=qry[q].second; for(int i=q-1;i>=0;i--) { suf[i]=min(suf[i+1],qry[i].second); } for(int i=1;i<=n;i++) { // a[i-1] .. a[i] .. a[i+1] // find min x a[i-1]<=x and x<=a[i] // time(x+1,i-1)>time(x,i) ll l=a[i-1]-1,r=a[i]; while(l+1<r) { ll mid=(l+r)/2; bool pos=0; ll fi=q+1,fi1=q+1; int it=0; it=lower_bound(qry,qry+q+1,make_pair(mid-a[i],q+1ll))-qry; if(it>0) { fi=pre[it-1]; } it=lower_bound(qry,qry+q+1,make_pair(mid+1-a[i-1],-1ll))-qry; if(it!=(q+1)) { fi1=suf[it]; } if(fi<fi1) { r=mid; } else { l=mid; } } ll st=r; l=a[i],r=a[i+1]+1; while(l+1<r) { ll mid=(l+r)/2; bool pos=0; ll fi=q+1,fi1=q+1; // >= mid-a[i] int it=0; it=lower_bound(qry,qry+q+1,make_pair(mid-a[i],-1ll))-qry; if(it!=(q+1)) { fi=suf[it]; } it=lower_bound(qry,qry+q+1,make_pair(mid-1-a[i+1],q+1ll))-qry; if(it>0) { fi1=pre[it-1]; } // cout<<"For "<<i<<' '<<a[i]<<endl; // cout<<"checking "<<mid<<' '<<fi<<' '<<fi1<<endl; if(fi<fi1) { l=mid; } else { r=mid; } } // cout<<"Hola "<<i<<' '<<st<<' '<<l<<endl; cout<<l-st<<endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...