#include <bits/stdc++.h>
#define pii pair<int, int>
#define ti3 tuple<int, int, int>
#define ti4 tuple<int, int, int, int>
#define int long long
// We are dancing on fire ~~ ~ Seunghee, Dun Dun Dance
using namespace std;
// Notice that in a 2d array (pos, t), each number will form a parallelogram in the array.
// for eg, sample input (look at 6):
// 93265
// 99366
// 99966
// 99996
// 99999
// for each parallelogram, we can decompose it into 3 triangles starting from t=0.
// for eg, 6 in sample input:
// .XX6Y
// ..X66
// ...66
// ....6
// .....
// left point of triangle is 1 + (largest leftmost idx >= a[i]), right point of triangle is
// (smallest rightmost idx > a[i]) - 1;
// updating a triangle = updating a parallelogram [l, inf) starting from t=0 and
// updating a rectangle [r+1, inf) starting from t=0
// so actual update on triangle will be the difference b/w the 2 intersections.
// ie. actual_range_sum0(l, r) = range_sum_par0(l, r) - range_sum_rect0(l, r);
// note: if rightmost / leftmost elem does not exist just use a very big/small number
// Moreover, all (actual) updates are parallelograms, so
// B..XX
// XC..X
// abc
// notice that b to c = a to b (B and C are equal unless the parallelogram of B ends there)
// so range_sum(l, r, t) = range_sum_par(l, r, t) - range_sum_rect(l, r, t)
// = range_sum_par0(l - h + 1, r - h + 1, t) - range_sum_rect0(l, r)
// when h is the height of the parallelogram, which is basically t + 1(0-indexed)
// to ensure that B and C are equal (I think? - but we should remove those out of range triangles anyways)
// we sweep line by T, and remove triangles that are out of range (height < t);
int arr[200005], L[200005], R[200005], ans[200005];
vector<ti3> heights[200005];
vector<ti3> queries[200005];
struct fenwick{
int N;
int fw[400010], fw2[400010];
int off = 200002;
fenwick(int N): N(N){
memset(fw, 0, sizeof fw);
memset(fw2, 0, sizeof fw2);
}
void update(int x, int y, int v) { //inclusive
x += off; y += off;
for (int tx=x; tx <= N; tx += tx&(-tx)) fw[tx] += v, fw2[tx] -= v*(x-1);
for (int ty=y+1; ty <= N; ty += ty&(-ty)) fw[ty] -= v, fw2[ty] += v*y;
}
int sum (int x) {
int res = 0;
for (int tx=x; tx; tx -= tx&(-tx)) res += fw[tx]*x + fw2[tx];
return res;
}
int range_sum(int x, int y) { //inclusive
x += off; y += off;
return sum(y)-sum(x-1);
}
} *parfw = new fenwick(400005), *rectfw = new fenwick(400005);
int n;
void tri(int l, int r, int v, bool upd){
if (l > r) return;
parfw->update(l, n+1, v);
rectfw->update(r+1, n+1, v);
if (upd){
int height = r-l+1;
if (height <= n) heights[height].push_back({l, r, -v});
}
}
main(){
int q; cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> arr[i];
stack<int> nums;
for (int i = 1; i <= n; i++){
while (!nums.empty() && arr[nums.top()] < arr[i]) nums.pop();
L[i] = (nums.empty()?-n-1:nums.top());
nums.push(i);
}
while (!nums.empty()) nums.pop();
for (int i = n; i > 0; i--){
while (!nums.empty() && arr[nums.top()] <= arr[i]) nums.pop();
R[i] = (nums.empty()?n+1:nums.top());
nums.push(i);
}
for (int i = 1; i <= n; i++){
tri(L[i]+1, R[i]-1, arr[i], true); // large triangle
tri(L[i]+1, i-1, -arr[i], true); // small triangle
tri(i+1, R[i]-1, -arr[i], true); // small triangle
}
for (int i = 1; i <= q; i++){
int t, l, r; cin >> t >> l >> r;
queries[t].push_back({l, r, i});
}
// time starts from 0, so triangles with heights t or below should be removed
for (int t = 1; t <= n; t++){
for (auto [l, r, v]: heights[t]) tri(l, r, v, false);
for (auto [l, r, i]: queries[t]) ans[i] = parfw->range_sum(l-t, r-t) - rectfw->range_sum(l, r);
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
Compilation message
ho_t5.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
75 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22236 KB |
Output is correct |
2 |
Correct |
11 ms |
22240 KB |
Output is correct |
3 |
Correct |
11 ms |
22232 KB |
Output is correct |
4 |
Correct |
12 ms |
22232 KB |
Output is correct |
5 |
Correct |
12 ms |
22224 KB |
Output is correct |
6 |
Correct |
12 ms |
22248 KB |
Output is correct |
7 |
Correct |
11 ms |
22172 KB |
Output is correct |
8 |
Correct |
12 ms |
22248 KB |
Output is correct |
9 |
Correct |
11 ms |
22224 KB |
Output is correct |
10 |
Correct |
11 ms |
22248 KB |
Output is correct |
11 |
Correct |
11 ms |
22224 KB |
Output is correct |
12 |
Correct |
11 ms |
22188 KB |
Output is correct |
13 |
Correct |
11 ms |
22252 KB |
Output is correct |
14 |
Correct |
11 ms |
22252 KB |
Output is correct |
15 |
Correct |
12 ms |
22200 KB |
Output is correct |
16 |
Correct |
14 ms |
22240 KB |
Output is correct |
17 |
Correct |
12 ms |
22224 KB |
Output is correct |
18 |
Correct |
11 ms |
22244 KB |
Output is correct |
19 |
Correct |
11 ms |
22224 KB |
Output is correct |
20 |
Correct |
13 ms |
22252 KB |
Output is correct |
21 |
Correct |
13 ms |
22224 KB |
Output is correct |
22 |
Correct |
11 ms |
22224 KB |
Output is correct |
23 |
Correct |
11 ms |
22244 KB |
Output is correct |
24 |
Correct |
12 ms |
22296 KB |
Output is correct |
25 |
Correct |
11 ms |
22240 KB |
Output is correct |
26 |
Correct |
11 ms |
22224 KB |
Output is correct |
27 |
Correct |
11 ms |
22224 KB |
Output is correct |
28 |
Correct |
11 ms |
22224 KB |
Output is correct |
29 |
Correct |
11 ms |
22224 KB |
Output is correct |
30 |
Correct |
11 ms |
22260 KB |
Output is correct |
31 |
Correct |
11 ms |
22224 KB |
Output is correct |
32 |
Correct |
13 ms |
22224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22236 KB |
Output is correct |
2 |
Correct |
402 ms |
55136 KB |
Output is correct |
3 |
Correct |
357 ms |
52128 KB |
Output is correct |
4 |
Correct |
413 ms |
52436 KB |
Output is correct |
5 |
Correct |
378 ms |
58560 KB |
Output is correct |
6 |
Correct |
382 ms |
54708 KB |
Output is correct |
7 |
Correct |
369 ms |
55284 KB |
Output is correct |
8 |
Correct |
361 ms |
52476 KB |
Output is correct |
9 |
Correct |
461 ms |
55228 KB |
Output is correct |
10 |
Correct |
379 ms |
52116 KB |
Output is correct |
11 |
Correct |
379 ms |
55680 KB |
Output is correct |
12 |
Correct |
362 ms |
54824 KB |
Output is correct |
13 |
Correct |
378 ms |
58344 KB |
Output is correct |
14 |
Correct |
366 ms |
52164 KB |
Output is correct |
15 |
Correct |
391 ms |
58584 KB |
Output is correct |
16 |
Correct |
390 ms |
51940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22236 KB |
Output is correct |
2 |
Correct |
430 ms |
52576 KB |
Output is correct |
3 |
Correct |
407 ms |
52048 KB |
Output is correct |
4 |
Correct |
414 ms |
53016 KB |
Output is correct |
5 |
Correct |
430 ms |
52120 KB |
Output is correct |
6 |
Correct |
399 ms |
52420 KB |
Output is correct |
7 |
Correct |
404 ms |
52292 KB |
Output is correct |
8 |
Correct |
399 ms |
52764 KB |
Output is correct |
9 |
Correct |
397 ms |
52208 KB |
Output is correct |
10 |
Correct |
393 ms |
51696 KB |
Output is correct |
11 |
Correct |
415 ms |
53076 KB |
Output is correct |
12 |
Correct |
429 ms |
52632 KB |
Output is correct |
13 |
Correct |
408 ms |
52792 KB |
Output is correct |
14 |
Correct |
414 ms |
52356 KB |
Output is correct |
15 |
Correct |
417 ms |
52848 KB |
Output is correct |
16 |
Correct |
420 ms |
52252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
50452 KB |
Output is correct |
2 |
Correct |
381 ms |
50500 KB |
Output is correct |
3 |
Correct |
385 ms |
51148 KB |
Output is correct |
4 |
Correct |
378 ms |
50616 KB |
Output is correct |
5 |
Correct |
395 ms |
50696 KB |
Output is correct |
6 |
Correct |
388 ms |
50356 KB |
Output is correct |
7 |
Correct |
402 ms |
50820 KB |
Output is correct |
8 |
Correct |
380 ms |
50852 KB |
Output is correct |
9 |
Correct |
396 ms |
50804 KB |
Output is correct |
10 |
Correct |
401 ms |
50792 KB |
Output is correct |
11 |
Correct |
374 ms |
50968 KB |
Output is correct |
12 |
Correct |
377 ms |
50896 KB |
Output is correct |
13 |
Correct |
364 ms |
50752 KB |
Output is correct |
14 |
Correct |
405 ms |
50876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22236 KB |
Output is correct |
2 |
Correct |
11 ms |
22240 KB |
Output is correct |
3 |
Correct |
11 ms |
22232 KB |
Output is correct |
4 |
Correct |
12 ms |
22232 KB |
Output is correct |
5 |
Correct |
12 ms |
22224 KB |
Output is correct |
6 |
Correct |
12 ms |
22248 KB |
Output is correct |
7 |
Correct |
11 ms |
22172 KB |
Output is correct |
8 |
Correct |
12 ms |
22248 KB |
Output is correct |
9 |
Correct |
11 ms |
22224 KB |
Output is correct |
10 |
Correct |
11 ms |
22248 KB |
Output is correct |
11 |
Correct |
11 ms |
22224 KB |
Output is correct |
12 |
Correct |
11 ms |
22188 KB |
Output is correct |
13 |
Correct |
11 ms |
22252 KB |
Output is correct |
14 |
Correct |
11 ms |
22252 KB |
Output is correct |
15 |
Correct |
12 ms |
22200 KB |
Output is correct |
16 |
Correct |
14 ms |
22240 KB |
Output is correct |
17 |
Correct |
12 ms |
22224 KB |
Output is correct |
18 |
Correct |
11 ms |
22244 KB |
Output is correct |
19 |
Correct |
11 ms |
22224 KB |
Output is correct |
20 |
Correct |
13 ms |
22252 KB |
Output is correct |
21 |
Correct |
13 ms |
22224 KB |
Output is correct |
22 |
Correct |
11 ms |
22224 KB |
Output is correct |
23 |
Correct |
11 ms |
22244 KB |
Output is correct |
24 |
Correct |
12 ms |
22296 KB |
Output is correct |
25 |
Correct |
11 ms |
22240 KB |
Output is correct |
26 |
Correct |
11 ms |
22224 KB |
Output is correct |
27 |
Correct |
11 ms |
22224 KB |
Output is correct |
28 |
Correct |
11 ms |
22224 KB |
Output is correct |
29 |
Correct |
11 ms |
22224 KB |
Output is correct |
30 |
Correct |
11 ms |
22260 KB |
Output is correct |
31 |
Correct |
11 ms |
22224 KB |
Output is correct |
32 |
Correct |
13 ms |
22224 KB |
Output is correct |
33 |
Correct |
402 ms |
55136 KB |
Output is correct |
34 |
Correct |
357 ms |
52128 KB |
Output is correct |
35 |
Correct |
413 ms |
52436 KB |
Output is correct |
36 |
Correct |
378 ms |
58560 KB |
Output is correct |
37 |
Correct |
382 ms |
54708 KB |
Output is correct |
38 |
Correct |
369 ms |
55284 KB |
Output is correct |
39 |
Correct |
361 ms |
52476 KB |
Output is correct |
40 |
Correct |
461 ms |
55228 KB |
Output is correct |
41 |
Correct |
379 ms |
52116 KB |
Output is correct |
42 |
Correct |
379 ms |
55680 KB |
Output is correct |
43 |
Correct |
362 ms |
54824 KB |
Output is correct |
44 |
Correct |
378 ms |
58344 KB |
Output is correct |
45 |
Correct |
366 ms |
52164 KB |
Output is correct |
46 |
Correct |
391 ms |
58584 KB |
Output is correct |
47 |
Correct |
390 ms |
51940 KB |
Output is correct |
48 |
Correct |
430 ms |
52576 KB |
Output is correct |
49 |
Correct |
407 ms |
52048 KB |
Output is correct |
50 |
Correct |
414 ms |
53016 KB |
Output is correct |
51 |
Correct |
430 ms |
52120 KB |
Output is correct |
52 |
Correct |
399 ms |
52420 KB |
Output is correct |
53 |
Correct |
404 ms |
52292 KB |
Output is correct |
54 |
Correct |
399 ms |
52764 KB |
Output is correct |
55 |
Correct |
397 ms |
52208 KB |
Output is correct |
56 |
Correct |
393 ms |
51696 KB |
Output is correct |
57 |
Correct |
415 ms |
53076 KB |
Output is correct |
58 |
Correct |
429 ms |
52632 KB |
Output is correct |
59 |
Correct |
408 ms |
52792 KB |
Output is correct |
60 |
Correct |
414 ms |
52356 KB |
Output is correct |
61 |
Correct |
417 ms |
52848 KB |
Output is correct |
62 |
Correct |
420 ms |
52252 KB |
Output is correct |
63 |
Correct |
387 ms |
50452 KB |
Output is correct |
64 |
Correct |
381 ms |
50500 KB |
Output is correct |
65 |
Correct |
385 ms |
51148 KB |
Output is correct |
66 |
Correct |
378 ms |
50616 KB |
Output is correct |
67 |
Correct |
395 ms |
50696 KB |
Output is correct |
68 |
Correct |
388 ms |
50356 KB |
Output is correct |
69 |
Correct |
402 ms |
50820 KB |
Output is correct |
70 |
Correct |
380 ms |
50852 KB |
Output is correct |
71 |
Correct |
396 ms |
50804 KB |
Output is correct |
72 |
Correct |
401 ms |
50792 KB |
Output is correct |
73 |
Correct |
374 ms |
50968 KB |
Output is correct |
74 |
Correct |
377 ms |
50896 KB |
Output is correct |
75 |
Correct |
364 ms |
50752 KB |
Output is correct |
76 |
Correct |
405 ms |
50876 KB |
Output is correct |
77 |
Correct |
495 ms |
53384 KB |
Output is correct |
78 |
Correct |
446 ms |
54196 KB |
Output is correct |
79 |
Correct |
455 ms |
53280 KB |
Output is correct |
80 |
Correct |
409 ms |
53452 KB |
Output is correct |
81 |
Correct |
437 ms |
53288 KB |
Output is correct |
82 |
Correct |
425 ms |
53444 KB |
Output is correct |
83 |
Correct |
449 ms |
53392 KB |
Output is correct |
84 |
Correct |
420 ms |
53504 KB |
Output is correct |
85 |
Correct |
446 ms |
53500 KB |
Output is correct |
86 |
Correct |
450 ms |
53408 KB |
Output is correct |
87 |
Correct |
424 ms |
55028 KB |
Output is correct |
88 |
Correct |
376 ms |
54912 KB |
Output is correct |
89 |
Correct |
363 ms |
54460 KB |
Output is correct |
90 |
Correct |
377 ms |
54924 KB |
Output is correct |
91 |
Correct |
403 ms |
54488 KB |
Output is correct |
92 |
Correct |
382 ms |
54096 KB |
Output is correct |
93 |
Correct |
399 ms |
54344 KB |
Output is correct |
94 |
Correct |
402 ms |
55536 KB |
Output is correct |
95 |
Correct |
415 ms |
55168 KB |
Output is correct |
96 |
Correct |
464 ms |
54656 KB |
Output is correct |
97 |
Correct |
422 ms |
54620 KB |
Output is correct |
98 |
Correct |
356 ms |
53728 KB |
Output is correct |
99 |
Correct |
361 ms |
53928 KB |
Output is correct |
100 |
Correct |
382 ms |
54488 KB |
Output is correct |
101 |
Correct |
398 ms |
54484 KB |
Output is correct |
102 |
Correct |
407 ms |
54920 KB |
Output is correct |