#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
struct IT {
vector<ll> tr, lazy, lo;
vector<int> vec;
IT (int sz) : tr(4 * sz), lazy(4 * sz), lo(4 * sz) {}
ll getSum (int k, int l, int r) {
return tr[k] + ll(r - l + 1) * lazy[k];
}
ll getLo (int k) {
return lo[k] + lazy[k];
}
void refine (int k, int l, int r, int mid) {
lo[k] = min(getLo(2 * k), getLo(2 * k + 1));
tr[k] = getSum(2 * k, l, mid) + getSum(2 * k + 1, mid + 1, r);
}
void pushDown (int k, int l, int r, int mid) {
lazy[2 * k] += lazy[k], lazy[2 * k + 1] += lazy[k], lazy[k] = 0;
refine(k, l, r, mid);
}
void update (int a, int b, ll val, int k, int l, int r) {
if (b < l || r < a) return;
if (a <= l && r <= b) {
lazy[k] += val;
return;
}
int mid = (l + r) >> 1;
pushDown(k, l, r, mid);
update(a, b, val, 2 * k, l, mid);
update(a, b, val, 2 * k + 1, mid + 1, r);
refine(k, l, r, mid);
}
ll querySum (int a, int b, int k, int l, int r) {
if (b < l || r < a) return 0;
if (a <= l && r <= b) return getSum(k, l, r);
int mid = (l + r) >> 1;
pushDown(k, l, r, mid);
return querySum(a, b, 2 * k, l, mid) + querySum(a, b, 2 * k + 1, mid + 1, r);
}
ll queryLo (int a, int b, int k, int l, int r) {
if (b < l || r < a) return LLONG_MAX;
if (a <= l && r <= b) return getLo(k);
int mid = (l + r) >> 1;
pushDown(k, l, r, mid);
return min(queryLo(a, b, 2 * k, l, mid), queryLo(a, b, 2 * k + 1, mid + 1, r));
}
void walk (int a, int b, int k, int l, int r) {
if (b < l || r < a || getLo(k) > 0) return;
if (l == r) {
vec.push_back(l);
return;
}
int mid = (l + r) >> 1;
pushDown(k, l, r, mid);
walk(a, b, 2 * k, l, mid);
walk(a, b, 2 * k + 1, mid + 1, r);
}
vector<int> findZero (int a, int b, int k, int l, int r) {
vec.clear();
walk(a, b, k, l, r);
return vec;
}
};
const int mn = 3e5 + 5;
ll a[mn], base[mn], bound[mn], ans[mn];
vector<pii> qry[mn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; ll D; cin >> n >> D;
for (int i = 1; i <= n; i++) {
cin >> a[i];
base[i] = a[i] % D, bound[i] = a[i] / D;
}
int q; cin >> q;
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
qry[r].push_back({l, i});
}
IT tree(n), cur(n);
ll last = LLONG_MAX;
priority_queue<int> pq; pq.push(0);
for (int i = n; i >= 1; i--) {
last = min(bound[i], last - (base[i] > base[i + 1]));
tree.update(i, i, bound[i] - last, 1, 1, n);
cur.update(i, i, last, 1, 1, n);
if (bound[i] == last) pq.push(i);
}
for (int i = n; i >= 1; i--) {
if (pq.top() == i) pq.pop();
ll decr = tree.queryLo(i, i, 1, 1, n);
while (decr) {
ll tmp = tree.queryLo(pq.top() + 1, i, 1, 1, n);
tree.update(pq.top() + 1, i, -tmp, 1, 1, n);
cur.update(pq.top() + 1, i, tmp, 1, 1, n);
if (pq.top() + 1 <= i - 1) {
vector<int> pos = tree.findZero(pq.top() + 1, i - 1, 1, 1, n);
for (int u : pos) pq.push(u);
}
decr -= tmp;
}
for (auto [l, id] : qry[i]) {
if (cur.queryLo(l, i, 1, 1, n) < 0) ans[id] = -1;
else ans[id] = tree.querySum(l, i, 1, 1, n);
}
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:132:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
132 | for (auto [l, id] : qry[i]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14996 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
8 ms |
15708 KB |
Output is correct |
5 |
Correct |
6 ms |
15452 KB |
Output is correct |
6 |
Correct |
6 ms |
15452 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
5 ms |
15452 KB |
Output is correct |
9 |
Correct |
4 ms |
15660 KB |
Output is correct |
10 |
Correct |
5 ms |
15692 KB |
Output is correct |
11 |
Correct |
5 ms |
15448 KB |
Output is correct |
12 |
Correct |
5 ms |
15708 KB |
Output is correct |
13 |
Correct |
5 ms |
15720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
711 ms |
85748 KB |
Output is correct |
2 |
Correct |
603 ms |
85860 KB |
Output is correct |
3 |
Correct |
83 ms |
22608 KB |
Output is correct |
4 |
Correct |
602 ms |
83792 KB |
Output is correct |
5 |
Correct |
538 ms |
83792 KB |
Output is correct |
6 |
Correct |
525 ms |
84996 KB |
Output is correct |
7 |
Correct |
540 ms |
85088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
26448 KB |
Output is correct |
2 |
Correct |
858 ms |
90592 KB |
Output is correct |
3 |
Correct |
845 ms |
90696 KB |
Output is correct |
4 |
Correct |
819 ms |
90448 KB |
Output is correct |
5 |
Correct |
842 ms |
90472 KB |
Output is correct |
6 |
Correct |
749 ms |
85276 KB |
Output is correct |
7 |
Correct |
762 ms |
85332 KB |
Output is correct |
8 |
Correct |
760 ms |
87684 KB |
Output is correct |
9 |
Correct |
793 ms |
87708 KB |
Output is correct |
10 |
Correct |
617 ms |
87504 KB |
Output is correct |
11 |
Correct |
589 ms |
87500 KB |
Output is correct |
12 |
Correct |
701 ms |
89528 KB |
Output is correct |
13 |
Correct |
869 ms |
89508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
461 ms |
74576 KB |
Output is correct |
2 |
Correct |
590 ms |
85076 KB |
Output is correct |
3 |
Correct |
262 ms |
37200 KB |
Output is correct |
4 |
Correct |
631 ms |
87460 KB |
Output is correct |
5 |
Correct |
658 ms |
82900 KB |
Output is correct |
6 |
Correct |
803 ms |
90452 KB |
Output is correct |
7 |
Correct |
646 ms |
76640 KB |
Output is correct |
8 |
Correct |
817 ms |
90452 KB |
Output is correct |
9 |
Correct |
538 ms |
84304 KB |
Output is correct |
10 |
Correct |
490 ms |
84048 KB |
Output is correct |
11 |
Correct |
588 ms |
87376 KB |
Output is correct |
12 |
Correct |
514 ms |
86660 KB |
Output is correct |
13 |
Correct |
770 ms |
90116 KB |
Output is correct |
14 |
Correct |
671 ms |
86152 KB |
Output is correct |
15 |
Correct |
790 ms |
90192 KB |
Output is correct |
16 |
Correct |
654 ms |
86096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14996 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
8 ms |
15708 KB |
Output is correct |
5 |
Correct |
6 ms |
15452 KB |
Output is correct |
6 |
Correct |
6 ms |
15452 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
5 ms |
15452 KB |
Output is correct |
9 |
Correct |
4 ms |
15660 KB |
Output is correct |
10 |
Correct |
5 ms |
15692 KB |
Output is correct |
11 |
Correct |
5 ms |
15448 KB |
Output is correct |
12 |
Correct |
5 ms |
15708 KB |
Output is correct |
13 |
Correct |
5 ms |
15720 KB |
Output is correct |
14 |
Correct |
711 ms |
85748 KB |
Output is correct |
15 |
Correct |
603 ms |
85860 KB |
Output is correct |
16 |
Correct |
83 ms |
22608 KB |
Output is correct |
17 |
Correct |
602 ms |
83792 KB |
Output is correct |
18 |
Correct |
538 ms |
83792 KB |
Output is correct |
19 |
Correct |
525 ms |
84996 KB |
Output is correct |
20 |
Correct |
540 ms |
85088 KB |
Output is correct |
21 |
Correct |
133 ms |
26448 KB |
Output is correct |
22 |
Correct |
858 ms |
90592 KB |
Output is correct |
23 |
Correct |
845 ms |
90696 KB |
Output is correct |
24 |
Correct |
819 ms |
90448 KB |
Output is correct |
25 |
Correct |
842 ms |
90472 KB |
Output is correct |
26 |
Correct |
749 ms |
85276 KB |
Output is correct |
27 |
Correct |
762 ms |
85332 KB |
Output is correct |
28 |
Correct |
760 ms |
87684 KB |
Output is correct |
29 |
Correct |
793 ms |
87708 KB |
Output is correct |
30 |
Correct |
617 ms |
87504 KB |
Output is correct |
31 |
Correct |
589 ms |
87500 KB |
Output is correct |
32 |
Correct |
701 ms |
89528 KB |
Output is correct |
33 |
Correct |
869 ms |
89508 KB |
Output is correct |
34 |
Correct |
461 ms |
74576 KB |
Output is correct |
35 |
Correct |
590 ms |
85076 KB |
Output is correct |
36 |
Correct |
262 ms |
37200 KB |
Output is correct |
37 |
Correct |
631 ms |
87460 KB |
Output is correct |
38 |
Correct |
658 ms |
82900 KB |
Output is correct |
39 |
Correct |
803 ms |
90452 KB |
Output is correct |
40 |
Correct |
646 ms |
76640 KB |
Output is correct |
41 |
Correct |
817 ms |
90452 KB |
Output is correct |
42 |
Correct |
538 ms |
84304 KB |
Output is correct |
43 |
Correct |
490 ms |
84048 KB |
Output is correct |
44 |
Correct |
588 ms |
87376 KB |
Output is correct |
45 |
Correct |
514 ms |
86660 KB |
Output is correct |
46 |
Correct |
770 ms |
90116 KB |
Output is correct |
47 |
Correct |
671 ms |
86152 KB |
Output is correct |
48 |
Correct |
790 ms |
90192 KB |
Output is correct |
49 |
Correct |
654 ms |
86096 KB |
Output is correct |
50 |
Correct |
822 ms |
90532 KB |
Output is correct |
51 |
Correct |
770 ms |
85332 KB |
Output is correct |
52 |
Correct |
785 ms |
87628 KB |
Output is correct |
53 |
Correct |
582 ms |
87432 KB |
Output is correct |
54 |
Correct |
718 ms |
89552 KB |
Output is correct |
55 |
Correct |
1186 ms |
90516 KB |
Output is correct |
56 |
Correct |
580 ms |
86112 KB |
Output is correct |
57 |
Correct |
545 ms |
83796 KB |
Output is correct |
58 |
Correct |
562 ms |
83816 KB |
Output is correct |
59 |
Correct |
736 ms |
88404 KB |
Output is correct |
60 |
Correct |
641 ms |
86096 KB |
Output is correct |
61 |
Correct |
583 ms |
87496 KB |
Output is correct |
62 |
Correct |
610 ms |
87512 KB |
Output is correct |
63 |
Correct |
705 ms |
89296 KB |
Output is correct |
64 |
Correct |
573 ms |
86108 KB |
Output is correct |