题意:
写一个数据结构,支持区间加,区间翻转,求区间最大值。
题解:
splay裸题不解释,拿来练手。
这么就没写居然能一次过编译+1A也是很感动。
code:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
const LL inf=1<<28;
struct trnode{
LL son[2],fa,d,c,max,rev,u;
}tr[50010];LL tot=0,root;
void update(LL x)
{
LL lc=tr[x].son[0],rc=tr[x].son[1];
tr[x].c=tr[lc].c+tr[rc].c+1;
if(tr[x].rev)
{
tr[x].rev=0;swap(tr[x].son[0],tr[x].son[1]);
tr[lc].rev^=1;tr[rc].rev^=1;
}
if(tr[x].u)
{
LL u=tr[x].u;tr[x].u=0;
if(lc) tr[lc].max+=u,tr[lc].d+=u,tr[lc].u+=u;
if(rc) tr[rc].max+=u,tr[rc].d+=u,tr[rc].u+=u;
}
tr[x].max=max(tr[x].d,max(tr[lc].max,tr[rc].max));
}
void add(LL d,LL fa,LL w)
{
LL x=++tot;
tr[x].d=d;tr[x].c=1;tr[x].fa=fa;tr[x].max=d;
tr[fa].son[w]=x;
}
LL findid(LL k)
{
LL x=root;
while(1)
{
update(x);
LL lc=tr[x].son[0],rc=tr[x].son[1];
if(k<=tr[lc].c) x=lc;
else if(tr[lc].c+1<k) x=rc,k-=(tr[lc].c+1);
else break;
}
return x;
}
void pushdown(LL x)
{
if(tr[x].fa) pushdown(tr[x].fa);
update(x);
}
void rotate(LL x)
{
LL y=tr[x].fa,z=tr[y].fa,w,R,r;
w=tr[y].son[0]==x;
R=y;r=tr[x].son[w];tr[R].son[1-w]=r;if(r) tr[r].fa=R;
R=z;r=x;tr[R].son[tr[z].son[1]==y]=r;tr[r].fa=R;
R=x;r=y;tr[R].son[w]=r;tr[r].fa=R;
update(y);update(x);
}
void splay(LL x,LL fa)
{
pushdown(x);
while(tr[x].fa!=fa)
{
LL y=tr[x].fa,z=tr[y].fa;
if(z==fa) rotate(x);
else
if((tr[y].son[0]==x)==(tr[z].son[0]==y)) rotate(y),rotate(x);
else rotate(x),rotate(x);
}
if(fa==0) root=x;
}
void build(LL fa,LL w,LL l,LL r)
{
LL mid=(l+r)/2;
add(0,fa,w);LL x=tot;
if(l<mid) build(x,0,l,mid-1);
if(r>mid) build(x,1,mid+1,r);
update(x);
}
void solve(LL l,LL r)
{
LL x=findid(l);splay(x,0);
LL y=findid(r+2);splay(y,x);
}
LL n,m;
int main()
{
scanf("%lld %lld",&n,&m);
tr[0].d=tr[0].max=-inf;
add(-inf,0,0);root=tot;add(-inf,1,1);
build(2,0,1,n);update(2);update(1);
for(LL i=1;i<=m;i++)
{
LL op;scanf("%lld",&op);
if(op==1)
{
LL l,r,c;scanf("%lld %lld %lld",&l,&r,&c);
solve(l,r);LL p=tr[tr[root].son[1]].son[0];
tr[p].d+=c;tr[p].max+=c;tr[p].u+=c;
}
if(op==2)
{
LL l,r;scanf("%lld %lld",&l,&r);
solve(l,r);LL p=tr[tr[root].son[1]].son[0];
tr[p].rev^=1;
}
if(op==3)
{
LL l,r;scanf("%lld %lld",&l,&r);
solve(l,r);LL p=tr[tr[root].son[1]].son[0];
printf("%lld\n",tr[p].max);
}
}
}