#include <bits/stdc++.h>
using namespace std;
//ifstream fin("a.in");
//ofstream fout("a.out");
typedef long long ll;
int v[200000],l[200000],r[200000],stv[200000],n;
ll ans[200000];
///i misspled square
int cnt2 = 0,disp = 300000;
class superfenwick{
ll fen[550001][2];
private:
ll query2(ll pos,ll t){
pos+=disp;
ll r = 0;
for(ll i = pos;i > 0;i-=(i&-i))r+=fen[i][t];
return r;
}
void add2(ll pos,ll nr,ll t){
pos+=disp;
for(ll i = pos;i <= 550000;i+=(i&-i))fen[i][t]+=nr;
}
public:
ll query(ll l,ll r){
return (query2(r,0)*r - query2(l - 1,0)*(l - 1)) - query2(r,1) + query2(l - 1,1);
}
void add(ll l,ll r,ll nr){
add2(l,nr,0);
add2(r + 1,-nr,0);
add2(l,nr*(l - 1),1);
add2(r + 1,-nr*r,1);
}
};
superfenwick triangles,squares;
struct events{
int tip,t,l,r,nr;
}v2[3000000];
bool cmp(events a,events b){
if(a.t == b.t)return a.tip < b.tip;
return a.t < b.t;
}
void addsqaure(int l,int r,int h,int nr){
//cout<<"squaredata:"<<l<<','<<r<<','<<h<<','<<nr<<'\n';
v2[cnt2++] = {0,0,l,r,nr};
v2[cnt2++] = {0,h,l,r,-nr};
}
void addtriangle(int l,int r,int nr,int ydisp = 0){
if(r == n - 1){
v2[cnt2++] = {1,ydisp,l - ydisp,r - ydisp,nr};
}else{
v2[cnt2++] = {1,0,l,n - 1,nr};
v2[cnt2++] = {1,r - l + 1,r + 1 - (r - l + 1),n - 1 - (r - l + 1),-nr};
//addsqaure(r + 1,n - 1,r - l + 1,-nr);
v2[cnt2++] = {0,0,r + 1,n - 1,-nr};
v2[cnt2++] = {0,r - l + 1,r + 1,n - 1,nr};
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int q,i,cnt = 0,a,b,c;
cin>>n>>q;
//n = q = 200000;
for(i = 0;i < n;i++){
cin>>v[i];
//v[i] = rand()%1000000000;
}
cnt = -1;
for(i = 0;i < n;i++){
while(cnt >= 0 && v[i] > v[stv[cnt]]){
r[stv[cnt--]] = i;
}
stv[++cnt] = i;
}
while(cnt-- >= 0){
r[stv[cnt + 1]] = n;
}
cnt = -1;
for(i = n - 1;i >= 0;i--){
while(cnt >= 0 && v[i] >= v[stv[cnt]]){
l[stv[cnt--]] = i;
}
stv[++cnt] = i;
}
while(cnt-- >= 0){
l[stv[cnt + 1]] = -200005;
}
for(i = 0;i < n;i++){
l[i]++;r[i]--;
//fout<<"For i = "<<i<<'\n';
//fout<<l[i]<<' '<<r[i]<<'\n';
addtriangle(l[i],r[i],v[i]);
addtriangle(l[i],i - 1,-v[i]);
addtriangle(i + 1,r[i],-v[i]);
//cout<<'\n';
}
for(i = 0;i < q;i++){
cin>>a>>b>>c;
//a = i;
//b = n - 1;
//c = n;
v2[cnt2++] = {2,a,b - 1,c - 1,i};
}
sort(v2,v2 + cnt2,cmp);
for(i = 0;i < cnt2;i++){
if(v2[i].tip == 0){
squares.add(v2[i].l,v2[i].r,v2[i].nr);
}else if(v2[i].tip == 1){
triangles.add(v2[i].l,v2[i].r,v2[i].nr);
}else{
ans[v2[i].nr] = squares.query(v2[i].l,v2[i].r) + triangles.query(v2[i].l - v2[i].t,v2[i].r - v2[i].t);
}
}
for(i = 0;i < q;i++)cout<<ans[i]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
582 ms |
63008 KB |
Output is correct |
3 |
Correct |
637 ms |
68152 KB |
Output is correct |
4 |
Correct |
667 ms |
68348 KB |
Output is correct |
5 |
Correct |
634 ms |
69472 KB |
Output is correct |
6 |
Correct |
651 ms |
68488 KB |
Output is correct |
7 |
Correct |
640 ms |
69204 KB |
Output is correct |
8 |
Correct |
557 ms |
69632 KB |
Output is correct |
9 |
Correct |
643 ms |
69176 KB |
Output is correct |
10 |
Correct |
751 ms |
67700 KB |
Output is correct |
11 |
Correct |
596 ms |
69460 KB |
Output is correct |
12 |
Correct |
712 ms |
67520 KB |
Output is correct |
13 |
Correct |
560 ms |
69168 KB |
Output is correct |
14 |
Correct |
577 ms |
68948 KB |
Output is correct |
15 |
Correct |
616 ms |
69144 KB |
Output is correct |
16 |
Correct |
565 ms |
68460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
671 ms |
61920 KB |
Output is correct |
3 |
Correct |
503 ms |
66280 KB |
Output is correct |
4 |
Correct |
599 ms |
68936 KB |
Output is correct |
5 |
Correct |
657 ms |
67020 KB |
Output is correct |
6 |
Correct |
709 ms |
67720 KB |
Output is correct |
7 |
Correct |
648 ms |
68020 KB |
Output is correct |
8 |
Correct |
672 ms |
67408 KB |
Output is correct |
9 |
Correct |
516 ms |
66552 KB |
Output is correct |
10 |
Correct |
546 ms |
65932 KB |
Output is correct |
11 |
Correct |
601 ms |
68796 KB |
Output is correct |
12 |
Correct |
638 ms |
68120 KB |
Output is correct |
13 |
Correct |
608 ms |
68432 KB |
Output is correct |
14 |
Correct |
665 ms |
66936 KB |
Output is correct |
15 |
Correct |
599 ms |
68644 KB |
Output is correct |
16 |
Correct |
621 ms |
68112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
519 ms |
49516 KB |
Output is correct |
2 |
Correct |
494 ms |
49804 KB |
Output is correct |
3 |
Correct |
498 ms |
50908 KB |
Output is correct |
4 |
Correct |
451 ms |
49220 KB |
Output is correct |
5 |
Correct |
539 ms |
49704 KB |
Output is correct |
6 |
Correct |
446 ms |
49876 KB |
Output is correct |
7 |
Correct |
429 ms |
50768 KB |
Output is correct |
8 |
Correct |
523 ms |
50340 KB |
Output is correct |
9 |
Correct |
530 ms |
49516 KB |
Output is correct |
10 |
Correct |
468 ms |
50324 KB |
Output is correct |
11 |
Correct |
513 ms |
49740 KB |
Output is correct |
12 |
Correct |
500 ms |
50076 KB |
Output is correct |
13 |
Correct |
491 ms |
49652 KB |
Output is correct |
14 |
Correct |
539 ms |
49868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
582 ms |
63008 KB |
Output is correct |
34 |
Correct |
637 ms |
68152 KB |
Output is correct |
35 |
Correct |
667 ms |
68348 KB |
Output is correct |
36 |
Correct |
634 ms |
69472 KB |
Output is correct |
37 |
Correct |
651 ms |
68488 KB |
Output is correct |
38 |
Correct |
640 ms |
69204 KB |
Output is correct |
39 |
Correct |
557 ms |
69632 KB |
Output is correct |
40 |
Correct |
643 ms |
69176 KB |
Output is correct |
41 |
Correct |
751 ms |
67700 KB |
Output is correct |
42 |
Correct |
596 ms |
69460 KB |
Output is correct |
43 |
Correct |
712 ms |
67520 KB |
Output is correct |
44 |
Correct |
560 ms |
69168 KB |
Output is correct |
45 |
Correct |
577 ms |
68948 KB |
Output is correct |
46 |
Correct |
616 ms |
69144 KB |
Output is correct |
47 |
Correct |
565 ms |
68460 KB |
Output is correct |
48 |
Correct |
671 ms |
61920 KB |
Output is correct |
49 |
Correct |
503 ms |
66280 KB |
Output is correct |
50 |
Correct |
599 ms |
68936 KB |
Output is correct |
51 |
Correct |
657 ms |
67020 KB |
Output is correct |
52 |
Correct |
709 ms |
67720 KB |
Output is correct |
53 |
Correct |
648 ms |
68020 KB |
Output is correct |
54 |
Correct |
672 ms |
67408 KB |
Output is correct |
55 |
Correct |
516 ms |
66552 KB |
Output is correct |
56 |
Correct |
546 ms |
65932 KB |
Output is correct |
57 |
Correct |
601 ms |
68796 KB |
Output is correct |
58 |
Correct |
638 ms |
68120 KB |
Output is correct |
59 |
Correct |
608 ms |
68432 KB |
Output is correct |
60 |
Correct |
665 ms |
66936 KB |
Output is correct |
61 |
Correct |
599 ms |
68644 KB |
Output is correct |
62 |
Correct |
621 ms |
68112 KB |
Output is correct |
63 |
Correct |
519 ms |
49516 KB |
Output is correct |
64 |
Correct |
494 ms |
49804 KB |
Output is correct |
65 |
Correct |
498 ms |
50908 KB |
Output is correct |
66 |
Correct |
451 ms |
49220 KB |
Output is correct |
67 |
Correct |
539 ms |
49704 KB |
Output is correct |
68 |
Correct |
446 ms |
49876 KB |
Output is correct |
69 |
Correct |
429 ms |
50768 KB |
Output is correct |
70 |
Correct |
523 ms |
50340 KB |
Output is correct |
71 |
Correct |
530 ms |
49516 KB |
Output is correct |
72 |
Correct |
468 ms |
50324 KB |
Output is correct |
73 |
Correct |
513 ms |
49740 KB |
Output is correct |
74 |
Correct |
500 ms |
50076 KB |
Output is correct |
75 |
Correct |
491 ms |
49652 KB |
Output is correct |
76 |
Correct |
539 ms |
49868 KB |
Output is correct |
77 |
Correct |
678 ms |
68188 KB |
Output is correct |
78 |
Correct |
627 ms |
69216 KB |
Output is correct |
79 |
Correct |
577 ms |
69316 KB |
Output is correct |
80 |
Correct |
600 ms |
68172 KB |
Output is correct |
81 |
Correct |
604 ms |
68172 KB |
Output is correct |
82 |
Correct |
618 ms |
68868 KB |
Output is correct |
83 |
Correct |
630 ms |
68548 KB |
Output is correct |
84 |
Correct |
555 ms |
67504 KB |
Output is correct |
85 |
Correct |
565 ms |
69404 KB |
Output is correct |
86 |
Correct |
659 ms |
67944 KB |
Output is correct |
87 |
Correct |
565 ms |
69536 KB |
Output is correct |
88 |
Correct |
556 ms |
69760 KB |
Output is correct |
89 |
Correct |
525 ms |
67440 KB |
Output is correct |
90 |
Correct |
542 ms |
69324 KB |
Output is correct |
91 |
Correct |
612 ms |
68184 KB |
Output is correct |
92 |
Correct |
504 ms |
67296 KB |
Output is correct |
93 |
Correct |
586 ms |
68516 KB |
Output is correct |
94 |
Correct |
546 ms |
70044 KB |
Output is correct |
95 |
Correct |
602 ms |
69708 KB |
Output is correct |
96 |
Correct |
621 ms |
68508 KB |
Output is correct |
97 |
Correct |
574 ms |
68572 KB |
Output is correct |
98 |
Correct |
663 ms |
67728 KB |
Output is correct |
99 |
Correct |
626 ms |
68392 KB |
Output is correct |
100 |
Correct |
593 ms |
68812 KB |
Output is correct |
101 |
Correct |
621 ms |
68124 KB |
Output is correct |
102 |
Correct |
572 ms |
69252 KB |
Output is correct |