#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 200005;
struct RURQ{
long long tree1[400014];
long long tree2[400014];
inline void update(long long l, long long r, long long v){
l += N, r += N;
int L = l, R = r;
for(r++;r <= 2*N;r += (r & -r)){
tree1[r] -= v;
tree2[r] -= R*v;
}
for(;l <= 2*N;l += (l & -l)){
tree1[l] += v;
tree2[l] += (L-1)*v;
}
}
inline long long query(long long r){
long long R = r;
long long ans = 0;
for(;r;r -= (r & -r)){
ans += R * tree1[r];
ans -= tree2[r];
}
return ans;
}
inline long long query(int l, int r){
l += N, r += N;
return query(r) - query(l-1);
}
} vert, diag;
struct QU{ int l, r; long long v; }; //Query or Update
vector<QU> queries[200005];
vector<QU> updates[200005];
int arr[200005];
long long ans[200005];
int L[200005];
int R[200005];
void tri(int l, int r, long long v){
if(r < l) return;
diag.update(l, N, v);
vert.update(r+1, N, -v);
if(r-l+1 < N) updates[r-l+1].push_back({l, r, v});
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, Q; cin >> n >> Q;
for(int i = 1;i <= n;i++) cin >> arr[i];
for(int i = 1;i <= Q;i++){
int t, l, r; cin >> t >> l >> r;
queries[t].push_back({l,r,i});
}
stack<int> S;
for(int i = 1;i <= n;i++){
while(!S.empty() && arr[S.top()] < arr[i]) S.pop();
if(S.empty()) L[i] = -N+1;
else L[i] = S.top();
S.push(i);
}
while(!S.empty()) S.pop();
for(int i = n;i >= 0;i--){
while(!S.empty() && arr[S.top()] <= arr[i]) S.pop();
if(S.empty()) R[i] = N-1;
else R[i] = S.top();
S.push(i);
}
for(int i = 1;i <= n;i++){
tri(L[i]+1,R[i]-1,arr[i]);
tri(L[i]+1,i-1,-arr[i]);
tri(i+1,R[i]-1,-arr[i]);
}
for(int t = 1;t <= n;t++){
for(QU u : updates[t]){
diag.update(u.l, N, -u.v);
vert.update(u.r+1, N, u.v);
}
for(QU q : queries[t]){
ans[q.v] = diag.query(q.l - t, q.r - t) + vert.query(q.l, q.r);
}
}
for(int i = 1;i <= Q;i++) cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9984 KB |
Output is correct |
2 |
Correct |
10 ms |
9984 KB |
Output is correct |
3 |
Correct |
10 ms |
9984 KB |
Output is correct |
4 |
Correct |
9 ms |
9984 KB |
Output is correct |
5 |
Correct |
10 ms |
9984 KB |
Output is correct |
6 |
Correct |
13 ms |
9984 KB |
Output is correct |
7 |
Correct |
11 ms |
9984 KB |
Output is correct |
8 |
Correct |
10 ms |
9984 KB |
Output is correct |
9 |
Correct |
10 ms |
10112 KB |
Output is correct |
10 |
Correct |
10 ms |
9984 KB |
Output is correct |
11 |
Correct |
9 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
9 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
10 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
10 ms |
9984 KB |
Output is correct |
20 |
Correct |
10 ms |
9984 KB |
Output is correct |
21 |
Correct |
10 ms |
9984 KB |
Output is correct |
22 |
Correct |
10 ms |
9984 KB |
Output is correct |
23 |
Correct |
9 ms |
9984 KB |
Output is correct |
24 |
Correct |
10 ms |
9984 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
9 ms |
9984 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
10 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
31 |
Correct |
11 ms |
9984 KB |
Output is correct |
32 |
Correct |
10 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9984 KB |
Output is correct |
2 |
Correct |
200 ms |
32868 KB |
Output is correct |
3 |
Correct |
200 ms |
38640 KB |
Output is correct |
4 |
Correct |
206 ms |
39136 KB |
Output is correct |
5 |
Correct |
221 ms |
40928 KB |
Output is correct |
6 |
Correct |
211 ms |
38496 KB |
Output is correct |
7 |
Correct |
205 ms |
39136 KB |
Output is correct |
8 |
Correct |
222 ms |
42744 KB |
Output is correct |
9 |
Correct |
220 ms |
40544 KB |
Output is correct |
10 |
Correct |
218 ms |
38632 KB |
Output is correct |
11 |
Correct |
211 ms |
40804 KB |
Output is correct |
12 |
Correct |
214 ms |
38880 KB |
Output is correct |
13 |
Correct |
213 ms |
40800 KB |
Output is correct |
14 |
Correct |
209 ms |
40900 KB |
Output is correct |
15 |
Correct |
216 ms |
40800 KB |
Output is correct |
16 |
Correct |
214 ms |
39008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9984 KB |
Output is correct |
2 |
Correct |
279 ms |
34396 KB |
Output is correct |
3 |
Correct |
283 ms |
33832 KB |
Output is correct |
4 |
Correct |
265 ms |
35664 KB |
Output is correct |
5 |
Correct |
270 ms |
34016 KB |
Output is correct |
6 |
Correct |
246 ms |
34136 KB |
Output is correct |
7 |
Correct |
251 ms |
34128 KB |
Output is correct |
8 |
Correct |
282 ms |
34264 KB |
Output is correct |
9 |
Correct |
287 ms |
34136 KB |
Output is correct |
10 |
Correct |
261 ms |
33888 KB |
Output is correct |
11 |
Correct |
267 ms |
35684 KB |
Output is correct |
12 |
Correct |
256 ms |
35420 KB |
Output is correct |
13 |
Correct |
267 ms |
34604 KB |
Output is correct |
14 |
Correct |
248 ms |
34008 KB |
Output is correct |
15 |
Correct |
260 ms |
34508 KB |
Output is correct |
16 |
Correct |
252 ms |
34120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
33772 KB |
Output is correct |
2 |
Correct |
263 ms |
33852 KB |
Output is correct |
3 |
Correct |
257 ms |
34356 KB |
Output is correct |
4 |
Correct |
247 ms |
33728 KB |
Output is correct |
5 |
Correct |
246 ms |
33752 KB |
Output is correct |
6 |
Correct |
261 ms |
33844 KB |
Output is correct |
7 |
Correct |
247 ms |
34224 KB |
Output is correct |
8 |
Correct |
255 ms |
33976 KB |
Output is correct |
9 |
Correct |
248 ms |
33852 KB |
Output is correct |
10 |
Correct |
230 ms |
34056 KB |
Output is correct |
11 |
Correct |
270 ms |
34112 KB |
Output is correct |
12 |
Correct |
244 ms |
34104 KB |
Output is correct |
13 |
Correct |
299 ms |
33884 KB |
Output is correct |
14 |
Correct |
239 ms |
33976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9984 KB |
Output is correct |
2 |
Correct |
10 ms |
9984 KB |
Output is correct |
3 |
Correct |
10 ms |
9984 KB |
Output is correct |
4 |
Correct |
9 ms |
9984 KB |
Output is correct |
5 |
Correct |
10 ms |
9984 KB |
Output is correct |
6 |
Correct |
13 ms |
9984 KB |
Output is correct |
7 |
Correct |
11 ms |
9984 KB |
Output is correct |
8 |
Correct |
10 ms |
9984 KB |
Output is correct |
9 |
Correct |
10 ms |
10112 KB |
Output is correct |
10 |
Correct |
10 ms |
9984 KB |
Output is correct |
11 |
Correct |
9 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9984 KB |
Output is correct |
14 |
Correct |
9 ms |
9984 KB |
Output is correct |
15 |
Correct |
10 ms |
9984 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
10 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
10 ms |
9984 KB |
Output is correct |
20 |
Correct |
10 ms |
9984 KB |
Output is correct |
21 |
Correct |
10 ms |
9984 KB |
Output is correct |
22 |
Correct |
10 ms |
9984 KB |
Output is correct |
23 |
Correct |
9 ms |
9984 KB |
Output is correct |
24 |
Correct |
10 ms |
9984 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
9 ms |
9984 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
10 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
31 |
Correct |
11 ms |
9984 KB |
Output is correct |
32 |
Correct |
10 ms |
9984 KB |
Output is correct |
33 |
Correct |
269 ms |
40796 KB |
Output is correct |
34 |
Correct |
269 ms |
41188 KB |
Output is correct |
35 |
Correct |
285 ms |
41156 KB |
Output is correct |
36 |
Correct |
251 ms |
40544 KB |
Output is correct |
37 |
Correct |
270 ms |
40768 KB |
Output is correct |
38 |
Correct |
257 ms |
40796 KB |
Output is correct |
39 |
Correct |
276 ms |
40912 KB |
Output is correct |
40 |
Correct |
279 ms |
40804 KB |
Output is correct |
41 |
Correct |
265 ms |
41940 KB |
Output is correct |
42 |
Correct |
263 ms |
40784 KB |
Output is correct |
43 |
Correct |
246 ms |
41040 KB |
Output is correct |
44 |
Correct |
229 ms |
41812 KB |
Output is correct |
45 |
Correct |
217 ms |
40116 KB |
Output is correct |
46 |
Correct |
229 ms |
41820 KB |
Output is correct |
47 |
Correct |
240 ms |
40420 KB |
Output is correct |
48 |
Correct |
216 ms |
40160 KB |
Output is correct |
49 |
Correct |
236 ms |
40416 KB |
Output is correct |
50 |
Correct |
237 ms |
42204 KB |
Output is correct |
51 |
Correct |
241 ms |
42324 KB |
Output is correct |
52 |
Correct |
228 ms |
40596 KB |
Output is correct |
53 |
Correct |
257 ms |
40676 KB |
Output is correct |
54 |
Correct |
227 ms |
39896 KB |
Output is correct |
55 |
Correct |
245 ms |
40404 KB |
Output is correct |
56 |
Correct |
241 ms |
40436 KB |
Output is correct |
57 |
Correct |
240 ms |
40048 KB |
Output is correct |
58 |
Correct |
241 ms |
41684 KB |
Output is correct |
59 |
Correct |
200 ms |
32868 KB |
Output is correct |
60 |
Correct |
200 ms |
38640 KB |
Output is correct |
61 |
Correct |
206 ms |
39136 KB |
Output is correct |
62 |
Correct |
221 ms |
40928 KB |
Output is correct |
63 |
Correct |
211 ms |
38496 KB |
Output is correct |
64 |
Correct |
205 ms |
39136 KB |
Output is correct |
65 |
Correct |
222 ms |
42744 KB |
Output is correct |
66 |
Correct |
220 ms |
40544 KB |
Output is correct |
67 |
Correct |
218 ms |
38632 KB |
Output is correct |
68 |
Correct |
211 ms |
40804 KB |
Output is correct |
69 |
Correct |
214 ms |
38880 KB |
Output is correct |
70 |
Correct |
213 ms |
40800 KB |
Output is correct |
71 |
Correct |
209 ms |
40900 KB |
Output is correct |
72 |
Correct |
216 ms |
40800 KB |
Output is correct |
73 |
Correct |
214 ms |
39008 KB |
Output is correct |
74 |
Correct |
279 ms |
34396 KB |
Output is correct |
75 |
Correct |
283 ms |
33832 KB |
Output is correct |
76 |
Correct |
265 ms |
35664 KB |
Output is correct |
77 |
Correct |
270 ms |
34016 KB |
Output is correct |
78 |
Correct |
246 ms |
34136 KB |
Output is correct |
79 |
Correct |
251 ms |
34128 KB |
Output is correct |
80 |
Correct |
282 ms |
34264 KB |
Output is correct |
81 |
Correct |
287 ms |
34136 KB |
Output is correct |
82 |
Correct |
261 ms |
33888 KB |
Output is correct |
83 |
Correct |
267 ms |
35684 KB |
Output is correct |
84 |
Correct |
256 ms |
35420 KB |
Output is correct |
85 |
Correct |
267 ms |
34604 KB |
Output is correct |
86 |
Correct |
248 ms |
34008 KB |
Output is correct |
87 |
Correct |
260 ms |
34508 KB |
Output is correct |
88 |
Correct |
252 ms |
34120 KB |
Output is correct |
89 |
Correct |
264 ms |
33772 KB |
Output is correct |
90 |
Correct |
263 ms |
33852 KB |
Output is correct |
91 |
Correct |
257 ms |
34356 KB |
Output is correct |
92 |
Correct |
247 ms |
33728 KB |
Output is correct |
93 |
Correct |
246 ms |
33752 KB |
Output is correct |
94 |
Correct |
261 ms |
33844 KB |
Output is correct |
95 |
Correct |
247 ms |
34224 KB |
Output is correct |
96 |
Correct |
255 ms |
33976 KB |
Output is correct |
97 |
Correct |
248 ms |
33852 KB |
Output is correct |
98 |
Correct |
230 ms |
34056 KB |
Output is correct |
99 |
Correct |
270 ms |
34112 KB |
Output is correct |
100 |
Correct |
244 ms |
34104 KB |
Output is correct |
101 |
Correct |
299 ms |
33884 KB |
Output is correct |
102 |
Correct |
239 ms |
33976 KB |
Output is correct |