#ifndef LOCAL
#pragma GCC Optimize("O3,Ofast,unroll-loops")
#pragma GCC Target("bmi,bmi2,avx,avx2")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin() (x).rend()
#ifndef LOCAL
#define endl "\n"
#endif
mt19937 rnd(11);
const int MAX_N = (1<<19);
pii T[MAX_N * 2];
void push(int i, int x, int l, int r) {
T[i].s += x;
T[i].f += x * (r - l);
}
void push(int i, int l, int r) {
int m = (l + r) / 2;
push(i * 2, T[i].s, l, m);
push(i * 2 + 1, T[i].s, m, r);
T[i].s = 0;
}
void update(int i, int l, int r, int ql, int qr, int qx) {
if (l >= qr || r <= ql) {
return;
}
if (ql <= l && r <= qr) {
push(i, qx, l, r);
} else {
int m = (l + r) / 2;
push(i, l, r);
update(i * 2, l, m, ql, qr, qx);
update(i * 2 + 1, m, r, ql, qr, qx);
T[i].f = T[i * 2].f + T[i * 2 + 1].f;
}
}
int get(int i, int l, int r, int ql, int qr) {
if (l >= qr || r <= ql) {
return 0;
}
if (ql <= l && r <= qr) {
return T[i].f;
} else {
int m = (l + r) / 2;
push(i, l, r);
return get(i * 2, l, m, ql, qr) + get(i * 2 + 1, m, r, ql, qr);
}
}
struct F {
vector<int> t;
F(int n) {
t.assign(n, 0);
}
void upd(int i, int x) {
for (; i < t.size(); i = (i|(i + 1))) {
t[i] += x;
}
}
int get(int r) {
int ans = 0;
for (; r >= 0; r = (r&(r + 1)) - 1) {
ans += t[r];
}
return ans;
}
};
void solve() {
memset(T, 0, sizeof(T));
int n, d;
cin >> n >> d;
vector<int> a(n);
for (auto &x : a) {
cin >> x;
}
int q;
cin >> q;
vector<array<int, 3>> quests(q);
int ind = 0;
for (auto &x : quests) {
cin >> x[0] >> x[1];
--x[0]; --x[1];
x[2] = ind++;
}
sort(all(quests), [&](array<int, 3> a, array<int, 3> b) {
return a[1] < b[1];
});
F f(n);
vector<int> ans(q, -1);
vector<int> st;
int l = 0;
ind = 0;
auto getA = [&](int ind) {
return a[ind] + f.get(ind);
};
for (int i = 0; i < n; ++i) {
if (i && a[i] < a[i - 1]) {
st.pop_back();
int cnt = (a[i - 1] - a[i] + d - 1) / d;
f.upd(i, cnt * d);
while (!st.empty()) {
int ind = st.back();
int delta = (getA(ind + 1) - getA(ind)) / d;
if (delta <= cnt) {
cnt -= delta;
f.upd(ind + 1, -delta * d);
update(1, 0, MAX_N, ind + 1, i, delta);
st.pop_back();
} else {
f.upd(ind + 1, -cnt * d);
update(1, 0, MAX_N, ind + 1, i, cnt);
cnt = 0;
break;
}
}
update(1, 0, MAX_N, 0, i, cnt);
f.upd(0, -cnt * d);
}
// for (int j = 0; j < n; ++j) {
// cout << getA(j) << ' ';
// }
// cout << endl;
while (getA(l) < 0) {
++l;
}
st.pb(i);
while (ind < quests.size() && quests[ind][1] == i) {
if (quests[ind][0] >= l) {
ans[quests[ind][2]] = get(1, 0, MAX_N, quests[ind][0], i + 1);
}
++ind;
}
}
for (auto &x : ans) {
cout << x << ' ';
}
cout << endl;
}
signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| # | 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... |