提交时间:2023-08-14 12:17:46

运行 ID: 98099

#include <cstdio> #include <iostream> using namespace std; typedef long long ll; #define TYPE ll #define FOR(x,y,z) for (TYPE x = (y); x <= (z); x += 1) #define ROF(x,y,z) for (TYPE x = (z); x >= (y); x -= 1) #define FORR(x,y) FOR(x, 1, y) #define ROFF(x,y) ROF(x, y, 1) #define FORS(x,y) FOR(x, 0, (y) - 1) #define ROFS(x,y) ROF(x, (y) - 1, 0) // {{{ OI TYPE read() { TYPE x=0,f=1;char h=getchar(); while(!(h>='0'&&h<='9')){if(h=='-')f=-f;h=getchar();} while(h>='0'&&h<='9'){x=x*10+int(h-'0');h=getchar();} return x*f; } struct OI { OI operator >> (TYPE& x) const { x = read(); return *this; } } oi; // }}} const int N = 1e6 + 600; ll n, m, p; ll a[N], l[N], r[N]; namespace ch1 { bool check = true; ll s[N]; void sol() { FORR(i, n) { s[i] = (s[i - 1] + a[i]) % p; } FORR(i, m) { cout << s[r[i]] - s[l[i] - 1] << endl; } } }; namespace chx { void sol() { FORR(i, m) { ll ans = 0; FOR(j, l[i], r[i]) { ans += a[j]; if (ans >= p) ans -= p; } cout << ans << endl; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("d.in", "r", stdin); oi >> n >> m >> p; FORR(i, n) { // cout << i << endl; oi >> a[i]; if (ch1::check && (a[i] < 0 || a[i] >= p)) ch1::check = false; } FORR(i, m) { // cout << i << endl; oi >> l[i] >> r[i]; } if (ch1::check) { ch1::sol(); return 0; } chx::sol(); return 0; }