• BZOJ 1251 Splay维护序列


    思路:
    splay维护序列的裸题
    啊woc调了一天
    感谢yzy大佬的模板……

    //By SiriusRen
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 55555
    #define inf 0x3f3f3f3f
    int n,m,root,cnt,size[N],ch[N][2],fa[N],v[N],mx[N],rev[N],tag[N];
    int build(int l,int r){
        if(l>r)return 0;
        if(l==r){size[l]=1;return l;}
        int mid=(l+r)>>1;
        ch[mid][0]=build(l,mid-1);fa[ch[mid][0]]=mid;
        ch[mid][1]=build(mid+1,r);fa[ch[mid][1]]=mid;
        size[mid]=size[ch[mid][0]]+size[ch[mid][1]]+1;
        return mid;
    }
    void push_up(int x){
        mx[0]=-inf;
        size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
        mx[x]=max(mx[ch[x][0]],max(mx[ch[x][1]],v[x]));
    }
    void push_down(int x){
        if(rev[x]){rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,swap(ch[x][0],ch[x][1]);rev[x]^=1;}
        if(tag[x]){
            tag[ch[x][0]]+=tag[x],tag[ch[x][1]]+=tag[x];
            mx[ch[x][0]]+=tag[x],mx[ch[x][1]]+=tag[x];
            v[ch[x][0]]+=tag[x],v[ch[x][1]]+=tag[x];
            tag[x]=0;
        }
    }
    void rotate(int p){
        int q=fa[p],y=fa[q],x=(ch[q][1]==p);
        ch[q][x]=ch[p][!x],fa[ch[q][x]]=q;
        ch[p][!x]=q;fa[q]=p;fa[p]=y;
        if(y){
            if(ch[y][0]==q)ch[y][0]=p;
            else ch[y][1]=p;
        }push_up(q);
    }
    void Splay(int x,int tp){
        for(int y;y=fa[x];rotate(x)){
            if(y==tp)break;
            if(fa[y]!=tp){
                if((ch[y][0]==x)^(ch[fa[y]][0]==y))rotate(x);
                else rotate(y);
            }
        }push_up(x);
        if(!tp)root=x;
    }
    int find(int x,int num){
        push_down(x);
        if(size[ch[x][0]]+1==num)return x;
        else if(size[ch[x][0]]+1>num)return find(ch[x][0],num);
        else return find(ch[x][1],num-size[ch[x][0]]-1);
    }
    void add(int l,int r,int wei){
        int y=find(root,l);
        Splay(y,0),Splay(find(root,r+2),y);
        int V=ch[ch[root][1]][0];
        tag[V]+=wei,v[V]+=wei;mx[V]+=wei;
    }
    void reverse(int l,int r){
        int y=find(root,l);
        Splay(y,0),Splay(find(root,r+2),y);
        rev[ch[ch[root][1]][0]]^=1;
    }
    void get_max(int l,int r){
        int y=find(root,l);
        Splay(y,0),Splay(find(root,r+2),y);
        printf("%d
    ",mx[ch[ch[root][1]][0]]);
    }
    int main(){
        int op,l,r,wei;
        scanf("%d%d",&n,&m);
        root=build(1,n+2);
        mx[0]=v[1]=v[n+2]=mx[1]=mx[n+2]=-inf;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&op,&l,&r);
            if(op==1)scanf("%d",&wei),add(l,r,wei);
            else if(op==2)reverse(l,r);
            else get_max(l,r);
        }
    }

    这里写图片描述

  • 相关阅读:
    Redis笔记 —— string 篇
    解决跨域请求无法携带Cookie的问题
    Mysql事务学习笔记
    Redis笔记 —— hash 篇
    mysql视图的简单学习
    axios无法被识别为ajax请求的解决方案
    常见header信息详解
    int 15h
    操作系统有进程了
    是时候走我自己的路线了,真正的做我自己的Jinux
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532121.html
Copyright © 2020-2023  润新知