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;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long d;
cin >> n >> d;
vector<long long> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
bool ok = true;
long long md = 0;
vector<long long> b = c;
for (int i = l; i <= r; ++i) {
b[i] -= md;
if (b[i] < 0) {
ok = false;
break;
}
long long tmp = b[i] % d;
b[i] -= tmp;
md += tmp;
}
if (!ok) {
cout << -1 << '\n';
continue;
}
long long mn = LLONG_MAX;
for (int i = r; i >= l; --i) {
mn = min(mn, b[i]);
b[i] -= mn;
}
long long ans = 0;
for (int i = l; i <= r; ++i) {
ans += b[i] / d;
}
cout << ans << '\n';
}
return 0;
}
# | 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... |