bzoj 1251: 序列终结者

题意:

写一个数据结构,支持区间加,区间翻转,求区间最大值。

题解:

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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值