提交时间:2023-08-14 11:58:23

运行 ID: 98070

#include<cstdio> const int N=1e6+1;int a[N];long long s[N]; int main(){int n,m,p,A=1,B=1;scanf("%d%d%d",&n,&m,&p);for(int i=1;i<=n;i++) {scanf("%d",a+i);if(a[i]>=p)A=0;if(a[i]<p)B=0;s[i]=s[i-1]+a[i];} for(int i=1;i<=m;i++){int l,r;scanf("%d%d",&l,&r);if(l>r){puts("0");continue;}long long w=0; if(A)printf("%lld\n",(s[r]-s[l-1])%p);else if(B)printf("%lld\n",(s[r]-s[l-1])-(r-l+1)*p); else{for(int j=l;j<=r;j++){w+=a[j];if(w>=p)w-=p;}printf("%lld\n",w);}}return 0;}