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;
vector<ll> memo = a;
bool ok = true;
for (int i = r - 1; i >= l; --i) {
ll diff = a[i + 1] - a[i];
if (diff < 0) {
ll me = (-diff - 1) / d + 1;
ans += me;
a[i] -= d * me;
if (a[i] < 0) ok = false;
}
}
a = memo;
if (!ok) 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... |