#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 2e5 + 5;
struct segtree {
int n;
vector<long long> seg, lazy;
segtree() {
n = N * 3;
seg.resize(n * 2);
lazy.resize(n * 2);
}
void push(int nd, int l, int r) {
if (!lazy[nd]) {
return;
}
seg[nd] += lazy[nd] * (r - l + 1);
if (l != r) {
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
lazy[nd + 1] += lazy[nd];
lazy[rs] += lazy[nd];
}
lazy[nd] = 0;
}
void upd(int nd, int l, int r, int s, int e, int v) {
push(nd, l, r);
if (l >= s && r <= e) {
lazy[nd] = v;
push(nd, l, r);
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
upd(nd + 1, l, mid, s, e, v);
push(rs, mid + 1, r);
} else {
if (mid < s) {
upd(rs, mid + 1, r, s, e, v);
push(nd + 1, l, mid);
} else {
upd(nd + 1, l, mid, s, e, v);
upd(rs, mid + 1, r, s, e, v);
}
}
seg[nd] = seg[nd + 1] + seg[rs];
}
void upd(int s, int e, int v) {
if (s <= e) {
s += N, e += N;
upd(0, 0, n - 1, s, e, v);
}
}
long long qry(int nd, int l, int r, int s, int e) {
push(nd, l, r);
if (l >= s && r <= e) {
return seg[nd];
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
return qry(nd + 1, l, mid, s, e);
} else {
if (mid < s) {
return qry(rs, mid + 1, r, s, e);
} else {
return qry(nd + 1, l, mid, s, e) + qry(rs, mid + 1, r, s, e);
}
}
}
long long qry(int s, int e) {
s += N, e += N;
return qry(0, 0, n - 1, s, e);
}
};
int n, q;
int a[N];
segtree one, two;
long long ans[N];
int pre[N], nxt[N];
vector<array<int, 3>> qt[N];
vector<array<int, 3>> rev[N];
void triangle(int l, int r, int v) {
if (l > r) return;
one.upd(l, n + n, v); //[-n, 2n] --> [0, 3n] (size = 6N)
two.upd(r + 1, n, -v); //[2, n] --> [2 + n, 2n] (size = 4N)
if (r - l + 1 <= n) {
rev[r - l + 1].push_back({l, r, v});
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
stack<int> stk;
for (int i = 1; i <= n; ++i) {
while (!stk.empty() && a[stk.top()] <= a[i]) {
stk.pop();
}
if (stk.empty()) {
pre[i] = -n;
} else {
pre[i] = stk.top();
}
stk.push(i);
}
while (!stk.empty()) {
stk.pop();
}
for (int i = n; i >= 1; --i) {
while (!stk.empty() && a[stk.top()] < a[i]) {
stk.pop();
}
if (stk.empty()) {
nxt[i] = n + 1;
} else {
nxt[i] = stk.top();
}
stk.push(i);
}
for (int i = 1; i <= n; ++i) {
triangle(pre[i] + 1, nxt[i] - 1, a[i]);
triangle(pre[i] + 1, i - 1, -a[i]);
triangle(i + 1, nxt[i] - 1, -a[i]);
}
for (int i = 1; i <= q; ++i) {
int t, l, r;
cin >> t >> l >> r;
qt[t].push_back({l, r, i});
}
for (int t = 1; t <= n; ++t) {
for (auto [l, r, v] : rev[t]) {
one.upd(l, n + n, -v);
two.upd(r + 1, n, v);
}
for (auto [l, r, i] : qt[t]) {
ans[i] = one.qry(l - t, r - t) + two.qry(l, r);
}
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50524 KB |
Output is correct |
2 |
Correct |
9 ms |
50400 KB |
Output is correct |
3 |
Correct |
8 ms |
50352 KB |
Output is correct |
4 |
Correct |
9 ms |
50524 KB |
Output is correct |
5 |
Correct |
9 ms |
50524 KB |
Output is correct |
6 |
Correct |
9 ms |
50540 KB |
Output is correct |
7 |
Correct |
9 ms |
50524 KB |
Output is correct |
8 |
Correct |
9 ms |
50540 KB |
Output is correct |
9 |
Correct |
9 ms |
50524 KB |
Output is correct |
10 |
Correct |
9 ms |
50524 KB |
Output is correct |
11 |
Correct |
9 ms |
50524 KB |
Output is correct |
12 |
Correct |
9 ms |
50780 KB |
Output is correct |
13 |
Correct |
8 ms |
50524 KB |
Output is correct |
14 |
Correct |
10 ms |
50500 KB |
Output is correct |
15 |
Correct |
9 ms |
50524 KB |
Output is correct |
16 |
Correct |
8 ms |
50524 KB |
Output is correct |
17 |
Correct |
11 ms |
50524 KB |
Output is correct |
18 |
Correct |
8 ms |
50448 KB |
Output is correct |
19 |
Correct |
8 ms |
50540 KB |
Output is correct |
20 |
Correct |
9 ms |
50524 KB |
Output is correct |
21 |
Correct |
9 ms |
50524 KB |
Output is correct |
22 |
Correct |
12 ms |
50524 KB |
Output is correct |
23 |
Correct |
8 ms |
50524 KB |
Output is correct |
24 |
Correct |
8 ms |
50524 KB |
Output is correct |
25 |
Correct |
8 ms |
50524 KB |
Output is correct |
26 |
Correct |
8 ms |
50524 KB |
Output is correct |
27 |
Correct |
8 ms |
50316 KB |
Output is correct |
28 |
Correct |
8 ms |
50524 KB |
Output is correct |
29 |
Correct |
9 ms |
50780 KB |
Output is correct |
30 |
Correct |
8 ms |
50524 KB |
Output is correct |
31 |
Correct |
8 ms |
50548 KB |
Output is correct |
32 |
Correct |
9 ms |
50524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50524 KB |
Output is correct |
2 |
Correct |
707 ms |
68220 KB |
Output is correct |
3 |
Correct |
653 ms |
67468 KB |
Output is correct |
4 |
Correct |
738 ms |
68272 KB |
Output is correct |
5 |
Correct |
798 ms |
71172 KB |
Output is correct |
6 |
Correct |
749 ms |
69380 KB |
Output is correct |
7 |
Correct |
698 ms |
67636 KB |
Output is correct |
8 |
Correct |
725 ms |
68236 KB |
Output is correct |
9 |
Correct |
727 ms |
68276 KB |
Output is correct |
10 |
Correct |
677 ms |
67640 KB |
Output is correct |
11 |
Correct |
674 ms |
67716 KB |
Output is correct |
12 |
Correct |
609 ms |
67552 KB |
Output is correct |
13 |
Correct |
652 ms |
71084 KB |
Output is correct |
14 |
Correct |
691 ms |
67228 KB |
Output is correct |
15 |
Correct |
619 ms |
71428 KB |
Output is correct |
16 |
Correct |
643 ms |
68044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50524 KB |
Output is correct |
2 |
Correct |
676 ms |
68560 KB |
Output is correct |
3 |
Correct |
647 ms |
68132 KB |
Output is correct |
4 |
Correct |
621 ms |
70076 KB |
Output is correct |
5 |
Correct |
667 ms |
68052 KB |
Output is correct |
6 |
Correct |
617 ms |
68288 KB |
Output is correct |
7 |
Correct |
663 ms |
69048 KB |
Output is correct |
8 |
Correct |
694 ms |
68308 KB |
Output is correct |
9 |
Correct |
627 ms |
68284 KB |
Output is correct |
10 |
Correct |
661 ms |
67968 KB |
Output is correct |
11 |
Correct |
691 ms |
69308 KB |
Output is correct |
12 |
Correct |
636 ms |
69380 KB |
Output is correct |
13 |
Correct |
699 ms |
69560 KB |
Output is correct |
14 |
Correct |
635 ms |
68128 KB |
Output is correct |
15 |
Correct |
691 ms |
68532 KB |
Output is correct |
16 |
Correct |
659 ms |
68020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
550 ms |
63608 KB |
Output is correct |
2 |
Correct |
540 ms |
63508 KB |
Output is correct |
3 |
Correct |
528 ms |
63752 KB |
Output is correct |
4 |
Correct |
519 ms |
63764 KB |
Output is correct |
5 |
Correct |
527 ms |
63752 KB |
Output is correct |
6 |
Correct |
564 ms |
63504 KB |
Output is correct |
7 |
Correct |
542 ms |
63620 KB |
Output is correct |
8 |
Correct |
523 ms |
63832 KB |
Output is correct |
9 |
Correct |
546 ms |
63724 KB |
Output is correct |
10 |
Correct |
511 ms |
63576 KB |
Output is correct |
11 |
Correct |
569 ms |
64008 KB |
Output is correct |
12 |
Correct |
533 ms |
63888 KB |
Output is correct |
13 |
Correct |
592 ms |
63876 KB |
Output is correct |
14 |
Correct |
543 ms |
63876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50524 KB |
Output is correct |
2 |
Correct |
9 ms |
50400 KB |
Output is correct |
3 |
Correct |
8 ms |
50352 KB |
Output is correct |
4 |
Correct |
9 ms |
50524 KB |
Output is correct |
5 |
Correct |
9 ms |
50524 KB |
Output is correct |
6 |
Correct |
9 ms |
50540 KB |
Output is correct |
7 |
Correct |
9 ms |
50524 KB |
Output is correct |
8 |
Correct |
9 ms |
50540 KB |
Output is correct |
9 |
Correct |
9 ms |
50524 KB |
Output is correct |
10 |
Correct |
9 ms |
50524 KB |
Output is correct |
11 |
Correct |
9 ms |
50524 KB |
Output is correct |
12 |
Correct |
9 ms |
50780 KB |
Output is correct |
13 |
Correct |
8 ms |
50524 KB |
Output is correct |
14 |
Correct |
10 ms |
50500 KB |
Output is correct |
15 |
Correct |
9 ms |
50524 KB |
Output is correct |
16 |
Correct |
8 ms |
50524 KB |
Output is correct |
17 |
Correct |
11 ms |
50524 KB |
Output is correct |
18 |
Correct |
8 ms |
50448 KB |
Output is correct |
19 |
Correct |
8 ms |
50540 KB |
Output is correct |
20 |
Correct |
9 ms |
50524 KB |
Output is correct |
21 |
Correct |
9 ms |
50524 KB |
Output is correct |
22 |
Correct |
12 ms |
50524 KB |
Output is correct |
23 |
Correct |
8 ms |
50524 KB |
Output is correct |
24 |
Correct |
8 ms |
50524 KB |
Output is correct |
25 |
Correct |
8 ms |
50524 KB |
Output is correct |
26 |
Correct |
8 ms |
50524 KB |
Output is correct |
27 |
Correct |
8 ms |
50316 KB |
Output is correct |
28 |
Correct |
8 ms |
50524 KB |
Output is correct |
29 |
Correct |
9 ms |
50780 KB |
Output is correct |
30 |
Correct |
8 ms |
50524 KB |
Output is correct |
31 |
Correct |
8 ms |
50548 KB |
Output is correct |
32 |
Correct |
9 ms |
50524 KB |
Output is correct |
33 |
Correct |
707 ms |
68220 KB |
Output is correct |
34 |
Correct |
653 ms |
67468 KB |
Output is correct |
35 |
Correct |
738 ms |
68272 KB |
Output is correct |
36 |
Correct |
798 ms |
71172 KB |
Output is correct |
37 |
Correct |
749 ms |
69380 KB |
Output is correct |
38 |
Correct |
698 ms |
67636 KB |
Output is correct |
39 |
Correct |
725 ms |
68236 KB |
Output is correct |
40 |
Correct |
727 ms |
68276 KB |
Output is correct |
41 |
Correct |
677 ms |
67640 KB |
Output is correct |
42 |
Correct |
674 ms |
67716 KB |
Output is correct |
43 |
Correct |
609 ms |
67552 KB |
Output is correct |
44 |
Correct |
652 ms |
71084 KB |
Output is correct |
45 |
Correct |
691 ms |
67228 KB |
Output is correct |
46 |
Correct |
619 ms |
71428 KB |
Output is correct |
47 |
Correct |
643 ms |
68044 KB |
Output is correct |
48 |
Correct |
676 ms |
68560 KB |
Output is correct |
49 |
Correct |
647 ms |
68132 KB |
Output is correct |
50 |
Correct |
621 ms |
70076 KB |
Output is correct |
51 |
Correct |
667 ms |
68052 KB |
Output is correct |
52 |
Correct |
617 ms |
68288 KB |
Output is correct |
53 |
Correct |
663 ms |
69048 KB |
Output is correct |
54 |
Correct |
694 ms |
68308 KB |
Output is correct |
55 |
Correct |
627 ms |
68284 KB |
Output is correct |
56 |
Correct |
661 ms |
67968 KB |
Output is correct |
57 |
Correct |
691 ms |
69308 KB |
Output is correct |
58 |
Correct |
636 ms |
69380 KB |
Output is correct |
59 |
Correct |
699 ms |
69560 KB |
Output is correct |
60 |
Correct |
635 ms |
68128 KB |
Output is correct |
61 |
Correct |
691 ms |
68532 KB |
Output is correct |
62 |
Correct |
659 ms |
68020 KB |
Output is correct |
63 |
Correct |
550 ms |
63608 KB |
Output is correct |
64 |
Correct |
540 ms |
63508 KB |
Output is correct |
65 |
Correct |
528 ms |
63752 KB |
Output is correct |
66 |
Correct |
519 ms |
63764 KB |
Output is correct |
67 |
Correct |
527 ms |
63752 KB |
Output is correct |
68 |
Correct |
564 ms |
63504 KB |
Output is correct |
69 |
Correct |
542 ms |
63620 KB |
Output is correct |
70 |
Correct |
523 ms |
63832 KB |
Output is correct |
71 |
Correct |
546 ms |
63724 KB |
Output is correct |
72 |
Correct |
511 ms |
63576 KB |
Output is correct |
73 |
Correct |
569 ms |
64008 KB |
Output is correct |
74 |
Correct |
533 ms |
63888 KB |
Output is correct |
75 |
Correct |
592 ms |
63876 KB |
Output is correct |
76 |
Correct |
543 ms |
63876 KB |
Output is correct |
77 |
Correct |
655 ms |
69312 KB |
Output is correct |
78 |
Correct |
690 ms |
69640 KB |
Output is correct |
79 |
Correct |
669 ms |
69300 KB |
Output is correct |
80 |
Correct |
728 ms |
69232 KB |
Output is correct |
81 |
Correct |
680 ms |
69100 KB |
Output is correct |
82 |
Correct |
723 ms |
69248 KB |
Output is correct |
83 |
Correct |
655 ms |
69384 KB |
Output is correct |
84 |
Correct |
636 ms |
69384 KB |
Output is correct |
85 |
Correct |
722 ms |
69244 KB |
Output is correct |
86 |
Correct |
649 ms |
69388 KB |
Output is correct |
87 |
Correct |
674 ms |
69284 KB |
Output is correct |
88 |
Correct |
690 ms |
69048 KB |
Output is correct |
89 |
Correct |
689 ms |
68684 KB |
Output is correct |
90 |
Correct |
704 ms |
70404 KB |
Output is correct |
91 |
Correct |
649 ms |
68872 KB |
Output is correct |
92 |
Correct |
770 ms |
68740 KB |
Output is correct |
93 |
Correct |
673 ms |
68696 KB |
Output is correct |
94 |
Correct |
651 ms |
70044 KB |
Output is correct |
95 |
Correct |
636 ms |
70528 KB |
Output is correct |
96 |
Correct |
694 ms |
69004 KB |
Output is correct |
97 |
Correct |
709 ms |
69128 KB |
Output is correct |
98 |
Correct |
772 ms |
68288 KB |
Output is correct |
99 |
Correct |
689 ms |
68592 KB |
Output is correct |
100 |
Correct |
765 ms |
68532 KB |
Output is correct |
101 |
Correct |
705 ms |
68616 KB |
Output is correct |
102 |
Correct |
727 ms |
70060 KB |
Output is correct |