#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], qs[N];
ll tree[4 * N], lazy[4 * N], mn[4 * N];
vector<pair<int, int>> Q[N];
void build(int now = 1, int l = 1, int r = n) {
lazy[now] = -1e18;
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] == -1e18) return;
tree[now] = lazy[now] * (r - l + 1);
if (l != r) lazy[2 * now] = lazy[2 * now + 1] = lazy[now];
lazy[now] = -1e18;
}
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);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
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] + (b[i - 1] > b[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
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& [l, idx] : Q[i]) {
if (qmn(l, i) + p[l] < 0) ans[idx] = -1;
else ans[idx] = qs[i] - qs[l - 1] - query(l, i);
}
}
for (int i = 1;i <= q;i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7520 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
6 ms |
8028 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
5 ms |
7772 KB |
Output is correct |
7 |
Correct |
6 ms |
7736 KB |
Output is correct |
8 |
Correct |
6 ms |
7972 KB |
Output is correct |
9 |
Correct |
5 ms |
7772 KB |
Output is correct |
10 |
Correct |
6 ms |
7996 KB |
Output is correct |
11 |
Correct |
5 ms |
8028 KB |
Output is correct |
12 |
Correct |
5 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
390 ms |
55892 KB |
Output is correct |
2 |
Correct |
403 ms |
55380 KB |
Output is correct |
3 |
Correct |
93 ms |
17492 KB |
Output is correct |
4 |
Correct |
255 ms |
55044 KB |
Output is correct |
5 |
Correct |
267 ms |
54968 KB |
Output is correct |
6 |
Correct |
322 ms |
54928 KB |
Output is correct |
7 |
Correct |
345 ms |
54984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
21332 KB |
Output is correct |
2 |
Correct |
440 ms |
62800 KB |
Output is correct |
3 |
Correct |
422 ms |
62804 KB |
Output is correct |
4 |
Correct |
475 ms |
62800 KB |
Output is correct |
5 |
Correct |
437 ms |
62800 KB |
Output is correct |
6 |
Correct |
412 ms |
56400 KB |
Output is correct |
7 |
Correct |
447 ms |
56468 KB |
Output is correct |
8 |
Correct |
414 ms |
59480 KB |
Output is correct |
9 |
Correct |
424 ms |
59444 KB |
Output is correct |
10 |
Correct |
415 ms |
62876 KB |
Output is correct |
11 |
Correct |
438 ms |
62840 KB |
Output is correct |
12 |
Correct |
431 ms |
63144 KB |
Output is correct |
13 |
Correct |
428 ms |
63236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
41732 KB |
Output is correct |
2 |
Correct |
422 ms |
56200 KB |
Output is correct |
3 |
Correct |
255 ms |
27988 KB |
Output is correct |
4 |
Correct |
431 ms |
59280 KB |
Output is correct |
5 |
Correct |
392 ms |
60868 KB |
Output is correct |
6 |
Correct |
434 ms |
62664 KB |
Output is correct |
7 |
Correct |
383 ms |
46836 KB |
Output is correct |
8 |
Correct |
428 ms |
62428 KB |
Output is correct |
9 |
Correct |
295 ms |
55204 KB |
Output is correct |
10 |
Correct |
274 ms |
55164 KB |
Output is correct |
11 |
Correct |
426 ms |
59360 KB |
Output is correct |
12 |
Correct |
319 ms |
58704 KB |
Output is correct |
13 |
Correct |
428 ms |
62292 KB |
Output is correct |
14 |
Correct |
280 ms |
58448 KB |
Output is correct |
15 |
Correct |
388 ms |
62284 KB |
Output is correct |
16 |
Correct |
279 ms |
58456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7520 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
6 ms |
8028 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
5 ms |
7772 KB |
Output is correct |
7 |
Correct |
6 ms |
7736 KB |
Output is correct |
8 |
Correct |
6 ms |
7972 KB |
Output is correct |
9 |
Correct |
5 ms |
7772 KB |
Output is correct |
10 |
Correct |
6 ms |
7996 KB |
Output is correct |
11 |
Correct |
5 ms |
8028 KB |
Output is correct |
12 |
Correct |
5 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
8024 KB |
Output is correct |
14 |
Correct |
390 ms |
55892 KB |
Output is correct |
15 |
Correct |
403 ms |
55380 KB |
Output is correct |
16 |
Correct |
93 ms |
17492 KB |
Output is correct |
17 |
Correct |
255 ms |
55044 KB |
Output is correct |
18 |
Correct |
267 ms |
54968 KB |
Output is correct |
19 |
Correct |
322 ms |
54928 KB |
Output is correct |
20 |
Correct |
345 ms |
54984 KB |
Output is correct |
21 |
Correct |
150 ms |
21332 KB |
Output is correct |
22 |
Correct |
440 ms |
62800 KB |
Output is correct |
23 |
Correct |
422 ms |
62804 KB |
Output is correct |
24 |
Correct |
475 ms |
62800 KB |
Output is correct |
25 |
Correct |
437 ms |
62800 KB |
Output is correct |
26 |
Correct |
412 ms |
56400 KB |
Output is correct |
27 |
Correct |
447 ms |
56468 KB |
Output is correct |
28 |
Correct |
414 ms |
59480 KB |
Output is correct |
29 |
Correct |
424 ms |
59444 KB |
Output is correct |
30 |
Correct |
415 ms |
62876 KB |
Output is correct |
31 |
Correct |
438 ms |
62840 KB |
Output is correct |
32 |
Correct |
431 ms |
63144 KB |
Output is correct |
33 |
Correct |
428 ms |
63236 KB |
Output is correct |
34 |
Correct |
371 ms |
41732 KB |
Output is correct |
35 |
Correct |
422 ms |
56200 KB |
Output is correct |
36 |
Correct |
255 ms |
27988 KB |
Output is correct |
37 |
Correct |
431 ms |
59280 KB |
Output is correct |
38 |
Correct |
392 ms |
60868 KB |
Output is correct |
39 |
Correct |
434 ms |
62664 KB |
Output is correct |
40 |
Correct |
383 ms |
46836 KB |
Output is correct |
41 |
Correct |
428 ms |
62428 KB |
Output is correct |
42 |
Correct |
295 ms |
55204 KB |
Output is correct |
43 |
Correct |
274 ms |
55164 KB |
Output is correct |
44 |
Correct |
426 ms |
59360 KB |
Output is correct |
45 |
Correct |
319 ms |
58704 KB |
Output is correct |
46 |
Correct |
428 ms |
62292 KB |
Output is correct |
47 |
Correct |
280 ms |
58448 KB |
Output is correct |
48 |
Correct |
388 ms |
62284 KB |
Output is correct |
49 |
Correct |
279 ms |
58456 KB |
Output is correct |
50 |
Correct |
412 ms |
62616 KB |
Output is correct |
51 |
Correct |
433 ms |
56248 KB |
Output is correct |
52 |
Correct |
417 ms |
59472 KB |
Output is correct |
53 |
Correct |
382 ms |
63064 KB |
Output is correct |
54 |
Correct |
413 ms |
63080 KB |
Output is correct |
55 |
Correct |
402 ms |
62548 KB |
Output is correct |
56 |
Correct |
261 ms |
58196 KB |
Output is correct |
57 |
Correct |
242 ms |
55120 KB |
Output is correct |
58 |
Correct |
267 ms |
55200 KB |
Output is correct |
59 |
Correct |
425 ms |
60752 KB |
Output is correct |
60 |
Correct |
295 ms |
58240 KB |
Output is correct |
61 |
Correct |
414 ms |
63060 KB |
Output is correct |
62 |
Correct |
470 ms |
57940 KB |
Output is correct |
63 |
Correct |
494 ms |
62800 KB |
Output is correct |
64 |
Correct |
302 ms |
58432 KB |
Output is correct |