#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
#define debug(x...) cout << '[' << #x << "] : ", dout(x)
void dout() { cout << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i,j,k) for (int i = j, Z = k; i <= Z; i++)
typedef pair<int, int> pii;
typedef pair<pii, pii> p4i;
const int MXN = 1000005;
int n, q, a[MXN], l[MXN], r[MXN], ans[MXN];
int ql[MXN], qr[MXN], qt[MXN];
vector<p4i> op;
struct BIT {
int n;
int val[MXN];
void init(int _n) {
n = _n;
fill(val + 1, val + n + 1, 0);
}
void modify(int id, int v) {
for (; id <= n; id += (id & -id)) val[id] += v;
}
int query(int id) {
int ans = 0;
for (; id > 0; id -= (id & -id)) ans += val[id];
return ans;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
};
struct BBIT {
BIT A, B;
void init(int _n) {
A.init(_n);
B.init(_n);
}
void modify(int id, int v) {
A.modify(id, v);
B.modify(id, v * id);
}
int query(int id) {
return (id + 1) * A.query(id) - B.query(id);
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
};
struct HBBIT {
BBIT A;
int sr;
void init(int _n) {
A.init(_n * 2);
sr = _n;
}
void move() {
sr--;
}
void modify(int id, int v) {
A.modify(sr + id, v);
}
int query(int id) {
return A.query(sr + id);
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
};
struct DS {
BBIT S;
HBBIT T;
void init(int _n) {
S.init(_n);
T.init(_n);
}
void move() {
T.move();
}
void modify(int id, int v) {
S.modify(id, v);
T.modify(id + 1, -v);
}
int query(int id) {
return S.query(id) + T.query(id);
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
} B;
void FIND_L() {
stack<pii> st;
st.push(mp(LLONG_MAX, -1LL));
for (int i = 1; i <= n; i++) {
while (st.top().fs <= a[i]) st.pop();
l[i] = st.top().sc;
st.push(mp(a[i], i));
}
}
void FIND_R() {
stack<pii> st;
st.push(mp(LLONG_MAX, -1LL));
for (int i = n; i > 0; i--) {
while (st.top().fs < a[i]) st.pop();
r[i] = st.top().sc;
st.push(mp(a[i], i));
}
}
void BOX(int id) {
op.push_back(mp(mp(0LL, 2LL), mp(id, a[id])));
if (l[id] != -1) op.push_back(mp(mp(id - l[id], 2LL), mp(id, -a[id])));
if (r[id] != -1) op.push_back(mp(mp(r[id] - id, 2LL), mp(r[id], -a[id])));
if (l[id] != -1 && r[id] != -1) op.push_back(mp(mp(r[id] - l[id], 2LL), mp(r[id], a[id])));
}
void BOX() {
FOR(i, 1, n) op.push_back(mp(mp(i, 1LL), mp(0LL, 0LL)));
FOR(i, 1, n) BOX(i);
FOR(i, 1, q) op.push_back(mp(mp(qt[i], 3LL), mp(i, 0LL)));
// op.push_back(mp(mp(0LL, 2LL), mp(3LL, 2LL)));
// op.push_back(mp(mp(3LL, 2LL), mp(6LL, -2LL)));
sort(op.begin(), op.end());
}
void miku() {
cin >> n >> q;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, q) cin >> qt[i] >> ql[i] >> qr[i];
FIND_L();
FIND_R();
BOX();
B.init(n);
for (auto &i : op) {
if (i.fs.sc == 1) {
// FOR(i, 1, n) cout << B.query(i, i) << ' ';
// cout << endl;
// debug("MOVE");
B.move();
} else if (i.fs.sc == 2) {
// debug("MODIFY");
B.modify(i.sc.fs, i.sc.sc);
} else {
// debug("QUERY");
int id = i.sc.fs;
ans[id] = B.query(ql[id], qr[id]);
}
}
FOR(i, 1, q) {
cout << ans[i] << '\n';
}
}
void RESET() {
FOR(i, 1, n) {
a[i] = 0;
l[i] = 0;
r[i] = 0;
}
FOR(i, 1, q) {
ans[i] = 0;
ql[i] = 0;
qr[i] = 0;
qt[i] = 0;
}
op.clear();
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--) {
miku();
// RESET();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21084 KB |
Output is correct |
8 |
Correct |
3 ms |
21080 KB |
Output is correct |
9 |
Correct |
3 ms |
21080 KB |
Output is correct |
10 |
Correct |
2 ms |
21080 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
20948 KB |
Output is correct |
13 |
Correct |
3 ms |
21080 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21084 KB |
Output is correct |
16 |
Correct |
3 ms |
21080 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
3 ms |
21084 KB |
Output is correct |
23 |
Correct |
3 ms |
21084 KB |
Output is correct |
24 |
Correct |
3 ms |
20948 KB |
Output is correct |
25 |
Correct |
3 ms |
21084 KB |
Output is correct |
26 |
Correct |
3 ms |
21084 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
3 ms |
21084 KB |
Output is correct |
29 |
Correct |
3 ms |
21084 KB |
Output is correct |
30 |
Correct |
3 ms |
21084 KB |
Output is correct |
31 |
Correct |
3 ms |
21084 KB |
Output is correct |
32 |
Correct |
3 ms |
21080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20824 KB |
Output is correct |
2 |
Correct |
240 ms |
86096 KB |
Output is correct |
3 |
Correct |
233 ms |
86188 KB |
Output is correct |
4 |
Correct |
250 ms |
91708 KB |
Output is correct |
5 |
Correct |
243 ms |
93996 KB |
Output is correct |
6 |
Correct |
251 ms |
91060 KB |
Output is correct |
7 |
Correct |
248 ms |
92240 KB |
Output is correct |
8 |
Correct |
245 ms |
94136 KB |
Output is correct |
9 |
Correct |
247 ms |
91832 KB |
Output is correct |
10 |
Correct |
236 ms |
92080 KB |
Output is correct |
11 |
Correct |
260 ms |
91688 KB |
Output is correct |
12 |
Correct |
232 ms |
90296 KB |
Output is correct |
13 |
Correct |
249 ms |
93104 KB |
Output is correct |
14 |
Correct |
242 ms |
93440 KB |
Output is correct |
15 |
Correct |
245 ms |
93612 KB |
Output is correct |
16 |
Correct |
240 ms |
91568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20824 KB |
Output is correct |
2 |
Correct |
255 ms |
86208 KB |
Output is correct |
3 |
Correct |
255 ms |
91328 KB |
Output is correct |
4 |
Correct |
273 ms |
93100 KB |
Output is correct |
5 |
Correct |
255 ms |
90764 KB |
Output is correct |
6 |
Correct |
254 ms |
91312 KB |
Output is correct |
7 |
Correct |
256 ms |
90908 KB |
Output is correct |
8 |
Correct |
262 ms |
92236 KB |
Output is correct |
9 |
Correct |
252 ms |
92084 KB |
Output is correct |
10 |
Correct |
255 ms |
91940 KB |
Output is correct |
11 |
Correct |
264 ms |
94568 KB |
Output is correct |
12 |
Correct |
263 ms |
93608 KB |
Output is correct |
13 |
Correct |
269 ms |
90828 KB |
Output is correct |
14 |
Correct |
254 ms |
91832 KB |
Output is correct |
15 |
Correct |
262 ms |
92348 KB |
Output is correct |
16 |
Correct |
259 ms |
94136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
72884 KB |
Output is correct |
2 |
Correct |
207 ms |
77036 KB |
Output is correct |
3 |
Correct |
213 ms |
78024 KB |
Output is correct |
4 |
Correct |
212 ms |
75020 KB |
Output is correct |
5 |
Correct |
214 ms |
76800 KB |
Output is correct |
6 |
Correct |
208 ms |
76116 KB |
Output is correct |
7 |
Correct |
214 ms |
78284 KB |
Output is correct |
8 |
Correct |
212 ms |
76956 KB |
Output is correct |
9 |
Correct |
214 ms |
75904 KB |
Output is correct |
10 |
Correct |
207 ms |
75732 KB |
Output is correct |
11 |
Correct |
213 ms |
75764 KB |
Output is correct |
12 |
Correct |
213 ms |
75496 KB |
Output is correct |
13 |
Correct |
215 ms |
77164 KB |
Output is correct |
14 |
Correct |
210 ms |
76580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21084 KB |
Output is correct |
8 |
Correct |
3 ms |
21080 KB |
Output is correct |
9 |
Correct |
3 ms |
21080 KB |
Output is correct |
10 |
Correct |
2 ms |
21080 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
20948 KB |
Output is correct |
13 |
Correct |
3 ms |
21080 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21084 KB |
Output is correct |
16 |
Correct |
3 ms |
21080 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
3 ms |
21084 KB |
Output is correct |
23 |
Correct |
3 ms |
21084 KB |
Output is correct |
24 |
Correct |
3 ms |
20948 KB |
Output is correct |
25 |
Correct |
3 ms |
21084 KB |
Output is correct |
26 |
Correct |
3 ms |
21084 KB |
Output is correct |
27 |
Correct |
3 ms |
21084 KB |
Output is correct |
28 |
Correct |
3 ms |
21084 KB |
Output is correct |
29 |
Correct |
3 ms |
21084 KB |
Output is correct |
30 |
Correct |
3 ms |
21084 KB |
Output is correct |
31 |
Correct |
3 ms |
21084 KB |
Output is correct |
32 |
Correct |
3 ms |
21080 KB |
Output is correct |
33 |
Correct |
240 ms |
86096 KB |
Output is correct |
34 |
Correct |
233 ms |
86188 KB |
Output is correct |
35 |
Correct |
250 ms |
91708 KB |
Output is correct |
36 |
Correct |
243 ms |
93996 KB |
Output is correct |
37 |
Correct |
251 ms |
91060 KB |
Output is correct |
38 |
Correct |
248 ms |
92240 KB |
Output is correct |
39 |
Correct |
245 ms |
94136 KB |
Output is correct |
40 |
Correct |
247 ms |
91832 KB |
Output is correct |
41 |
Correct |
236 ms |
92080 KB |
Output is correct |
42 |
Correct |
260 ms |
91688 KB |
Output is correct |
43 |
Correct |
232 ms |
90296 KB |
Output is correct |
44 |
Correct |
249 ms |
93104 KB |
Output is correct |
45 |
Correct |
242 ms |
93440 KB |
Output is correct |
46 |
Correct |
245 ms |
93612 KB |
Output is correct |
47 |
Correct |
240 ms |
91568 KB |
Output is correct |
48 |
Correct |
255 ms |
86208 KB |
Output is correct |
49 |
Correct |
255 ms |
91328 KB |
Output is correct |
50 |
Correct |
273 ms |
93100 KB |
Output is correct |
51 |
Correct |
255 ms |
90764 KB |
Output is correct |
52 |
Correct |
254 ms |
91312 KB |
Output is correct |
53 |
Correct |
256 ms |
90908 KB |
Output is correct |
54 |
Correct |
262 ms |
92236 KB |
Output is correct |
55 |
Correct |
252 ms |
92084 KB |
Output is correct |
56 |
Correct |
255 ms |
91940 KB |
Output is correct |
57 |
Correct |
264 ms |
94568 KB |
Output is correct |
58 |
Correct |
263 ms |
93608 KB |
Output is correct |
59 |
Correct |
269 ms |
90828 KB |
Output is correct |
60 |
Correct |
254 ms |
91832 KB |
Output is correct |
61 |
Correct |
262 ms |
92348 KB |
Output is correct |
62 |
Correct |
259 ms |
94136 KB |
Output is correct |
63 |
Correct |
218 ms |
72884 KB |
Output is correct |
64 |
Correct |
207 ms |
77036 KB |
Output is correct |
65 |
Correct |
213 ms |
78024 KB |
Output is correct |
66 |
Correct |
212 ms |
75020 KB |
Output is correct |
67 |
Correct |
214 ms |
76800 KB |
Output is correct |
68 |
Correct |
208 ms |
76116 KB |
Output is correct |
69 |
Correct |
214 ms |
78284 KB |
Output is correct |
70 |
Correct |
212 ms |
76956 KB |
Output is correct |
71 |
Correct |
214 ms |
75904 KB |
Output is correct |
72 |
Correct |
207 ms |
75732 KB |
Output is correct |
73 |
Correct |
213 ms |
75764 KB |
Output is correct |
74 |
Correct |
213 ms |
75496 KB |
Output is correct |
75 |
Correct |
215 ms |
77164 KB |
Output is correct |
76 |
Correct |
210 ms |
76580 KB |
Output is correct |
77 |
Correct |
262 ms |
92336 KB |
Output is correct |
78 |
Correct |
274 ms |
92344 KB |
Output is correct |
79 |
Correct |
264 ms |
93876 KB |
Output is correct |
80 |
Correct |
264 ms |
91824 KB |
Output is correct |
81 |
Correct |
258 ms |
90424 KB |
Output is correct |
82 |
Correct |
265 ms |
91824 KB |
Output is correct |
83 |
Correct |
272 ms |
90556 KB |
Output is correct |
84 |
Correct |
260 ms |
90808 KB |
Output is correct |
85 |
Correct |
263 ms |
92596 KB |
Output is correct |
86 |
Correct |
263 ms |
91324 KB |
Output is correct |
87 |
Correct |
262 ms |
92600 KB |
Output is correct |
88 |
Correct |
255 ms |
93252 KB |
Output is correct |
89 |
Correct |
250 ms |
92352 KB |
Output is correct |
90 |
Correct |
260 ms |
93240 KB |
Output is correct |
91 |
Correct |
254 ms |
91304 KB |
Output is correct |
92 |
Correct |
251 ms |
90812 KB |
Output is correct |
93 |
Correct |
254 ms |
92324 KB |
Output is correct |
94 |
Correct |
260 ms |
93868 KB |
Output is correct |
95 |
Correct |
255 ms |
93088 KB |
Output is correct |
96 |
Correct |
254 ms |
91812 KB |
Output is correct |
97 |
Correct |
255 ms |
91588 KB |
Output is correct |
98 |
Correct |
258 ms |
91828 KB |
Output is correct |
99 |
Correct |
261 ms |
90904 KB |
Output is correct |
100 |
Correct |
266 ms |
90044 KB |
Output is correct |
101 |
Correct |
258 ms |
90404 KB |
Output is correct |
102 |
Correct |
262 ms |
94256 KB |
Output is correct |