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>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
const int nax = 300 * 1000 + 10;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, d;
cin >> n >> d;
vector<ll> a(n);
for (ll &x : a) cin >> x;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
ll ans = 0;
ll open = 0;
for (int i = r; i >= l; --i) {
ll dif = (i == l ? a[i] : a[i] - a[i - 1]);
ll demand = 0;
if (dif < 0) {
demand = (abs(dif) - 1) / d + 1;
} else {
demand = - (dif / d);
}
if (demand > 0) {
ans += (ll)i * demand;
open += demand;
} else {
ll me = min(-demand, open);
ans -= me * i;
open -= me;
}
}
if (open > 0) cout << "-1\n";
else cout << ans << "\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... |