This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, k, q;
cin >> n >> k;
vector<int> c(n+1), ac(n+1, 0), a(n+1, 0), b(n+1, 0), aff(n+1, 0);
for (int i = 1; i <= n; i++) {
cin >> c[i];
ac[i] = c[i];
}
int prv = n;
for (int i = n - 1; i >= 1; i--) {
if (ac[i] > ac[i + 1]) {
int diff = ac[i] - ac[i + 1];
diff = (diff + k - 1) / k;
a[i] += diff;
ac[i] -= diff * k;
} else {
int t = 0;
for (int j = i + 1; j <= prv; j++) t++, aff[j] = t;
prv = i;
}
}
int t = 0;
for (int j = 1; j <= prv; j++) t++, aff[j] = t;
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
// cout << a[r] - a[l - 1] << '\n';
// cout << ((c[r] - ac[r]) / k) * (r - l + 1) << '\n';
int L = ac[l], R = ac[r];
L += (c[r] - R);
if (L < 0) cout << "-1\n";
else cout << a[r] - a[l - 1] - ((c[r] - ac[r]) / k) * min(aff[r], r - l + 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... |