#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define int ll
#define y1 yy
#define maksim gay
#define ppb pop_back
#define gcd(a,b) __gcd(a,b)
#define in insert
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1e16;
const int N=5e4+100;
const int MAX=4e5+10;
const int mod=1e9+7;
int n,q;
int a[MAX];
int l[MAX],r[MAX];
int ans[MAX];
vector<pair<pii,int>> zap[MAX];
vector<pair<pii,int>> ev_diag[MAX],ev_norm[MAX];
struct segtree{
int t[4*MAX];
int add[4*MAX];
segtree(){
mem(t,0);
mem(add,0);
}
void push(int v,int tl,int tr){
int tm=(tl+tr);
if(tm>=0)tm/=2;
else{
tm=tm/2;
tm--;
}
add[2*v]+=add[v];
add[2*v+1]+=add[v];
t[2*v]+=add[v]*(tm-tl+1);
t[2*v+1]+=add[v]*(tr-tm);
add[v]=0;
}
void update(int v,int tl,int tr,int l,int r,int x){
// if(v>1000)return;
if(l>r||tl>r||l>tr)return;
if(l<=tl&&tr<=r){
t[v]+=x*(tr-tl+1);
add[v]+=x;
return;
}
push(v,tl,tr);
int tm=(tl+tr);
if(tm>=0)tm/=2;
else{
tm=tm/2;
tm--;
}
update(2*v,tl,tm,l,r,x);
update(2*v+1,tm+1,tr,l,r,x);
t[v]=t[2*v]+t[2*v+1];
}
int get(int v,int tl,int tr,int l,int r){
if(l>r||tl>r||l>tr)return 0;
if(l<=tl&&tr<=r)return t[v];
push(v,tl,tr);
int tm=(tl+tr);
if(tm>=0)tm/=2;
else{
tm=tm/2;
tm--;
}
return get(2*v,tl,tm,l,r)+get(2*v+1,tm+1,tr,l,r);
}
}t[2];
void upd_triangle(int lx,int ly,int rx,int ry,int x){
int lef_diag=lx-ly;
int rig_diag=rx-ly;
// cout<<lx<<" "<<rx<<" "<<ly<<" "<<ry<<" "<<x<<" "<<lef_diag<<" "<<rig_diag<<"\n";
ev_diag[ly].pb({{lef_diag,rig_diag},x});
ev_diag[ry+1].pb({{lef_diag,rig_diag},-x});
}
void upd_square(int lx,int ly,int rx,int ry,int x){
// cout<<lx<<" "<<rx<<" "<<ly<<" "<<ry<<" "<<x<<"\n";
ev_norm[ly].pb({{lx,rx},x});
ev_norm[ry+1].pb({{lx,rx},-x});
}
void upd_triangle(int l,int r,int x){
// cout<<l<<" "<<r<<" "<<x<<"\n";
int h=r-l;
upd_triangle(l,0,n,n-l,x);
if(r!=n)upd_triangle(r+1,h+1,n,n-l,-x);
if(r!=n)upd_square(r+1,0,n,h,-x);
}
void solve(){
cin>>n>>q;
stack<int> st;
for(int i=1;i<=n;i++)cin>>a[i];
l[1]=0;
st.push(1);
for(int i=2;i<=n;i++){
l[i]=i-1;
while(!st.empty()&&a[st.top()]<=a[i]){
l[i]=l[st.top()];
st.pop();
}
st.push(i);
}
r[n]=n+1;
for(int i=n-1;i>=1;i--){
r[i]=i+1;
while(!st.empty()&&a[st.top()]<a[i]){
r[i]=r[st.top()];
st.pop();
}
st.push(i);
}
for(int i=1;i<=q;i++){
int t,l,r;
cin>>t>>l>>r;
zap[t].pb({{l,r},i});
}
for(int i=1;i<=n;i++){
if(l[i]==0)l[i]=i-n-1;
int tl=l[i]+1;
int tr=r[i]-1;
if(tl>tr)continue;
upd_triangle(tl,i-1,-a[i]);
upd_triangle(i+1,tr,-a[i]);
upd_triangle(tl,tr,a[i]);
}
for(int i=0;i<=n;i++){
for(auto x:ev_diag[i]){
int l=x.F.F,r=x.F.S;
// cout<<l<<" "<<r<<" "<<x.S<<"\n";
t[1].update(1,-n,n,l,r,x.S);
}
for(auto x:ev_norm[i]){
int l=x.F.F,r=x.F.S;
t[0].update(1,0,n,l,r,x.S);
}
for(auto x:zap[i]){
int lef_diag=x.F.F-i;
int rig_diag=x.F.S-i;
int lef_norm=x.F.F;
int rig_norm=x.F.S;
// cout<<t[1].get(1,-n,n,lef_diag,rig_diag)<<"\n";
ans[x.S]=t[0].get(1,0,n,lef_norm,rig_norm)+t[1].get(1,-n,n,lef_diag,rig_diag);
}
}
for(int i=1;i<=q;i++){
cout<<ans[i]<<" ";
}
}
main(){
// freopen("prizes.in", "r", stdin);
// freopen("prizes.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message
ho_t5.cpp:179:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
179 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
80480 KB |
Output is correct |
2 |
Correct |
19 ms |
80732 KB |
Output is correct |
3 |
Correct |
19 ms |
80732 KB |
Output is correct |
4 |
Correct |
19 ms |
80732 KB |
Output is correct |
5 |
Correct |
20 ms |
80732 KB |
Output is correct |
6 |
Correct |
30 ms |
80720 KB |
Output is correct |
7 |
Correct |
21 ms |
80732 KB |
Output is correct |
8 |
Correct |
19 ms |
80732 KB |
Output is correct |
9 |
Correct |
20 ms |
80736 KB |
Output is correct |
10 |
Correct |
20 ms |
80732 KB |
Output is correct |
11 |
Correct |
20 ms |
80732 KB |
Output is correct |
12 |
Correct |
23 ms |
80728 KB |
Output is correct |
13 |
Correct |
20 ms |
80732 KB |
Output is correct |
14 |
Correct |
21 ms |
80640 KB |
Output is correct |
15 |
Correct |
20 ms |
80640 KB |
Output is correct |
16 |
Correct |
20 ms |
80768 KB |
Output is correct |
17 |
Correct |
21 ms |
80700 KB |
Output is correct |
18 |
Correct |
20 ms |
80732 KB |
Output is correct |
19 |
Correct |
20 ms |
80732 KB |
Output is correct |
20 |
Correct |
20 ms |
80620 KB |
Output is correct |
21 |
Correct |
20 ms |
80732 KB |
Output is correct |
22 |
Correct |
20 ms |
80644 KB |
Output is correct |
23 |
Correct |
19 ms |
80620 KB |
Output is correct |
24 |
Correct |
20 ms |
80720 KB |
Output is correct |
25 |
Correct |
19 ms |
80984 KB |
Output is correct |
26 |
Correct |
20 ms |
80732 KB |
Output is correct |
27 |
Correct |
23 ms |
80804 KB |
Output is correct |
28 |
Correct |
20 ms |
80732 KB |
Output is correct |
29 |
Correct |
20 ms |
80572 KB |
Output is correct |
30 |
Correct |
19 ms |
80732 KB |
Output is correct |
31 |
Correct |
20 ms |
80728 KB |
Output is correct |
32 |
Correct |
22 ms |
80720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
80480 KB |
Output is correct |
2 |
Correct |
849 ms |
194300 KB |
Output is correct |
3 |
Correct |
844 ms |
193460 KB |
Output is correct |
4 |
Correct |
850 ms |
193912 KB |
Output is correct |
5 |
Correct |
893 ms |
203592 KB |
Output is correct |
6 |
Correct |
859 ms |
194476 KB |
Output is correct |
7 |
Correct |
863 ms |
194996 KB |
Output is correct |
8 |
Correct |
881 ms |
205140 KB |
Output is correct |
9 |
Correct |
829 ms |
198196 KB |
Output is correct |
10 |
Correct |
859 ms |
192748 KB |
Output is correct |
11 |
Correct |
872 ms |
198440 KB |
Output is correct |
12 |
Correct |
829 ms |
192732 KB |
Output is correct |
13 |
Correct |
842 ms |
202852 KB |
Output is correct |
14 |
Correct |
860 ms |
202832 KB |
Output is correct |
15 |
Correct |
870 ms |
203056 KB |
Output is correct |
16 |
Correct |
855 ms |
194344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
80480 KB |
Output is correct |
2 |
Correct |
840 ms |
194808 KB |
Output is correct |
3 |
Correct |
819 ms |
192820 KB |
Output is correct |
4 |
Correct |
866 ms |
204452 KB |
Output is correct |
5 |
Correct |
831 ms |
194316 KB |
Output is correct |
6 |
Correct |
817 ms |
195320 KB |
Output is correct |
7 |
Correct |
839 ms |
201772 KB |
Output is correct |
8 |
Correct |
832 ms |
194372 KB |
Output is correct |
9 |
Correct |
811 ms |
193096 KB |
Output is correct |
10 |
Correct |
799 ms |
192824 KB |
Output is correct |
11 |
Correct |
832 ms |
204356 KB |
Output is correct |
12 |
Correct |
835 ms |
203824 KB |
Output is correct |
13 |
Correct |
897 ms |
205088 KB |
Output is correct |
14 |
Correct |
823 ms |
193848 KB |
Output is correct |
15 |
Correct |
835 ms |
205896 KB |
Output is correct |
16 |
Correct |
841 ms |
205056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
771 ms |
188080 KB |
Output is correct |
2 |
Correct |
776 ms |
191268 KB |
Output is correct |
3 |
Correct |
787 ms |
193560 KB |
Output is correct |
4 |
Correct |
795 ms |
189764 KB |
Output is correct |
5 |
Correct |
771 ms |
189032 KB |
Output is correct |
6 |
Correct |
766 ms |
191612 KB |
Output is correct |
7 |
Correct |
786 ms |
193296 KB |
Output is correct |
8 |
Correct |
780 ms |
192312 KB |
Output is correct |
9 |
Correct |
776 ms |
190560 KB |
Output is correct |
10 |
Correct |
796 ms |
192404 KB |
Output is correct |
11 |
Correct |
783 ms |
190776 KB |
Output is correct |
12 |
Correct |
787 ms |
191248 KB |
Output is correct |
13 |
Correct |
774 ms |
191440 KB |
Output is correct |
14 |
Correct |
785 ms |
191408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
80480 KB |
Output is correct |
2 |
Correct |
19 ms |
80732 KB |
Output is correct |
3 |
Correct |
19 ms |
80732 KB |
Output is correct |
4 |
Correct |
19 ms |
80732 KB |
Output is correct |
5 |
Correct |
20 ms |
80732 KB |
Output is correct |
6 |
Correct |
30 ms |
80720 KB |
Output is correct |
7 |
Correct |
21 ms |
80732 KB |
Output is correct |
8 |
Correct |
19 ms |
80732 KB |
Output is correct |
9 |
Correct |
20 ms |
80736 KB |
Output is correct |
10 |
Correct |
20 ms |
80732 KB |
Output is correct |
11 |
Correct |
20 ms |
80732 KB |
Output is correct |
12 |
Correct |
23 ms |
80728 KB |
Output is correct |
13 |
Correct |
20 ms |
80732 KB |
Output is correct |
14 |
Correct |
21 ms |
80640 KB |
Output is correct |
15 |
Correct |
20 ms |
80640 KB |
Output is correct |
16 |
Correct |
20 ms |
80768 KB |
Output is correct |
17 |
Correct |
21 ms |
80700 KB |
Output is correct |
18 |
Correct |
20 ms |
80732 KB |
Output is correct |
19 |
Correct |
20 ms |
80732 KB |
Output is correct |
20 |
Correct |
20 ms |
80620 KB |
Output is correct |
21 |
Correct |
20 ms |
80732 KB |
Output is correct |
22 |
Correct |
20 ms |
80644 KB |
Output is correct |
23 |
Correct |
19 ms |
80620 KB |
Output is correct |
24 |
Correct |
20 ms |
80720 KB |
Output is correct |
25 |
Correct |
19 ms |
80984 KB |
Output is correct |
26 |
Correct |
20 ms |
80732 KB |
Output is correct |
27 |
Correct |
23 ms |
80804 KB |
Output is correct |
28 |
Correct |
20 ms |
80732 KB |
Output is correct |
29 |
Correct |
20 ms |
80572 KB |
Output is correct |
30 |
Correct |
19 ms |
80732 KB |
Output is correct |
31 |
Correct |
20 ms |
80728 KB |
Output is correct |
32 |
Correct |
22 ms |
80720 KB |
Output is correct |
33 |
Correct |
849 ms |
194300 KB |
Output is correct |
34 |
Correct |
844 ms |
193460 KB |
Output is correct |
35 |
Correct |
850 ms |
193912 KB |
Output is correct |
36 |
Correct |
893 ms |
203592 KB |
Output is correct |
37 |
Correct |
859 ms |
194476 KB |
Output is correct |
38 |
Correct |
863 ms |
194996 KB |
Output is correct |
39 |
Correct |
881 ms |
205140 KB |
Output is correct |
40 |
Correct |
829 ms |
198196 KB |
Output is correct |
41 |
Correct |
859 ms |
192748 KB |
Output is correct |
42 |
Correct |
872 ms |
198440 KB |
Output is correct |
43 |
Correct |
829 ms |
192732 KB |
Output is correct |
44 |
Correct |
842 ms |
202852 KB |
Output is correct |
45 |
Correct |
860 ms |
202832 KB |
Output is correct |
46 |
Correct |
870 ms |
203056 KB |
Output is correct |
47 |
Correct |
855 ms |
194344 KB |
Output is correct |
48 |
Correct |
840 ms |
194808 KB |
Output is correct |
49 |
Correct |
819 ms |
192820 KB |
Output is correct |
50 |
Correct |
866 ms |
204452 KB |
Output is correct |
51 |
Correct |
831 ms |
194316 KB |
Output is correct |
52 |
Correct |
817 ms |
195320 KB |
Output is correct |
53 |
Correct |
839 ms |
201772 KB |
Output is correct |
54 |
Correct |
832 ms |
194372 KB |
Output is correct |
55 |
Correct |
811 ms |
193096 KB |
Output is correct |
56 |
Correct |
799 ms |
192824 KB |
Output is correct |
57 |
Correct |
832 ms |
204356 KB |
Output is correct |
58 |
Correct |
835 ms |
203824 KB |
Output is correct |
59 |
Correct |
897 ms |
205088 KB |
Output is correct |
60 |
Correct |
823 ms |
193848 KB |
Output is correct |
61 |
Correct |
835 ms |
205896 KB |
Output is correct |
62 |
Correct |
841 ms |
205056 KB |
Output is correct |
63 |
Correct |
771 ms |
188080 KB |
Output is correct |
64 |
Correct |
776 ms |
191268 KB |
Output is correct |
65 |
Correct |
787 ms |
193560 KB |
Output is correct |
66 |
Correct |
795 ms |
189764 KB |
Output is correct |
67 |
Correct |
771 ms |
189032 KB |
Output is correct |
68 |
Correct |
766 ms |
191612 KB |
Output is correct |
69 |
Correct |
786 ms |
193296 KB |
Output is correct |
70 |
Correct |
780 ms |
192312 KB |
Output is correct |
71 |
Correct |
776 ms |
190560 KB |
Output is correct |
72 |
Correct |
796 ms |
192404 KB |
Output is correct |
73 |
Correct |
783 ms |
190776 KB |
Output is correct |
74 |
Correct |
787 ms |
191248 KB |
Output is correct |
75 |
Correct |
774 ms |
191440 KB |
Output is correct |
76 |
Correct |
785 ms |
191408 KB |
Output is correct |
77 |
Correct |
892 ms |
195496 KB |
Output is correct |
78 |
Correct |
923 ms |
196784 KB |
Output is correct |
79 |
Correct |
920 ms |
206636 KB |
Output is correct |
80 |
Correct |
881 ms |
195060 KB |
Output is correct |
81 |
Correct |
876 ms |
194876 KB |
Output is correct |
82 |
Correct |
903 ms |
200052 KB |
Output is correct |
83 |
Correct |
898 ms |
196064 KB |
Output is correct |
84 |
Correct |
910 ms |
194020 KB |
Output is correct |
85 |
Correct |
899 ms |
204788 KB |
Output is correct |
86 |
Correct |
903 ms |
194804 KB |
Output is correct |
87 |
Correct |
861 ms |
207088 KB |
Output is correct |
88 |
Correct |
863 ms |
205728 KB |
Output is correct |
89 |
Correct |
871 ms |
194356 KB |
Output is correct |
90 |
Correct |
884 ms |
205072 KB |
Output is correct |
91 |
Correct |
840 ms |
195740 KB |
Output is correct |
92 |
Correct |
864 ms |
194196 KB |
Output is correct |
93 |
Correct |
870 ms |
196584 KB |
Output is correct |
94 |
Correct |
877 ms |
206300 KB |
Output is correct |
95 |
Correct |
891 ms |
205484 KB |
Output is correct |
96 |
Correct |
853 ms |
196580 KB |
Output is correct |
97 |
Correct |
856 ms |
196384 KB |
Output is correct |
98 |
Correct |
834 ms |
195012 KB |
Output is correct |
99 |
Correct |
859 ms |
199540 KB |
Output is correct |
100 |
Correct |
959 ms |
199572 KB |
Output is correct |
101 |
Correct |
874 ms |
195584 KB |
Output is correct |
102 |
Correct |
884 ms |
205032 KB |
Output is correct |