P4146
这题和文艺平衡树差不了多少 但是换了一个处理区间的方式 常数竟然小了一点 下次卡常就用这种方式了 之所以会小 是因为和之前相切的方式来讲 这里这样切的长度小一点
自然期望时间复杂度也小了一点
维护两个lazy标记 区间不断down下去即可
注意区间加我们在外面就得维护
但是swap是针对子树的 所以必须要传进去才能进行swap
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 50025;
int root,r1,r2,r3,r4,cnt = 0;
struct fhqtp
{
int ch[2],size,rd,val,maxn,lazy,col;
}t[MAX_N];
int Max(int a,int b){return a > b?a:b;}
namespace fhq
{
void up(int rt)
{
t[rt].size = t[t[rt].ch[0]].size+t[t[rt].ch[1]].size+1;
t[rt].maxn = t[rt].val;
if(t[rt].ch[0]) t[rt].maxn = Max(t[rt].maxn,t[t[rt].ch[0]].maxn);
if(t[rt].ch[1]) t[rt].maxn = Max(t[rt].maxn,t[t[rt].ch[1]].maxn);
}
void down(int x)
{
if(t[x].col)
{
swap(t[x].ch[0],t[x].ch[1]);
if(t[x].ch[0]) t[t[x].ch[0]].col^=1;
if(t[x].ch[1]) t[t[x].ch[1]].col^=1;
t[x].col = 0;
}
if(t[x].lazy)
{
if(t[x].ch[0]) t[t[x].ch[0]].lazy += t[x].lazy,t[t[x].ch[0]].maxn+=t[x].lazy,t[t[x].ch[0]].val+=t[x].lazy;
if(t[x].ch[1]) t[t[x].ch[1]].lazy += t[x].lazy,t[t[x].ch[1]].maxn+=t[x].lazy,t[t[x].ch[1]].val+=t[x].lazy;
t[x].lazy = 0;
}
up(x);
}
void split(int x,int k,int &a,int &b)
{
if(!x) {a = b = 0;return;}
down(x);
if(k<=t[t[x].ch[0]].size) b = x,split(t[x].ch[0],k,a,t[x].ch[0]);
else a = x,split(t[x].ch[1],k-t[t[x].ch[0]].size-1,t[x].ch[1],b);
up(x);
}
int Merge(int x,int y)
{
if(x==0||y==0) return x + y;
if(t[x].rd<t[y].rd)
{
down(x);
t[x].ch[1] = Merge(t[x].ch[1],y);
up(x);return x;
}
else
{
down(y);
t[y].ch[0] = Merge(x,t[y].ch[0]);
up(y);return y;
}
}
int newnode(int val)
{
t[++cnt].val = val;t[cnt].rd = rand();t[cnt].size = 1;t[cnt].maxn = val;
return cnt;
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m,opt,x,y,v;srand(19260817);
scanf("%d%d",&n,&m);
for(int i = 1;i<=n;++i) root = fhq::Merge(root,fhq::newnode(0));
while(m--)
{
scanf("%d%d%d",&opt,&x,&y);
if(opt==1)
{
scanf("%d",&v);
fhq::split(root,x-1,r1,r2);
fhq::split(r2,y-x+1,r2,r3);
t[r2].maxn+=v;
t[r2].lazy+=v;
t[r2].val+=v;
root = fhq::Merge(fhq::Merge(r1,r2),r3);
}
else if(opt==2)
{
fhq::split(root,x-1,r1,r2);
fhq::split(r2,y-x+1,r2,r3);
t[r2].col^=1;
root = fhq::Merge(fhq::Merge(r1,r2),r3);
}
else
{
fhq::split(root,x-1,r1,r2);
fhq::split(r2,y-x+1,r2,r3);
printf("%d\n",t[r2].maxn);
root = fhq::Merge(fhq::Merge(r1,r2),r3);
}
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}