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

运行 ID: 98129

#include<bits/stdc++.h> using namespace std; #define ll long long inline void read(ll &x){ x=0;bool f=0;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=1;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); if(f)x=-x; } const int N=1e6+10; ll n,m,p,a[N],s[N]; int get_sub(){ int A=1,B=1,C=1,D=1; for(int i=1;i<=n;i++){ if(a[i]<0||a[i]>=p)A=0; if(a[i]<p)B=0; if(a[i]<a[i-1]&&i!=1)C=0; if(a[i]>a[i-1]&&i!=1)D=0; } if(A)return 1; if(B)return 2; if(C)return 3; if(D)return 4; return 5; } int main(){ // freopen("D.in","r",stdin); // freopen("D.out","w",stdout); read(n);read(m);read(p); for(int i=1;i<=n;i++)read(a[i]); for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i]; int op=get_sub(); // int op=3; if(op==1){ while(m--){ ll l,r; read(l);read(r); cout<<(s[r]-s[l-1])%p<<endl; } return 0; } if(op==2){ while(m--){ ll l,r; read(l);read(r); cout<<s[r]-s[l-1]-(r-l+1)*p<<endl; } return 0; } if(op==3){ int posp=1; while(a[posp]<p)posp++; while(m--){ ll l,r; read(l);read(r); if(s[r]-s[l-1]<p)cout<<s[r]-s[l-1]<<endl; else{ if(r<posp)cout<<(s[r]-s[l-1])%p<<endl; else{ int k=upper_bound(s+1,s+n+1,p+s[l-1])-s; if(posp>k)cout<<(s[posp-1]-s[l-1])%p+s[r]-s[posp-1]-(r-posp+1)*p<<endl; else{ cout<<(s[k-1]-s[l-1])%p+s[r]-s[k-1]-(r-k+1)*p<<endl; } } } } return 0; } ll l,r; while(m--){ read(l);read(r); long long ans=0; for(int j=l; j<=r; ++j) { ans+=a[j]; if(ans>=p)ans-=p; } cout<<ans<<endl; } return 0; } /* 7 1 3 -2 -2 0 1 2 3 5 2 7 */