[bzoj1251]序列终结者

网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。

最近没打splay手生了 练手

#include<iostream>
#include<cstdio>
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

int n,m;
int rt,fa[50005],c[50005][2],size[50005],mx[50005],val[50005],num[50005];
bool rev[50005],tag[50005];

inline void update(int x)
{
    int l=c[x][0],r=c[x][1];
    size[x]=size[l]+size[r]+1;
    mx[x]=max(num[x],max(mx[l],mx[r]));
}

void pushdown(int x)
{

    int l=c[x][0],r=c[x][1];
    if(rev[x])
    {
        rev[l]^=1;rev[r]^=1;rev[x]^=1;
        swap(c[x][0],c[x][1]);
    }
    if(tag[x])
    {
    //    cout<<"pushdown"<<x<<" to "<<l<<" "<<r<<endl;
        if(l){num[l]+=val[x];val[l]+=val[x];mx[l]+=val[x];}
        if(r){val[r]+=val[x];num[r]+=val[x];mx[r]+=val[x];}
        tag[l]=tag[r]=1;tag[x]=0;val[x]=0;
        update(x);
    }
}

void rotate(int x,int&k)
{
    int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    if(y!=k) c[z][c[z][1]==y]=x;
    else k=x;
    fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
    c[y][l]=c[x][r];c[x][r]=y;
    update(y);update(x);
}

void splay(int x,int&k)
{
    while(x!=k)
    {
        int y=fa[x],z=fa[y];
        if(y!=k)
        {
            if(c[z][1]==y^c[y][1]==x) rotate(x,k);
            else rotate(y,k); 
        }
        rotate(x,k);
    }
}

void build(int&x,int l,int r,int last)
{
    if(l>r)return;
    x=l+r>>1;fa[x]=last;
    build(c[x][0],l,x-1,x);
    build(c[x][1],x+1,r,x);
    update(x);
}

int find(int x,int rk)
{
    pushdown(x);
    int l=c[x][0];
//    cout<<"find"<<x<<" "<<rk<<" "<<size[x]<<endl;
    if(size[l]+1==rk) return x;
    if(size[l]+1<rk) return find(c[x][1],rk-size[l]-1);
    else return find(c[x][0],rk);
}

int split(int l,int r)
{
    splay(find(rt,l),rt);
    splay(find(rt,r),c[rt][1]);
//    cout<<"split"<<c[c[rt][1]][0]<<endl;
    return c[c[rt][1]][0];    
}

void dfs(int x)
{
    printf("%d %d %d %d %d %d %d\n",x,size[x],mx[x],num[x],val[x],rev[x],tag[x]);
    if(c[x][0]) dfs(c[x][0]);
    if(c[x][1]) dfs(c[x][1]);
}

int main()
{
    n=read();m=read();mx[0]=-2000000000;
    build(rt,1,n+2,0); 
    for(int i=1;i<=m;i++)
    {
        int t=read(),l=read(),r=read();
        int y=split(l,r+2);
        if(t==1)
        {
            int x=read();//cout<<x<<endl;
            tag[y]=1;val[y]+=x;num[y]+=x;mx[y]+=x;
        }
        else
        if(t==2)
            rev[y]^=1;
        else
            printf("%d\n",mx[y]);
    //    dfs(rt); 
    }
    return 0;
}

 

posted @ 2017-03-24 11:15  FallDream  阅读(243)  评论(0编辑  收藏  举报