#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 5001001001001001001;
vector<pair<int,int>> q[1000005];
int z[1000005], t[1000005], prefix[1000005], res[1000005];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a, d;
cin >> a >> d;
for (int i = 0; i < a; i++) cin >> z[i];
int c;
cin >> c;
for (int i = 0; i < c; i++) {
int x, y;
cin >> x >> y;
x--; y--;
q[y].emplace_back(x, i);
}
for (int i = a - 2; i >= 0; i--) {
t[i]=(z[i]+d-1)/d;
}
for (int i = 0; i < a; i++) {
prefix[i + 1] = prefix[i] + t[i];
}
vector<pair<int,int>> st;
st.emplace_back(-1, 0);
for (int i = 0; i < a; i++) {
while (st.size() > 1 && t[st.back().first] >= t[i]) {
st.pop_back();
}
st.emplace_back(i, st.back().second + (i - st.back().first) * t[i]);
for (auto p : q[i]) {
int x = p.first, idx = p.second;
int pos = lower_bound(st.begin(), st.end(), make_pair(x, -INF)) - st.begin();
int pre = st.back().second - st[pos - 1].second - t[st[pos].first] * (x - 1 - st[pos - 1].first);
res[idx] = prefix[i + 1] - prefix[x] - pre;
if (z[x] < d * (t[x] - t[st[pos].first])) {
res[idx] = -1;
}
}
}
for (int i = 0; i < c; i++) {
cout << res[i] << '\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... |