提交时间:2023-08-14 12:27:49

运行 ID: 98207

#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 5; #define ll long long ll n, m, p; ll a[N]; ll sum[N << 2]; #define ls rt << 1 #define rs rt << 1 | 1 inline void build(int l, int r, int rt) { if(l == r) { sum[rt] = a[l]; return ; } int mid = l + r >> 1; build(l, mid, ls); build(mid + 1, r, rs); sum[rt] = sum[ls] + sum[rs]; } inline int query(int l, int r, int rt, int x, int y) { if(x <= l && r <= y) return sum[rt]; ll mid = l + r >> 1, ans = 0; if(mid <= x) ans += query(l, mid, ls, x, y); if(mid > y) ans += query(mid + 1, r, rs, x, y); return ans; } signed main() { // freopen("programme.in", "r", stdin); // freopen("programme.out", "w", stdout); ios::sync_with_stdio(0); cin>>n>>m>>p; bool A = 1, B = 1; for(int i = 1;i <= n;i ++) { cin>>a[i]; if(a[i] >= p) A = 0; if(a[i] < p) B = 0; } if(A) { build(1, n, 1); while(m --) { int l, r; cin>>l>>r; cout<<query(1, n, 1, l, r) % p<<endl; } return 0; } if(B) { for(int i = 1;i <= n;i ++) a[i] -= p; build(1, n, 1); while(m --) { int l, r; cin>>l>>r; cout<<query(1, n, 1, l, r)<<endl; } return 0; } for(int i = 1; i <= m; ++ i) { int l,r; cin>>l>>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; }