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;
using ll = long long;
const int N = 300300;
int n;
ll a[N], b[N], p[N], ans[N];
ll qs[N];
ll tree[4 * N], lazy[4 * N];
ll mn[4 * N];
void build(int now = 1, int l = 1, int r = n) {
if (l == r) {
mn[now] = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * now, l, mid), build(2 * now + 1, mid + 1, r);
mn[now] = min(mn[2 * now], mn[2 * now + 1]);
}
ll qmn(int ql, int qr, int now = 1, int l = 1, int r = n) {
if (l > qr || r < ql) return 1e18;
if (ql <= l && r <= qr) return mn[now];
int mid = (l + r) / 2;
return min(qmn(ql, qr, 2 * now, l, mid), qmn(ql, qr, 2 * now + 1, mid + 1, r));
}
void lzy(int now, int l, int r) {
if (lazy[now] == -1) return;
tree[now] = lazy[now] * (r - l + 1);
if (l != r) lazy[2 * now] = lazy[2 * now + 1] = lazy[now];
lazy[now] = -1;
}
void update(int ql, int qr, ll w, int now = 1, int l = 1, int r = n) {
lzy(now, l, r);
if (l > qr || r < ql) return;
if (ql <= l && r <= qr) {
lazy[now] = w;
lzy(now, l, r);
return;
}
int mid = (l + r) / 2;
update(ql, qr, w, 2 * now, l, mid), update(ql, qr, w, 2 * now + 1, mid + 1, r);
tree[now] = tree[2 * now] + tree[2 * now + 1];
}
ll query(int ql, int qr, int now = 1, int l = 1, int r = n) {
lzy(now, l, r);
if (l > qr || r < ql) return 0ll;
if (ql <= l && r <= qr) return tree[now];
int mid = (l + r) / 2;
return query(ql, qr, 2 * now, l, mid) + query(ql, qr, 2 * now + 1, mid + 1, r);
}
vector<pair<int, int>> Q[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(lazy, -1, sizeof lazy);
ll d;
cin >> n >> d;
for (int i = 1;i <= n;i++) {
// c[i] = d * a[i] + b[i]
ll c;
cin >> c;
a[i] = c / d;
b[i] = c % d;
}
for (int i = 2;i <= n;i++) {
p[i] = p[i - 1];
if (b[i - 1] > b[i]) p[i]++;
}
for (int i = 1;i <= n;i++) {
a[i] -= p[i];
qs[i] = qs[i - 1] + a[i];
}
build();
int q;
cin >> q;
for (int i = 1;i <= q;i++) {
int l, r;
cin >> l >> r;
Q[r].push_back({ l, i });
}
// do like subtask 3
int pre = 1;
stack<pair<ll, int>> st;
for (int i = 1;i <= n;i++) {
while (!st.empty() && st.top().first >= a[i]) {
st.pop();
}
if (st.empty()) update(1, i, a[i]);
else update(st.top().second + 1, i, a[i]);
st.push({ a[i], i });
for (auto& x : Q[i]) {
if (qmn(x.first, i) + p[x.first] < 0) {
ans[x.second] = -1;
continue;
}
ans[x.second] = qs[i] - qs[x.first - 1] - query(x.first, i);
}
}
for (int i = 1;i <= q;i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:80:9: warning: unused variable 'pre' [-Wunused-variable]
80 | int pre = 1;
| ^~~
# | 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... |