#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define ll long long
const int mxn=3e5+5,szz=(1<<20);
int n,q,a[mxn];
bool aval=0;
ll akhar,fakhar,bakhar,ans;
struct segment{
ll res[szz],rast[szz],chap[szz],lazysum[szz],lazy[szz],brast[szz],bchap[szz],frast[szz],fchap[szz];
void getm(int id,int L,int R){
if(!aval){
aval=1;
akhar=rast[id];
bakhar=brast[id];
fakhar=frast[id];
ans=res[id];
}
else{
int cnt=0;
//cout<<akhar<<" "<<chap[id]<<'\n';
if(akhar>chap[id]){
if(fakhar<=akhar && chap[id]<=fchap[id]){
ans+=bakhar*bchap[id];
if(brast[id]==R-L)
cnt=1;
}
else if(fakhar<=akhar){
ans+=bakhar;
}
else if(chap[id]<=fchap[id]){
ans+=bchap[id];
if(brast[id]==R-L)
cnt=2;
}
else
ans++;
}
if(akhar<chap[id]){
if(fakhar>=akhar && chap[id]>=fchap[id]){
ans+=bakhar*bchap[id];
if(brast[id]==R-L)
cnt=1;
}
else if(fakhar>=akhar){
ans+=bakhar;
}
else if(chap[id]>=fchap[id]){
ans+=bchap[id];
if(brast[id]==R-L)
cnt=2;
}
else
ans++;
}
fakhar=(R-L==1)?akhar:frast[id];
akhar=rast[id];
ans+=res[id];
bakhar=(cnt==1)?bakhar+brast[id]:brast[id];
if(cnt==2)
bakhar++;
}
}
void shift(int id){
for(int x=id*2;x<=id*2+1;x++){
rast[x]*=(lazy[id])?-1:1;
chap[x]*=(lazy[id])?-1:1;
rast[x]+=lazysum[id];
chap[x]+=lazysum[id];
frast[x]*=(lazy[id])?-1:1;
fchap[x]*=(lazy[id])?-1:1;
frast[x]+=lazysum[id];
fchap[x]+=lazysum[id];
lazysum[x]*=(lazy[id])?-1:1;
lazysum[x]+=lazysum[id];
lazy[x]^=lazy[id];
}
lazysum[id]=lazy[id]=0;
}
void merge(int id,int L,int R,int mid){
rast[id]=rast[id*2+1];
chap[id]=chap[id*2];
frast[id]=(R-mid==1)?rast[id*2]:frast[id*2+1];
fchap[id]=(mid-L==1)?chap[id*2+1]:fchap[id*2];
res[id]=res[id*2]+res[id*2+1];
brast[id]=brast[id*2+1];
bchap[id]=bchap[id*2];
if(rast[id*2]>chap[id*2+1]){
//cout<<chap[id*2+1]<<" "<<fchap[id*2+1]<<'\n';
if(frast[id*2]<=rast[id*2] && chap[id*2+1]<=fchap[id*2+1]){
res[id]+=brast[id*2]*bchap[id*2+1];
if(brast[id*2+1]==R-mid)
brast[id]+=brast[id*2];
if(bchap[id*2]==mid-L)
bchap[id]+=bchap[id*2+1];
}
else if(frast[id*2]<=rast[id*2]){
res[id]+=brast[id*2];
if(bchap[id*2]==mid-L)
bchap[id]++;
}
else if(chap[id*2+1]<=fchap[id*2+1]){
res[id]+=bchap[id*2+1];
if(brast[id*2+1]==R-mid)
brast[id]++;
}
else
res[id]++;
//cout<<" "<<L<<" "<<R<<"" <<res[id]<<''
}
else if(rast[id*2]<chap[id*2+1]){
if(frast[id*2]>=rast[id*2] && chap[id*2+1]>=fchap[id*2+1]){
res[id]+=brast[id*2]*bchap[id*2+1];
if(brast[id*2+1]==R-mid)
brast[id]+=brast[id*2];
if(bchap[id*2]==mid-L)
bchap[id]+=bchap[id*2+1];
}
else if(frast[id*2]>=rast[id*2]){
res[id]+=brast[id*2];
if(bchap[id*2]==mid-L)
bchap[id]++;
}
else if(chap[id*2+1]>=fchap[id*2+1]){
res[id]+=bchap[id*2+1];
if(brast[id*2+1]==R-mid)
brast[id]++;
}
else
res[id]++;
}
}
void add(int id,int L,int R,int l,int r,int x){
if(l>=R || L>=r)
return ;
if(l<=L && R<=r){
lazysum[id]+=x;
rast[id]+=x;
chap[id]+=x;
frast[id]+=x;
fchap[id]+=x;
if(L+1==R){
brast[id]=bchap[id]=1;
res[id]=1;
}
return ;
}
int mid=(L+R)>>1;
shift(id);
add(id*2,L,mid,l,r,x);
add(id*2+1,mid ,R,l,r,x);
merge(id,L,R,mid);
return ;
}
void up(int id,int L,int R,int l,int r){
if(l>=R || L>=r)
return ;
if(l<=L && R<=r){
lazysum[id]*=-1;
rast[id]*=-1;
chap[id]*=-1;
frast[id]*=-1;
fchap[id]*=-1;
lazy[id]^=1;
return ;
}
int mid=(L+R)>>1;
shift(id);
up(id*2,L,mid,l,r);
up(id*2+1,mid, R,l,r);
merge(id,L,R,mid);
return ;
}
void get(int id,int L,int R,int l,int r){
if(l>=R || L>=r)
return ;
if(l<=L && R<=r){
getm(id,L,R);
return ;
}
int mid=(L+R)>>1;
shift(id);
get(id*2,L,mid,l,r);
get(id*2+1,mid, R,l,r);
merge(id,L,R,mid);
return ;
}
}seg;
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
seg.add(1,1,n+1,i,i+1,a[i]);
}
while(q--){
int l,r,z;
char ty;
cin>>ty;
if(ty=='\\')
cin>>ty;
cin>>l>>r;
if(ty=='+'){
cin>>z;
seg.add(1,1,n+1,l,r+1,z);
}
else if(ty=='?'){
aval=0;
seg.get(1,1,n+1,l,r+1);
cout<<ans<<'\n';
}
else{
seg.up(1,1,n+1,l,r+1);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7768 KB |
Output is correct |
2 |
Correct |
10 ms |
7512 KB |
Output is correct |
3 |
Correct |
10 ms |
7516 KB |
Output is correct |
4 |
Correct |
13 ms |
7676 KB |
Output is correct |
5 |
Correct |
9 ms |
7620 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
15 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
736 ms |
76068 KB |
Output is correct |
2 |
Correct |
155 ms |
6668 KB |
Output is correct |
3 |
Correct |
689 ms |
76116 KB |
Output is correct |
4 |
Correct |
758 ms |
76112 KB |
Output is correct |
5 |
Correct |
708 ms |
76132 KB |
Output is correct |
6 |
Correct |
737 ms |
76148 KB |
Output is correct |
7 |
Correct |
716 ms |
76116 KB |
Output is correct |
8 |
Correct |
745 ms |
76116 KB |
Output is correct |
9 |
Correct |
691 ms |
76112 KB |
Output is correct |
10 |
Correct |
630 ms |
76296 KB |
Output is correct |
11 |
Correct |
771 ms |
76112 KB |
Output is correct |
12 |
Correct |
736 ms |
76112 KB |
Output is correct |
13 |
Correct |
583 ms |
77144 KB |
Output is correct |
14 |
Correct |
592 ms |
76904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7768 KB |
Output is correct |
2 |
Correct |
10 ms |
7512 KB |
Output is correct |
3 |
Correct |
10 ms |
7516 KB |
Output is correct |
4 |
Correct |
13 ms |
7676 KB |
Output is correct |
5 |
Correct |
9 ms |
7620 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
15 ms |
7512 KB |
Output is correct |
8 |
Correct |
259 ms |
27220 KB |
Output is correct |
9 |
Correct |
242 ms |
27476 KB |
Output is correct |
10 |
Correct |
260 ms |
28384 KB |
Output is correct |
11 |
Correct |
499 ms |
28264 KB |
Output is correct |
12 |
Correct |
256 ms |
28492 KB |
Output is correct |
13 |
Correct |
282 ms |
28576 KB |
Output is correct |
14 |
Correct |
248 ms |
28500 KB |
Output is correct |
15 |
Correct |
259 ms |
27324 KB |
Output is correct |
16 |
Correct |
259 ms |
28476 KB |
Output is correct |
17 |
Correct |
248 ms |
28500 KB |
Output is correct |
18 |
Correct |
201 ms |
28496 KB |
Output is correct |
19 |
Correct |
176 ms |
28408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7768 KB |
Output is correct |
2 |
Correct |
10 ms |
7512 KB |
Output is correct |
3 |
Correct |
10 ms |
7516 KB |
Output is correct |
4 |
Correct |
13 ms |
7676 KB |
Output is correct |
5 |
Correct |
9 ms |
7620 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
15 ms |
7512 KB |
Output is correct |
8 |
Correct |
736 ms |
76068 KB |
Output is correct |
9 |
Correct |
155 ms |
6668 KB |
Output is correct |
10 |
Correct |
689 ms |
76116 KB |
Output is correct |
11 |
Correct |
758 ms |
76112 KB |
Output is correct |
12 |
Correct |
708 ms |
76132 KB |
Output is correct |
13 |
Correct |
737 ms |
76148 KB |
Output is correct |
14 |
Correct |
716 ms |
76116 KB |
Output is correct |
15 |
Correct |
745 ms |
76116 KB |
Output is correct |
16 |
Correct |
691 ms |
76112 KB |
Output is correct |
17 |
Correct |
630 ms |
76296 KB |
Output is correct |
18 |
Correct |
771 ms |
76112 KB |
Output is correct |
19 |
Correct |
736 ms |
76112 KB |
Output is correct |
20 |
Correct |
583 ms |
77144 KB |
Output is correct |
21 |
Correct |
592 ms |
76904 KB |
Output is correct |
22 |
Correct |
259 ms |
27220 KB |
Output is correct |
23 |
Correct |
242 ms |
27476 KB |
Output is correct |
24 |
Correct |
260 ms |
28384 KB |
Output is correct |
25 |
Correct |
499 ms |
28264 KB |
Output is correct |
26 |
Correct |
256 ms |
28492 KB |
Output is correct |
27 |
Correct |
282 ms |
28576 KB |
Output is correct |
28 |
Correct |
248 ms |
28500 KB |
Output is correct |
29 |
Correct |
259 ms |
27324 KB |
Output is correct |
30 |
Correct |
259 ms |
28476 KB |
Output is correct |
31 |
Correct |
248 ms |
28500 KB |
Output is correct |
32 |
Correct |
201 ms |
28496 KB |
Output is correct |
33 |
Correct |
176 ms |
28408 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
885 ms |
81492 KB |
Output is correct |
37 |
Correct |
938 ms |
84560 KB |
Output is correct |
38 |
Correct |
1650 ms |
83436 KB |
Output is correct |
39 |
Correct |
901 ms |
84560 KB |
Output is correct |
40 |
Correct |
862 ms |
81416 KB |
Output is correct |
41 |
Correct |
851 ms |
84560 KB |
Output is correct |
42 |
Correct |
817 ms |
81492 KB |
Output is correct |
43 |
Correct |
916 ms |
84452 KB |
Output is correct |
44 |
Correct |
958 ms |
84564 KB |
Output is correct |
45 |
Correct |
930 ms |
84420 KB |
Output is correct |
46 |
Correct |
867 ms |
84472 KB |
Output is correct |
47 |
Correct |
825 ms |
84620 KB |
Output is correct |