#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 12, MOD = (int)1e9 + 7;
int n,q,s[N];
vector<array<int,3>> qr[N];
struct segtree {
vector<ll> t,add;
void init() {
t.assign(N * 4,0);
add.assign(N * 4,0);
}
void inc(int v,ll val,int tl,int tr) {
add[v] += val;
t[v] += val*1ll* (tr - tl + 1);
}
void push(int v,int tl,int tr) {
if(add[v]) {
int tm = (tl + tr) >> 1;
inc(v + v,add[v],tl,tm);
inc(v + v + 1,add[v],tm+1,tr);
add[v] = 0;
}
}
void upd(int l,int r,ll val,int v = 1,int tl = 1,int tr = n) {
if(l > r || tl > r || l > tr) return ;
if(tl >= l && tr <= r) {
inc(v,val,tl,tr);
} else {
push(v,tl,tr);
int tm = (tl + tr) >> 1;
upd(l,r,val,v+v,tl,tm);
upd(l,r,val,v+v+1,tm+1,tr);
t[v] = t[v + v] + t[v + v + 1];
}
}
ll get(int l,int r,int v = 1,int tl = 1,int tr = n) {
if(l > r || tl >r || l > tr) return 0;
if(tl >= l && tr <= r) return t[v];
push(v,tl,tr);
int tm = (tl + tr) >> 1;
return get(l,r,v+v,tl,tm) + get(l,r,v+v+1,tm+1,tr);
}
}t_[3];
int prv[N],nx[N];
vector<array<int,4>> sub[N];
void prec() {
for(int i = 0;i < 3;++i) t_[i].init();
vector<int> st;
for(int i = 1;i <= n;i++) {
while(!st.empty() && s[st.back()] <= s[i]) {
st.pop_back();
}
if(!st.empty()) {
prv[i] = st.back();
}
st.push_back(i);
}
st.clear();
for(int i = n;i >= 1;i--) {
while(!st.empty() && s[st.back()] < s[i]) {
st.pop_back();
}
if(!st.empty()) {
nx[i] = st.back();
} else {
nx[i] = n + 1;
}
st.push_back(i);
}
auto triag = [&](int i,int s,int val) {
++s;
int l = i,r = i + s - 1;
if(l > r) return;
sub[s].push_back({l,n,-val,0});
sub[s].push_back({r+1,n,val,1});
t_[0].upd(l,n,val);
t_[1].upd(r + 1,n,-val);
};
for(int i = 1;i <= n;i++) {
if(prv[i]) {
triag(prv[i] + 1,nx[i] - 1 - (prv[i] + 1),s[i]);
triag(prv[i] + 1,i - prv[i] - 2,-s[i]);
triag(i + 1,(nx[i] - 1) - (i + 1),-s[i]);
} else {
int _s = nx[i] - i;
// cout << i << ' ' << i + _s - 1 << '\n';
t_[1].upd(i,i + _s - 1,s[i]);
// cout << i + 1 << ' ' << _s - 1 << "\n";
triag(i+1,_s-2,-s[i]);
}
}
}
ll ans[N];
void test() {
cin >> n >> q;
for(int i = 1;i <= n;i++) {
cin >> s[i];
}
prec();
for(int i = 1;i <= q;i++) {
int l,r,t;
cin >> t >> l >> r;
qr[t].push_back({l,r,i});
}
for(int t = 0;t <= n;t++) {
for(auto [l,r,v,id]:sub[t]) {
t_[id].upd(l,r,v);
}
for(auto [l,r,i]:qr[t]) {
ans[i] = t_[0].get(l-t,r-t) + t_[1].get(l,r);
}
}
for(int i = 1;i <= q;++i) {
cout << ans[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94288 KB |
Output is correct |
2 |
Correct |
36 ms |
94476 KB |
Output is correct |
3 |
Correct |
38 ms |
94292 KB |
Output is correct |
4 |
Correct |
35 ms |
94228 KB |
Output is correct |
5 |
Correct |
40 ms |
94420 KB |
Output is correct |
6 |
Correct |
39 ms |
94292 KB |
Output is correct |
7 |
Correct |
40 ms |
94292 KB |
Output is correct |
8 |
Correct |
36 ms |
94300 KB |
Output is correct |
9 |
Correct |
36 ms |
94368 KB |
Output is correct |
10 |
Correct |
41 ms |
94288 KB |
Output is correct |
11 |
Correct |
49 ms |
94292 KB |
Output is correct |
12 |
Correct |
38 ms |
94300 KB |
Output is correct |
13 |
Correct |
35 ms |
94292 KB |
Output is correct |
14 |
Correct |
39 ms |
94216 KB |
Output is correct |
15 |
Correct |
39 ms |
94292 KB |
Output is correct |
16 |
Correct |
47 ms |
94236 KB |
Output is correct |
17 |
Correct |
38 ms |
94300 KB |
Output is correct |
18 |
Correct |
39 ms |
94384 KB |
Output is correct |
19 |
Correct |
36 ms |
94328 KB |
Output is correct |
20 |
Correct |
36 ms |
94348 KB |
Output is correct |
21 |
Correct |
37 ms |
94296 KB |
Output is correct |
22 |
Correct |
44 ms |
94244 KB |
Output is correct |
23 |
Correct |
36 ms |
94300 KB |
Output is correct |
24 |
Correct |
38 ms |
94452 KB |
Output is correct |
25 |
Correct |
37 ms |
94292 KB |
Output is correct |
26 |
Correct |
42 ms |
94292 KB |
Output is correct |
27 |
Correct |
43 ms |
94300 KB |
Output is correct |
28 |
Correct |
38 ms |
94292 KB |
Output is correct |
29 |
Correct |
40 ms |
94292 KB |
Output is correct |
30 |
Correct |
36 ms |
94292 KB |
Output is correct |
31 |
Correct |
40 ms |
94388 KB |
Output is correct |
32 |
Correct |
41 ms |
94232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94288 KB |
Output is correct |
2 |
Correct |
488 ms |
126412 KB |
Output is correct |
3 |
Correct |
448 ms |
125956 KB |
Output is correct |
4 |
Correct |
490 ms |
126280 KB |
Output is correct |
5 |
Correct |
495 ms |
126900 KB |
Output is correct |
6 |
Correct |
499 ms |
126140 KB |
Output is correct |
7 |
Correct |
514 ms |
126256 KB |
Output is correct |
8 |
Correct |
537 ms |
126496 KB |
Output is correct |
9 |
Correct |
484 ms |
126124 KB |
Output is correct |
10 |
Correct |
479 ms |
126208 KB |
Output is correct |
11 |
Correct |
490 ms |
126436 KB |
Output is correct |
12 |
Correct |
458 ms |
125884 KB |
Output is correct |
13 |
Correct |
515 ms |
127012 KB |
Output is correct |
14 |
Correct |
543 ms |
125960 KB |
Output is correct |
15 |
Correct |
503 ms |
126776 KB |
Output is correct |
16 |
Correct |
507 ms |
125748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94288 KB |
Output is correct |
2 |
Correct |
492 ms |
123568 KB |
Output is correct |
3 |
Correct |
481 ms |
123316 KB |
Output is correct |
4 |
Correct |
500 ms |
124584 KB |
Output is correct |
5 |
Correct |
489 ms |
123596 KB |
Output is correct |
6 |
Correct |
495 ms |
123692 KB |
Output is correct |
7 |
Correct |
480 ms |
124020 KB |
Output is correct |
8 |
Correct |
488 ms |
123824 KB |
Output is correct |
9 |
Correct |
454 ms |
123492 KB |
Output is correct |
10 |
Correct |
462 ms |
123068 KB |
Output is correct |
11 |
Correct |
534 ms |
124440 KB |
Output is correct |
12 |
Correct |
453 ms |
123804 KB |
Output is correct |
13 |
Correct |
479 ms |
124192 KB |
Output is correct |
14 |
Correct |
453 ms |
123692 KB |
Output is correct |
15 |
Correct |
464 ms |
124248 KB |
Output is correct |
16 |
Correct |
464 ms |
123852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
114364 KB |
Output is correct |
2 |
Correct |
364 ms |
113920 KB |
Output is correct |
3 |
Correct |
376 ms |
114480 KB |
Output is correct |
4 |
Correct |
369 ms |
114244 KB |
Output is correct |
5 |
Correct |
369 ms |
114176 KB |
Output is correct |
6 |
Correct |
355 ms |
113820 KB |
Output is correct |
7 |
Correct |
376 ms |
114216 KB |
Output is correct |
8 |
Correct |
376 ms |
114244 KB |
Output is correct |
9 |
Correct |
424 ms |
114248 KB |
Output is correct |
10 |
Correct |
383 ms |
114244 KB |
Output is correct |
11 |
Correct |
363 ms |
114760 KB |
Output is correct |
12 |
Correct |
375 ms |
114168 KB |
Output is correct |
13 |
Correct |
382 ms |
114284 KB |
Output is correct |
14 |
Correct |
369 ms |
114244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94288 KB |
Output is correct |
2 |
Correct |
36 ms |
94476 KB |
Output is correct |
3 |
Correct |
38 ms |
94292 KB |
Output is correct |
4 |
Correct |
35 ms |
94228 KB |
Output is correct |
5 |
Correct |
40 ms |
94420 KB |
Output is correct |
6 |
Correct |
39 ms |
94292 KB |
Output is correct |
7 |
Correct |
40 ms |
94292 KB |
Output is correct |
8 |
Correct |
36 ms |
94300 KB |
Output is correct |
9 |
Correct |
36 ms |
94368 KB |
Output is correct |
10 |
Correct |
41 ms |
94288 KB |
Output is correct |
11 |
Correct |
49 ms |
94292 KB |
Output is correct |
12 |
Correct |
38 ms |
94300 KB |
Output is correct |
13 |
Correct |
35 ms |
94292 KB |
Output is correct |
14 |
Correct |
39 ms |
94216 KB |
Output is correct |
15 |
Correct |
39 ms |
94292 KB |
Output is correct |
16 |
Correct |
47 ms |
94236 KB |
Output is correct |
17 |
Correct |
38 ms |
94300 KB |
Output is correct |
18 |
Correct |
39 ms |
94384 KB |
Output is correct |
19 |
Correct |
36 ms |
94328 KB |
Output is correct |
20 |
Correct |
36 ms |
94348 KB |
Output is correct |
21 |
Correct |
37 ms |
94296 KB |
Output is correct |
22 |
Correct |
44 ms |
94244 KB |
Output is correct |
23 |
Correct |
36 ms |
94300 KB |
Output is correct |
24 |
Correct |
38 ms |
94452 KB |
Output is correct |
25 |
Correct |
37 ms |
94292 KB |
Output is correct |
26 |
Correct |
42 ms |
94292 KB |
Output is correct |
27 |
Correct |
43 ms |
94300 KB |
Output is correct |
28 |
Correct |
38 ms |
94292 KB |
Output is correct |
29 |
Correct |
40 ms |
94292 KB |
Output is correct |
30 |
Correct |
36 ms |
94292 KB |
Output is correct |
31 |
Correct |
40 ms |
94388 KB |
Output is correct |
32 |
Correct |
41 ms |
94232 KB |
Output is correct |
33 |
Correct |
488 ms |
126412 KB |
Output is correct |
34 |
Correct |
448 ms |
125956 KB |
Output is correct |
35 |
Correct |
490 ms |
126280 KB |
Output is correct |
36 |
Correct |
495 ms |
126900 KB |
Output is correct |
37 |
Correct |
499 ms |
126140 KB |
Output is correct |
38 |
Correct |
514 ms |
126256 KB |
Output is correct |
39 |
Correct |
537 ms |
126496 KB |
Output is correct |
40 |
Correct |
484 ms |
126124 KB |
Output is correct |
41 |
Correct |
479 ms |
126208 KB |
Output is correct |
42 |
Correct |
490 ms |
126436 KB |
Output is correct |
43 |
Correct |
458 ms |
125884 KB |
Output is correct |
44 |
Correct |
515 ms |
127012 KB |
Output is correct |
45 |
Correct |
543 ms |
125960 KB |
Output is correct |
46 |
Correct |
503 ms |
126776 KB |
Output is correct |
47 |
Correct |
507 ms |
125748 KB |
Output is correct |
48 |
Correct |
492 ms |
123568 KB |
Output is correct |
49 |
Correct |
481 ms |
123316 KB |
Output is correct |
50 |
Correct |
500 ms |
124584 KB |
Output is correct |
51 |
Correct |
489 ms |
123596 KB |
Output is correct |
52 |
Correct |
495 ms |
123692 KB |
Output is correct |
53 |
Correct |
480 ms |
124020 KB |
Output is correct |
54 |
Correct |
488 ms |
123824 KB |
Output is correct |
55 |
Correct |
454 ms |
123492 KB |
Output is correct |
56 |
Correct |
462 ms |
123068 KB |
Output is correct |
57 |
Correct |
534 ms |
124440 KB |
Output is correct |
58 |
Correct |
453 ms |
123804 KB |
Output is correct |
59 |
Correct |
479 ms |
124192 KB |
Output is correct |
60 |
Correct |
453 ms |
123692 KB |
Output is correct |
61 |
Correct |
464 ms |
124248 KB |
Output is correct |
62 |
Correct |
464 ms |
123852 KB |
Output is correct |
63 |
Correct |
366 ms |
114364 KB |
Output is correct |
64 |
Correct |
364 ms |
113920 KB |
Output is correct |
65 |
Correct |
376 ms |
114480 KB |
Output is correct |
66 |
Correct |
369 ms |
114244 KB |
Output is correct |
67 |
Correct |
369 ms |
114176 KB |
Output is correct |
68 |
Correct |
355 ms |
113820 KB |
Output is correct |
69 |
Correct |
376 ms |
114216 KB |
Output is correct |
70 |
Correct |
376 ms |
114244 KB |
Output is correct |
71 |
Correct |
424 ms |
114248 KB |
Output is correct |
72 |
Correct |
383 ms |
114244 KB |
Output is correct |
73 |
Correct |
363 ms |
114760 KB |
Output is correct |
74 |
Correct |
375 ms |
114168 KB |
Output is correct |
75 |
Correct |
382 ms |
114284 KB |
Output is correct |
76 |
Correct |
369 ms |
114244 KB |
Output is correct |
77 |
Correct |
512 ms |
124724 KB |
Output is correct |
78 |
Correct |
519 ms |
125628 KB |
Output is correct |
79 |
Correct |
519 ms |
124692 KB |
Output is correct |
80 |
Correct |
504 ms |
124600 KB |
Output is correct |
81 |
Correct |
488 ms |
124464 KB |
Output is correct |
82 |
Correct |
501 ms |
124868 KB |
Output is correct |
83 |
Correct |
493 ms |
125116 KB |
Output is correct |
84 |
Correct |
496 ms |
124868 KB |
Output is correct |
85 |
Correct |
508 ms |
124972 KB |
Output is correct |
86 |
Correct |
492 ms |
124592 KB |
Output is correct |
87 |
Correct |
457 ms |
124896 KB |
Output is correct |
88 |
Correct |
449 ms |
124700 KB |
Output is correct |
89 |
Correct |
468 ms |
124292 KB |
Output is correct |
90 |
Correct |
460 ms |
124956 KB |
Output is correct |
91 |
Correct |
465 ms |
124348 KB |
Output is correct |
92 |
Correct |
459 ms |
123952 KB |
Output is correct |
93 |
Correct |
451 ms |
124292 KB |
Output is correct |
94 |
Correct |
447 ms |
125360 KB |
Output is correct |
95 |
Correct |
459 ms |
125272 KB |
Output is correct |
96 |
Correct |
476 ms |
124200 KB |
Output is correct |
97 |
Correct |
468 ms |
124516 KB |
Output is correct |
98 |
Correct |
524 ms |
123628 KB |
Output is correct |
99 |
Correct |
530 ms |
124232 KB |
Output is correct |
100 |
Correct |
539 ms |
124188 KB |
Output is correct |
101 |
Correct |
525 ms |
124096 KB |
Output is correct |
102 |
Correct |
533 ms |
124708 KB |
Output is correct |