rt,代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e6+114;
const int inf = 1e9;
int tot,root;
struct Node{
int val,sz,ls,rs,tag1,tag2,mx;//翻转标记和加法标记
int data;//这个节点存储的数据
}treap[maxn];
int clone(int data){
int New=++tot;
treap[New].data=data;
treap[New].val=rand();
treap[New].sz=1;
treap[New].tag1=0;
treap[New].tag2=0;
treap[New].mx=data;
treap[New].ls=0;
treap[New].rs=0;
return New;
}
inline void pushup(int cur){
if(cur==0) return ;
treap[cur].sz=treap[treap[cur].ls].sz+treap[treap[cur].rs].sz+1;
treap[cur].mx=max((treap[cur].ls==0?-inf:treap[treap[cur].ls].mx),max((treap[cur].rs==0?-inf:treap[treap[cur].rs].mx),treap[cur].data));
}
inline void rot(int cur){
if(cur==0) return ;
treap[cur].tag1^=1;
}
inline void add(int cur,int v){
if(cur==0) return ;
treap[cur].tag2+=v;
}
inline void pushdown(int cur){
if(cur==0) return ;
treap[cur].data+=treap[cur].tag2;
treap[cur].mx+=treap[cur].tag2;
if(treap[cur].ls!=0) treap[treap[cur].ls].tag2+=treap[cur].tag2;
if(treap[cur].rs!=0) treap[treap[cur].rs].tag2+=treap[cur].tag2;
treap[cur].tag2=0;
if(treap[cur].tag1==1){
swap(treap[cur].ls,treap[cur].rs);
if(treap[cur].ls!=0) treap[treap[cur].ls].tag1^=1;
if(treap[cur].rs!=0) treap[treap[cur].rs].tag1^=1;
treap[cur].tag1=0;
}
}
int merge(int x,int y){
pushdown(x);
pushdown(y);
if (!x||!y) return x+y;
if (treap[x].val<treap[y].val)
{
treap[x].rs=merge(treap[x].rs,y);
pushup(x);
return x;
}
else
{
treap[y].ls=merge(x,treap[y].ls);
pushup(y);
return y;
}
}
void split(int cur,int x,int &l,int &r) {
pushdown(cur);
if(cur==0){
l=r=0;
return ;
}
if(treap[treap[cur].ls].sz>=x){
r=cur;
split(treap[cur].ls,x,l,treap[cur].ls);
}
else{
l=cur;
split(treap[cur].rs,x-treap[treap[cur].ls].sz-1,treap[cur].rs,r);
}
pushup(cur);
}
int n,T;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
cin>>n>>T;
for(int i=1;i<=n;i++){
root=merge(root,clone(0));
}
while(T--){
int opt;
cin>>opt;
if(opt==1){
int l,r,v;
cin>>l>>r>>v;
int x=0,y=0,z=0;
split(root,l-1,x,z);
split(z,r-l+1,y,z);
add(y,v);
root=merge(x,merge(y,z));
}
else if(opt==2){
int l,r;
cin>>l>>r;
int x=0,y=0,z=0;
split(root,l-1,x,z);
split(z,r-l+1,y,z);
rot(y);
root=merge(x,merge(y,z));
}
else{
int l,r;
cin>>l>>r;
int x=0,y=0,z=0;
split(root,l-1,x,z);
split(z,r-l+1,y,z);
pushdown(y);
cout<<treap[y].mx<<'\n';
root=merge(x,merge(y,z));
}
}
}