提交时间:2023-08-14 11:59:14

运行 ID: 98084

#include<bits/stdc++.h> using namespace std; #define ll long long ll a[2000010],ans[2000010],cnt,po,e[2000010]; /*ll qumod(int x,int mod){ if(x==0){ return 0; } if(x>0){ while(x>=mod){ x-=mod; } return x; }else{ while(-x>=mod){ x+=mod; } return x; } }*/ inline int ls(int x) { return x<<1; } inline int rs(int x) { return x<<1|1; } void pushup(int p) { ans[p]=ans[ls(p)]+ans[rs(p)]; if(ans[ls(p)]+ans[rs(p)]>=0){ e[p]=e[ls(p)]+e[rs(p)]; }else{ e[p]=e[ls(p)]; } //cout<<p<<" "<<ans[p]<<endl; } void build(int p,int l,int r) { //cout<<p<<" "<<l<<" "<<r<<endl; if(l==r) { ans[p]=a[l]; if(a[l]>=po){ e[p]=1; } return ; } int mid=(l+r)>>1; build(ls(p),l,mid); build(rs(p),mid+1,r); pushup(p); } ll query(int p,int nl,int nr,int l,int r) { ll c=0; //cout<<p<<" "<<nl<<" "<<nr<<" "<<l<<" "<<r<<" "<<endl; if(l<=nl&&nr<=r) { //cout<<ans[p]<<endl; //cout<<ans[p]<<" "<<e[p]<<endl; return ans[p]-e[p]*po; } int mid=(nl+nr)>>1; //cout<<mid<<endl; if(l<=mid) { c+=query(ls(p),nl,mid,l,r); } if(mid<r) { c+=query(rs(p),mid+1,nr,l,r); } //cout<<c<<endl; return c; } int main() { //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); int n,m; cin>>n>>m>>po; for(int i=1; i<=n; i++) { cin>>a[i]; //a[i]=(a[i]%p); } build(1,1,n); for(int k=1;k<=m;k++){ int l,r; cin>>l>>r; cout<<query(1,1,n,l,r)<<endl; } return 0; } /*10 5 6 7 2 -3 17 23 3248 109 -32489 2 7 2 3 5 9 1 10 2 8 4 4*/