提交时间:2023-08-14 16:37:14

运行 ID: 98376

#include<bits/stdc++.h> using namespace std; #define ll long long template<typename T>inline void read(T &ret){ ret=0;T fh=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')fh=-1;ch=getchar();} while(isdigit(ch))ret=ret*10+(ch^48),ch=getchar(); ret*=fh; } const int N=1e5+5; ll n,m,p,a[N],sum[N],mn=2e9,mx,l,r; inline void solve1(){ while(m--){ read(l);read(r); printf("%lld\n",(sum[r]-sum[l-1])%p); } } inline void solve2(){ while(m--){ read(l);read(r); printf("%lld\n",sum[r]-sum[l-1]-(r-l+1)*p); } } inline void solve3(){ while(m--){ ll ans=0; for(int i=l;i<=r;++i){ ans+=a[i]; if(ans>=p)ans-=p; } printf("%lld\n",ans); } } int main(){ read(n);read(m);read(p); for(int i=1;i<=n;++i){ read(a[i]); sum[i]=sum[i-1]+a[i]; mn=min(mn,a[i]); mx=max(mx,a[i]); } if(0<=mn&&mx<p)solve1(); else if(mn>=p)solve2(); else solve3(); return 0; }