#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, q;
int a[N], lt[N];
long long res[N];
vector<array<int, 3>> events[N], Queries[N];
struct Fenwick {
long long s[2 * N];
void upd(int i, long long x) {
for (; i <= 2 * n; i += i & -i) {
s[i] += x;
}
}
long long qry(int i) {
long long res = 0;
for (; i; i -= i & -i) {
res += s[i];
}
return res;
}
} ft[4];
void upd(int l, int r, int x) {
ft[0].upd(l, x);
ft[1].upd(r + 1, -x);
ft[2].upd(l, (long long) (1 - l) * x);
ft[3].upd(r + 1, (long long) r * x);
}
long long qry(int i, int t) {
i += n;
auto A = ft[0].qry(i - t), B = ft[1].qry(i), C = ft[2].qry(i - t), D = ft[3].qry(i);
return (A + B) * (i - t) + C + D + B * t;
}
void add(int l, int r, int h, int x) {
l += n, r += n;
events[0].push_back({l, r, x});
if (h <= n) {
events[h].push_back({l, r, -x});
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= q; ++i) {
int l, r, t; cin >> t >> l >> r;
Queries[t].push_back({l, r, i});
}
vector<int> st = {-n};
for (int i = 1; i <= n; ++i) {
while (st.size() > 1 && a[st.back()] <= a[i]) {
st.pop_back();
}
lt[i] = st.back();
st.push_back(i);
}
st.clear();
st.push_back(n + 1);
for (int i = n; i > 0; --i) {
while (st.size() > 1 && a[st.back()] < a[i]) {
st.pop_back();
}
int r = st.back();
if (lt[i] + 1 < i) {
add(lt[i] + 1, i - 1, i - lt[i] - 1, -a[i]);
}
if (i < r - 1) {
add(i + 1, r - 1, r - i - 1, -a[i]);
}
add(lt[i] + 1, r - 1, r - lt[i] - 1, a[i]);
st.push_back(i);
}
for (int t = 0; t <= n; ++t) {
for (auto [l, r, x] : events[t]) {
upd(l, r, x);
}
for (auto [l, r, id] : Queries[t]) {
res[id] = qry(r, t) - qry(l - 1, t);
}
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18776 KB |
Output is correct |
2 |
Correct |
2 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18964 KB |
Output is correct |
4 |
Correct |
2 ms |
18928 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18912 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
2 ms |
18956 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18960 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
2 ms |
18964 KB |
Output is correct |
17 |
Correct |
3 ms |
18916 KB |
Output is correct |
18 |
Correct |
2 ms |
18780 KB |
Output is correct |
19 |
Correct |
2 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
2 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18776 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
2 ms |
18872 KB |
Output is correct |
25 |
Correct |
2 ms |
18780 KB |
Output is correct |
26 |
Correct |
3 ms |
18780 KB |
Output is correct |
27 |
Correct |
2 ms |
18864 KB |
Output is correct |
28 |
Correct |
2 ms |
18780 KB |
Output is correct |
29 |
Correct |
2 ms |
18780 KB |
Output is correct |
30 |
Correct |
2 ms |
18780 KB |
Output is correct |
31 |
Correct |
2 ms |
18780 KB |
Output is correct |
32 |
Correct |
2 ms |
18776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18776 KB |
Output is correct |
2 |
Correct |
115 ms |
42012 KB |
Output is correct |
3 |
Correct |
111 ms |
42424 KB |
Output is correct |
4 |
Correct |
110 ms |
42232 KB |
Output is correct |
5 |
Correct |
113 ms |
41656 KB |
Output is correct |
6 |
Correct |
105 ms |
40372 KB |
Output is correct |
7 |
Correct |
113 ms |
41008 KB |
Output is correct |
8 |
Correct |
112 ms |
44940 KB |
Output is correct |
9 |
Correct |
117 ms |
41940 KB |
Output is correct |
10 |
Correct |
112 ms |
41768 KB |
Output is correct |
11 |
Correct |
121 ms |
43516 KB |
Output is correct |
12 |
Correct |
129 ms |
40632 KB |
Output is correct |
13 |
Correct |
113 ms |
42768 KB |
Output is correct |
14 |
Correct |
121 ms |
42824 KB |
Output is correct |
15 |
Correct |
113 ms |
42436 KB |
Output is correct |
16 |
Correct |
129 ms |
40672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18776 KB |
Output is correct |
2 |
Correct |
143 ms |
41716 KB |
Output is correct |
3 |
Correct |
129 ms |
41856 KB |
Output is correct |
4 |
Correct |
133 ms |
44024 KB |
Output is correct |
5 |
Correct |
127 ms |
42232 KB |
Output is correct |
6 |
Correct |
138 ms |
42916 KB |
Output is correct |
7 |
Correct |
121 ms |
43116 KB |
Output is correct |
8 |
Correct |
126 ms |
42952 KB |
Output is correct |
9 |
Correct |
123 ms |
41744 KB |
Output is correct |
10 |
Correct |
120 ms |
40952 KB |
Output is correct |
11 |
Correct |
133 ms |
44272 KB |
Output is correct |
12 |
Correct |
128 ms |
44936 KB |
Output is correct |
13 |
Correct |
122 ms |
45044 KB |
Output is correct |
14 |
Correct |
125 ms |
42740 KB |
Output is correct |
15 |
Correct |
128 ms |
45280 KB |
Output is correct |
16 |
Correct |
128 ms |
44576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
39164 KB |
Output is correct |
2 |
Correct |
120 ms |
38584 KB |
Output is correct |
3 |
Correct |
114 ms |
36856 KB |
Output is correct |
4 |
Correct |
136 ms |
35828 KB |
Output is correct |
5 |
Correct |
119 ms |
37792 KB |
Output is correct |
6 |
Correct |
119 ms |
39672 KB |
Output is correct |
7 |
Correct |
130 ms |
39424 KB |
Output is correct |
8 |
Correct |
115 ms |
39924 KB |
Output is correct |
9 |
Correct |
120 ms |
39672 KB |
Output is correct |
10 |
Correct |
119 ms |
37236 KB |
Output is correct |
11 |
Correct |
134 ms |
38132 KB |
Output is correct |
12 |
Correct |
135 ms |
36120 KB |
Output is correct |
13 |
Correct |
120 ms |
37584 KB |
Output is correct |
14 |
Correct |
127 ms |
38128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18776 KB |
Output is correct |
2 |
Correct |
2 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18964 KB |
Output is correct |
4 |
Correct |
2 ms |
18928 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18912 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
2 ms |
18956 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18960 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
2 ms |
18964 KB |
Output is correct |
17 |
Correct |
3 ms |
18916 KB |
Output is correct |
18 |
Correct |
2 ms |
18780 KB |
Output is correct |
19 |
Correct |
2 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
2 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18776 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
2 ms |
18872 KB |
Output is correct |
25 |
Correct |
2 ms |
18780 KB |
Output is correct |
26 |
Correct |
3 ms |
18780 KB |
Output is correct |
27 |
Correct |
2 ms |
18864 KB |
Output is correct |
28 |
Correct |
2 ms |
18780 KB |
Output is correct |
29 |
Correct |
2 ms |
18780 KB |
Output is correct |
30 |
Correct |
2 ms |
18780 KB |
Output is correct |
31 |
Correct |
2 ms |
18780 KB |
Output is correct |
32 |
Correct |
2 ms |
18776 KB |
Output is correct |
33 |
Correct |
115 ms |
42012 KB |
Output is correct |
34 |
Correct |
111 ms |
42424 KB |
Output is correct |
35 |
Correct |
110 ms |
42232 KB |
Output is correct |
36 |
Correct |
113 ms |
41656 KB |
Output is correct |
37 |
Correct |
105 ms |
40372 KB |
Output is correct |
38 |
Correct |
113 ms |
41008 KB |
Output is correct |
39 |
Correct |
112 ms |
44940 KB |
Output is correct |
40 |
Correct |
117 ms |
41940 KB |
Output is correct |
41 |
Correct |
112 ms |
41768 KB |
Output is correct |
42 |
Correct |
121 ms |
43516 KB |
Output is correct |
43 |
Correct |
129 ms |
40632 KB |
Output is correct |
44 |
Correct |
113 ms |
42768 KB |
Output is correct |
45 |
Correct |
121 ms |
42824 KB |
Output is correct |
46 |
Correct |
113 ms |
42436 KB |
Output is correct |
47 |
Correct |
129 ms |
40672 KB |
Output is correct |
48 |
Correct |
143 ms |
41716 KB |
Output is correct |
49 |
Correct |
129 ms |
41856 KB |
Output is correct |
50 |
Correct |
133 ms |
44024 KB |
Output is correct |
51 |
Correct |
127 ms |
42232 KB |
Output is correct |
52 |
Correct |
138 ms |
42916 KB |
Output is correct |
53 |
Correct |
121 ms |
43116 KB |
Output is correct |
54 |
Correct |
126 ms |
42952 KB |
Output is correct |
55 |
Correct |
123 ms |
41744 KB |
Output is correct |
56 |
Correct |
120 ms |
40952 KB |
Output is correct |
57 |
Correct |
133 ms |
44272 KB |
Output is correct |
58 |
Correct |
128 ms |
44936 KB |
Output is correct |
59 |
Correct |
122 ms |
45044 KB |
Output is correct |
60 |
Correct |
125 ms |
42740 KB |
Output is correct |
61 |
Correct |
128 ms |
45280 KB |
Output is correct |
62 |
Correct |
128 ms |
44576 KB |
Output is correct |
63 |
Correct |
115 ms |
39164 KB |
Output is correct |
64 |
Correct |
120 ms |
38584 KB |
Output is correct |
65 |
Correct |
114 ms |
36856 KB |
Output is correct |
66 |
Correct |
136 ms |
35828 KB |
Output is correct |
67 |
Correct |
119 ms |
37792 KB |
Output is correct |
68 |
Correct |
119 ms |
39672 KB |
Output is correct |
69 |
Correct |
130 ms |
39424 KB |
Output is correct |
70 |
Correct |
115 ms |
39924 KB |
Output is correct |
71 |
Correct |
120 ms |
39672 KB |
Output is correct |
72 |
Correct |
119 ms |
37236 KB |
Output is correct |
73 |
Correct |
134 ms |
38132 KB |
Output is correct |
74 |
Correct |
135 ms |
36120 KB |
Output is correct |
75 |
Correct |
120 ms |
37584 KB |
Output is correct |
76 |
Correct |
127 ms |
38128 KB |
Output is correct |
77 |
Correct |
133 ms |
43504 KB |
Output is correct |
78 |
Correct |
135 ms |
44124 KB |
Output is correct |
79 |
Correct |
138 ms |
44996 KB |
Output is correct |
80 |
Correct |
136 ms |
42772 KB |
Output is correct |
81 |
Correct |
136 ms |
42488 KB |
Output is correct |
82 |
Correct |
130 ms |
43512 KB |
Output is correct |
83 |
Correct |
133 ms |
43000 KB |
Output is correct |
84 |
Correct |
142 ms |
43744 KB |
Output is correct |
85 |
Correct |
138 ms |
45380 KB |
Output is correct |
86 |
Correct |
151 ms |
39448 KB |
Output is correct |
87 |
Correct |
136 ms |
41884 KB |
Output is correct |
88 |
Correct |
125 ms |
40944 KB |
Output is correct |
89 |
Correct |
126 ms |
38896 KB |
Output is correct |
90 |
Correct |
129 ms |
44816 KB |
Output is correct |
91 |
Correct |
117 ms |
41716 KB |
Output is correct |
92 |
Correct |
139 ms |
42608 KB |
Output is correct |
93 |
Correct |
132 ms |
42492 KB |
Output is correct |
94 |
Correct |
131 ms |
43816 KB |
Output is correct |
95 |
Correct |
135 ms |
40736 KB |
Output is correct |
96 |
Correct |
129 ms |
39560 KB |
Output is correct |
97 |
Correct |
124 ms |
42236 KB |
Output is correct |
98 |
Correct |
135 ms |
41740 KB |
Output is correct |
99 |
Correct |
128 ms |
42960 KB |
Output is correct |
100 |
Correct |
126 ms |
44536 KB |
Output is correct |
101 |
Correct |
115 ms |
41848 KB |
Output is correct |
102 |
Correct |
130 ms |
45040 KB |
Output is correct |