提交时间:2023-08-14 12:24:53

运行 ID: 98176

#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e6; int a[N],p,n,pla,maxn,minn,m,s[N]; signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>m>>p; minn=1145141919810; for(int i=1;i<=n;++i){ cin>>a[i]; maxn=max(maxn,a[i]); minn=min(minn,a[i]); s[i]=s[i-1]+a[i]; } if(maxn<p&&minn>=0){ for(int i=1;i<=m;++i){ int l,r; cin>>l>>r; cout<<(s[r]-s[l-1])%p<<endl; } return 0; } if(minn>=p){ for(int i=1;i<=m;++i){ int l,r; cin>>l>>r; cout<<(s[r]-s[l-1])-p*(r-l+1)<<endl; } return 0; } if(a[2]>a[1]){ for(int i=1;i<=n;++i) if(a[i]>p){ pla=i; break; } for(int i=1;i<=m;++i){ int l,r; cin>>l>>r; if(r<pla) if(s[r]-s[l-1]>0) cout<<(s[r]-s[l-1])%p<<endl; else cout<<s[r]-s[l-1]<<endl; else if(l>=pla) cout<<(s[r]-s[l-1])-p*(r-l+1)<<endl; else{ int x=l,y=r; while(x<y){ int mid=l=r>>1; if(s[mid]-s[l-1]<0) l=mid+1; else r=mid; } cout<<(s[x]-s[l-1])+(s[pla-1]-s[x-1])%p+s[r]-s[pla-1]-p*(r-pla+1)<<endl; } } return 0; }else{ int pl; for(int i=1;i<=n;++i) if(a[i]<0){ pl=i; break; } for(int i=1;i<=m;++i){ int l,r,x,y; cin>>l>>r; x=l,y=r; while(x<y){ int mid=x+y>>1; if(s[mid]-s[l]-p*(mid-l+1)>=0) x=mid+1; else y=mid; } if(r<pl) cout<<s[x-1]-s[l-1]-p*(x-l)+(s[r]-s[x-1])%p<<endl; else cout<<s[x-1]-s[l-1]-p*(x-l)+(s[pl-1]-s[x-1])%p+(s[r]-s[pl-1])<<endl; } return 0; } return 0; }