// JOI 2023 - 2024
// JJ_ Master
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, d;
cin >> n >> d;
vector<int> a(n + 1);
for(int i=1;i<=n;i++) cin >> a[i];
int q;
cin >> q;
for(int i=1;i<=q;i++) {
int l, r;
cin >> l >> r;
vector<int> cur(n + 1);
for(int x=1;x<=r;x++) {
cur[x] = cur[x-1] + ((d + (a[x] % d) - (cur[x - 1] % d)) % d);
}
int uses = 1e15;
int ans = 0;
bool works = true;
for(int x=r;x>=1;x--) {
int req = (a[x] - cur[x]) / d;
if(req < 0) works = false;
uses = min(uses, req);
ans += req - uses;
}
if(works) cout << ans << '\n';
else cout << "-1" << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |