#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
struct Query{
int l, r, idx;
Query(){}
Query(int l, int r, int idx): l(l), r(r), idx(idx){}
};
struct maxSegmentTree{
ll tree[800002];
void init(int i, int l, int r, ll A[]){
if(l==r){
tree[i] = A[l];
return;
}
int m = (l+r)>>1;
init(i*2, l, m, A);
init(i*2+1, m+1, r, A);
tree[i] = max(tree[i*2], tree[i*2+1]);
}
int biggerLim(int i, int l, int r, int x, ll lim, int dir){ /// bigger than lim / dir=0: left, dir=1: right
if(tree[i] <= lim) return dir==0 ? l-1 : r+1;
if(l==r) return l;
int m = (l+r)>>1;
if(dir == 0){
if(x<=m) return biggerLim(i*2, l, m, x, lim, dir);
int tmp = biggerLim(i*2+1, m+1, r, x, lim, dir);
if(tmp > m) return tmp;
return biggerLim(i*2, l, m, x, lim, dir);
}
else{
if(m<x) return biggerLim(i*2+1, m+1, r, x, lim, dir);
int tmp = biggerLim(i*2, l, m, x, lim, dir);
if(tmp <= m) return tmp;
return biggerLim(i*2+1, m+1, r, x, lim, dir);
}
}
} maxSeg;
struct lazySegmentTree{
struct Fenwick{
int n;
ll tree[400002];
void addRange(int x, ll y){
while(x){
tree[x] += y;
x -= x&-x;
}
}
ll rangeSum(int x){
ll ret = 0;
while(x<=n){
ret += tree[x];
x += x&-x;
}
return ret;
}
} mul, add;
int n;
void init(int _n){
mul.n = add.n = n = _n;
}
void addRange(int x, ll y){
x = min(x, n);
mul.addRange(x, y);
add.addRange(n, y*x);
add.addRange(x, -y*x);
}
inline ll rangeSum(int x){
if(x<=0) return 0;
return mul.rangeSum(x) * x + add.rangeSum(x);
}
inline ll rangeSum(int l, int r){
return rangeSum(r) - rangeSum(l-1);
}
} segRight, segDiag;
int n, q;
ll arr[200002];
vector<Query> query[200005];
ll ans[200002];
vector<pair<int, ll> > triangle[200005];
void input();
void makeTriangles();
void sweep();
int main(){
input();
makeTriangles();
sweep();
for(int i=1; i<=q; i++) printf("%lld\n", ans[i]);
}
void input(){
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%lld", &arr[i]);
}
for(int i=1; i<=q; i++){
int t, l, r;
scanf("%d %d %d", &t, &l, &r);
query[t+1].push_back(Query(l, r, i));
}
}
void makeTriangles(){
maxSeg.init(1, 1, n, arr);
for(int i=1; i<=n; i++){
triangle[1].push_back(make_pair(i, arr[i]));
int rLim = maxSeg.biggerLim(1, 1, n, i+1, arr[i]-1, 1);
if(i<n && rLim <= n && 1<=rLim-i+1 && rLim-i+1<=n+1)
triangle[rLim-i+1].push_back(make_pair(rLim, -arr[i]));
int lLim = maxSeg.biggerLim(1, 1, n, i-1, arr[i], 0);
if(i>1 && lLim >= 1 && 1<=i-lLim+1 && i-lLim+1<=n+1)
triangle[i-lLim+1].push_back(make_pair(i, -arr[i]));
if(i<n && i>1 && rLim <= n && lLim >= 1 &&
1<=(rLim - i + 1) + (i - lLim + 1) - 1 && (rLim - i + 1) + (i - lLim + 1) - 1 <= n+1){
triangle[(rLim - i + 1) + (i - lLim + 1) - 1].push_back(make_pair(rLim, arr[i]));
}
}
}
void sweep(){
segRight.init(n);
segDiag.init(2*n+1);
for(int i=1; i<=n+1; i++){
for(auto pr: triangle[i]){
segRight.addRange(pr.first - 1, -pr.second);
segDiag.addRange((n+1) + pr.first - i, pr.second);
}
for(auto qr: query[i]){
ans[qr.idx] += segRight.rangeSum(qr.l, qr.r);
ans[qr.idx] += segDiag.rangeSum((n+1) + qr.l - i, (n+1) + qr.r - i);
}
}
}
Compilation message
ho_t5.cpp: In function 'void input()':
ho_t5.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
109 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
111 | scanf("%lld", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
115 | scanf("%d %d %d", &t, &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9744 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9776 KB |
Output is correct |
4 |
Correct |
8 ms |
9680 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9760 KB |
Output is correct |
8 |
Correct |
7 ms |
9740 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9680 KB |
Output is correct |
11 |
Correct |
6 ms |
9672 KB |
Output is correct |
12 |
Correct |
7 ms |
9756 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9704 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9776 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9692 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9744 KB |
Output is correct |
2 |
Correct |
332 ms |
42072 KB |
Output is correct |
3 |
Correct |
311 ms |
47080 KB |
Output is correct |
4 |
Correct |
328 ms |
48052 KB |
Output is correct |
5 |
Correct |
317 ms |
48808 KB |
Output is correct |
6 |
Correct |
348 ms |
47644 KB |
Output is correct |
7 |
Correct |
337 ms |
47476 KB |
Output is correct |
8 |
Correct |
323 ms |
49172 KB |
Output is correct |
9 |
Correct |
362 ms |
49180 KB |
Output is correct |
10 |
Correct |
316 ms |
46904 KB |
Output is correct |
11 |
Correct |
310 ms |
49560 KB |
Output is correct |
12 |
Correct |
322 ms |
47596 KB |
Output is correct |
13 |
Correct |
326 ms |
49240 KB |
Output is correct |
14 |
Correct |
310 ms |
48780 KB |
Output is correct |
15 |
Correct |
311 ms |
49032 KB |
Output is correct |
16 |
Correct |
343 ms |
47952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9744 KB |
Output is correct |
2 |
Correct |
393 ms |
42688 KB |
Output is correct |
3 |
Correct |
373 ms |
42332 KB |
Output is correct |
4 |
Correct |
381 ms |
44160 KB |
Output is correct |
5 |
Correct |
384 ms |
47956 KB |
Output is correct |
6 |
Correct |
383 ms |
48332 KB |
Output is correct |
7 |
Correct |
375 ms |
48224 KB |
Output is correct |
8 |
Correct |
425 ms |
48352 KB |
Output is correct |
9 |
Correct |
425 ms |
47920 KB |
Output is correct |
10 |
Correct |
413 ms |
47684 KB |
Output is correct |
11 |
Correct |
404 ms |
49796 KB |
Output is correct |
12 |
Correct |
397 ms |
49568 KB |
Output is correct |
13 |
Correct |
462 ms |
49580 KB |
Output is correct |
14 |
Correct |
402 ms |
47948 KB |
Output is correct |
15 |
Correct |
363 ms |
49692 KB |
Output is correct |
16 |
Correct |
361 ms |
49476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
38080 KB |
Output is correct |
2 |
Correct |
339 ms |
39836 KB |
Output is correct |
3 |
Correct |
331 ms |
40480 KB |
Output is correct |
4 |
Correct |
330 ms |
38540 KB |
Output is correct |
5 |
Correct |
329 ms |
40052 KB |
Output is correct |
6 |
Correct |
329 ms |
38948 KB |
Output is correct |
7 |
Correct |
338 ms |
39060 KB |
Output is correct |
8 |
Correct |
345 ms |
39712 KB |
Output is correct |
9 |
Correct |
337 ms |
38476 KB |
Output is correct |
10 |
Correct |
350 ms |
38540 KB |
Output is correct |
11 |
Correct |
337 ms |
38788 KB |
Output is correct |
12 |
Correct |
341 ms |
40036 KB |
Output is correct |
13 |
Correct |
332 ms |
38772 KB |
Output is correct |
14 |
Correct |
332 ms |
39880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9744 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9776 KB |
Output is correct |
4 |
Correct |
8 ms |
9680 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9760 KB |
Output is correct |
8 |
Correct |
7 ms |
9740 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9680 KB |
Output is correct |
11 |
Correct |
6 ms |
9672 KB |
Output is correct |
12 |
Correct |
7 ms |
9756 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9704 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9776 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9692 KB |
Output is correct |
28 |
Correct |
6 ms |
9676 KB |
Output is correct |
29 |
Correct |
6 ms |
9676 KB |
Output is correct |
30 |
Correct |
6 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9676 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
332 ms |
42072 KB |
Output is correct |
34 |
Correct |
311 ms |
47080 KB |
Output is correct |
35 |
Correct |
328 ms |
48052 KB |
Output is correct |
36 |
Correct |
317 ms |
48808 KB |
Output is correct |
37 |
Correct |
348 ms |
47644 KB |
Output is correct |
38 |
Correct |
337 ms |
47476 KB |
Output is correct |
39 |
Correct |
323 ms |
49172 KB |
Output is correct |
40 |
Correct |
362 ms |
49180 KB |
Output is correct |
41 |
Correct |
316 ms |
46904 KB |
Output is correct |
42 |
Correct |
310 ms |
49560 KB |
Output is correct |
43 |
Correct |
322 ms |
47596 KB |
Output is correct |
44 |
Correct |
326 ms |
49240 KB |
Output is correct |
45 |
Correct |
310 ms |
48780 KB |
Output is correct |
46 |
Correct |
311 ms |
49032 KB |
Output is correct |
47 |
Correct |
343 ms |
47952 KB |
Output is correct |
48 |
Correct |
393 ms |
42688 KB |
Output is correct |
49 |
Correct |
373 ms |
42332 KB |
Output is correct |
50 |
Correct |
381 ms |
44160 KB |
Output is correct |
51 |
Correct |
384 ms |
47956 KB |
Output is correct |
52 |
Correct |
383 ms |
48332 KB |
Output is correct |
53 |
Correct |
375 ms |
48224 KB |
Output is correct |
54 |
Correct |
425 ms |
48352 KB |
Output is correct |
55 |
Correct |
425 ms |
47920 KB |
Output is correct |
56 |
Correct |
413 ms |
47684 KB |
Output is correct |
57 |
Correct |
404 ms |
49796 KB |
Output is correct |
58 |
Correct |
397 ms |
49568 KB |
Output is correct |
59 |
Correct |
462 ms |
49580 KB |
Output is correct |
60 |
Correct |
402 ms |
47948 KB |
Output is correct |
61 |
Correct |
363 ms |
49692 KB |
Output is correct |
62 |
Correct |
361 ms |
49476 KB |
Output is correct |
63 |
Correct |
348 ms |
38080 KB |
Output is correct |
64 |
Correct |
339 ms |
39836 KB |
Output is correct |
65 |
Correct |
331 ms |
40480 KB |
Output is correct |
66 |
Correct |
330 ms |
38540 KB |
Output is correct |
67 |
Correct |
329 ms |
40052 KB |
Output is correct |
68 |
Correct |
329 ms |
38948 KB |
Output is correct |
69 |
Correct |
338 ms |
39060 KB |
Output is correct |
70 |
Correct |
345 ms |
39712 KB |
Output is correct |
71 |
Correct |
337 ms |
38476 KB |
Output is correct |
72 |
Correct |
350 ms |
38540 KB |
Output is correct |
73 |
Correct |
337 ms |
38788 KB |
Output is correct |
74 |
Correct |
341 ms |
40036 KB |
Output is correct |
75 |
Correct |
332 ms |
38772 KB |
Output is correct |
76 |
Correct |
332 ms |
39880 KB |
Output is correct |
77 |
Correct |
398 ms |
49208 KB |
Output is correct |
78 |
Correct |
422 ms |
49664 KB |
Output is correct |
79 |
Correct |
374 ms |
50408 KB |
Output is correct |
80 |
Correct |
383 ms |
49112 KB |
Output is correct |
81 |
Correct |
380 ms |
48956 KB |
Output is correct |
82 |
Correct |
422 ms |
50504 KB |
Output is correct |
83 |
Correct |
401 ms |
49168 KB |
Output is correct |
84 |
Correct |
389 ms |
48920 KB |
Output is correct |
85 |
Correct |
382 ms |
50400 KB |
Output is correct |
86 |
Correct |
394 ms |
49252 KB |
Output is correct |
87 |
Correct |
341 ms |
50684 KB |
Output is correct |
88 |
Correct |
341 ms |
50328 KB |
Output is correct |
89 |
Correct |
350 ms |
48512 KB |
Output is correct |
90 |
Correct |
347 ms |
50072 KB |
Output is correct |
91 |
Correct |
367 ms |
48556 KB |
Output is correct |
92 |
Correct |
332 ms |
48296 KB |
Output is correct |
93 |
Correct |
358 ms |
48568 KB |
Output is correct |
94 |
Correct |
350 ms |
50436 KB |
Output is correct |
95 |
Correct |
360 ms |
50304 KB |
Output is correct |
96 |
Correct |
343 ms |
48800 KB |
Output is correct |
97 |
Correct |
366 ms |
49004 KB |
Output is correct |
98 |
Correct |
358 ms |
48308 KB |
Output is correct |
99 |
Correct |
371 ms |
49692 KB |
Output is correct |
100 |
Correct |
395 ms |
49652 KB |
Output is correct |
101 |
Correct |
374 ms |
48336 KB |
Output is correct |
102 |
Correct |
363 ms |
50024 KB |
Output is correct |