码迷,mamicode.com
首页 > 其他好文 > 详细

HNOI2017 单旋

时间:2017-12-30 12:06:37      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:节点   date   strong   cut   div   直接   www.   reg   top   

题目描述

网址:https://www.luogu.org/problemnew/show/3721
大意:
有一颗单旋Splay(Spaly),以key值为优先度,总共有5个操作。

  • [1] 插入一个节点,需返还插入后此节点的深度。
  • [2] 把最小点单旋到根,需要返还旋转前此点深度。
  • [3] 把最大点单旋到根,需要返还旋转前此点深度。
  • [4] 把最小点单旋到根,然后删除根,需要返还旋转前此点深度。
  • [5] 把最大点单旋到根,然后删除根,需要返还旋转前此点深度。
    总共有M个操作,数据范围:M <= 100000

题目解法

题目提出的为splay算法,那么正解肯定不是splay。
观察到所有旋转操作都是对极值进行操作的,又是单旋,手玩一下就会发现树的形态基本不改变。
所以,我们可以建立一棵普通二叉树,由于每次修改的点都为常数个。
所以对于修改操作,手动修改即可。
然后考虑查询深度的问题,可以用LCT维护,修改普通二叉树对应的Link、Cut即可维持树的形态。
用LCT查询点u的深度,只需要把u到root的路径变为重路径,查询这棵辅助树的大小即可
最后是插入操作。用set可快速查询前驱与后继。
可以发现一定是插在两者中深度较大的下面,与前面一样直接修改即可。

实现代码

#include<bits/stdc++.h>
#define ll long long
#define RG register
#define IL inline
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
#define maxn 210000
using namespace std;

IL int gi(){  RG int date = 0, m = 1;  RG 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;
}

int root,cnt,M;
int Stk[maxn],ch[maxn][2],fa[maxn],rev[maxn],sz[maxn],fth[maxn];
int tr[maxn][2];

IL bool Son(int x){ return rs(fa[x]) == x; }
IL bool Isroot(int x){return (ls(fa[x])!=x && rs(fa[x])!=x) ; }
IL void PushUp(int x){ sz[x] = 1+sz[ls(x)]+sz[rs(x)]; }
IL void PushDown(int x){
    if(!rev[x])return; rev[x] = 0;
    rev[ls(x)]^=1; rev[rs(x)]^=1;  swap(ls(x) , rs(x));
}
        
IL void Rot(RG int x){
    RG int y = fa[x],z = fa[y],c = Son(x);
    if(!Isroot(y))ch[z][Son(y)] = x; fa[x] = z;
    ch[y][c] = ch[x][!c]; fa[ch[y][c]] = y;
    ch[x][!c] = y; fa[y] = x; PushUp(y);
}
IL void Splay(RG int x){    
    RG int top = 0; Stk[++top] = x;
    for(RG int i = x; !Isroot(i); i = fa[i])Stk[++top] = fa[i];
    while(top)PushDown(Stk[top--]);
    for(RG int y = fa[x]; !Isroot(x); Rot(x),y = fa[x])
        if(!Isroot(y))Son(x) ^ Son(y) ? Rot(x) : Rot(y);
    PushUp(x);
    
}

IL void Access(int x){ 
    for(RG int y = 0; x; y = x,x = fa[x])
         Splay(x),ch[x][1] = y,PushUp(x);
}  
IL void Makeroot(int x){Access(x);Splay(x); rev[x]^=1; }
IL void Split(int x,int y){ Makeroot(x); Access(y); Splay(y); }
IL void Link(int x,int y){ if(!x||!y)return; Makeroot(x); fa[x] = y; }
IL void Cut(int x,int y){
    if(!x||!y)return; 
    Split(x,y); rs(y) = fa[x] = 0; 
    PushUp(x); PushUp(y); 
}
IL int Query(RG int x){Makeroot(root); Access(x); Splay(x); return sz[x];}  


set<int>S; set<int>::iterator bf,aft,it;
map<int,int>mp;
IL void NewNode(){cnt++; fa[cnt] = 0; sz[cnt] = 1;}

IL int Insert(){
    RG int data = gi(); S.insert(data);
    RG int dep,cs,dep1=0,dep2=0,v1,v2;
    bf = S.find(data); aft = bf; aft++;
    NewNode(); mp[data] = cnt;
    if(!root){root = cnt; return 1;}
    if(aft != S.end()){ v1 = mp[*aft]; dep1 = Query(v1); }
    if(bf !=S.begin()){bf--; v2 = mp[*bf]; dep2 = Query(v2);}
    cs = (dep1>dep2) ? v1:v2; dep = max(dep1,dep2);
    if(cs==v1)tr[cs][0] = cnt,fth[cnt] = cs;
    if(cs==v2)tr[cs][1] = cnt,fth[cnt] = cs;
    Link(cs,cnt);  return dep+1;
}

IL int Findmin1(){
    it = S.begin(); RG int p = mp[*it],dep;
    dep = Query(p);
    RG int x = p,sn = tr[x][1],ft = fth[x];
    if(x == root)return 1;
    Cut(x,sn); Cut(ft,x); Link(x,root); Link(ft,sn);
    fth[x] = 0; tr[x][1] = root; fth[root] = x; tr[ft][0] = sn; fth[sn] = ft;
    root = x; return dep;
}

IL int Findmax1(){
    it = S.end(); it--; RG int p = mp[*it],dep;
    dep = Query(p);
    RG int x = p,sn = tr[x][0],ft = fth[x];
    if(x == root)return 1;
    Cut(x,sn); Cut(ft,x); Link(x,root); Link(ft,sn);
    fth[x] = 0; tr[x][0] = root; fth[root] = x; tr[ft][1] = sn; fth[sn] = ft;
    root = x; return dep;
}

IL int Findmin2(){
    RG int dep = Findmin1(),rt = root,sn = tr[root][1];
    fth[sn] = 0;  Cut(root,sn);
    root = sn;
    tr[rt][1] = tr[rt][0] = 0;
    it = S.begin(); S.erase(it);
    return dep;
}

IL int Findmax2(){
    RG int dep = Findmax1(),rt = root,sn = tr[root][0];
    fth[sn] = 0;  Cut(root,sn);
    root = sn;
    tr[rt][1] = tr[rt][0] = 0;
    it = S.end(); it--; S.erase(it); 
    return dep;
}

IL void Work(){
    RG int c = gi(); 
    if(c == 1)printf("%d\n",Insert());
    else if(c == 2)printf("%d\n",Findmin1());
    else if(c == 3)printf("%d\n",Findmax1());
    else if(c == 4)printf("%d\n",Findmin2());
    else if(c == 5)printf("%d\n",Findmax2());
}

int main()
{
    freopen("testdate.in","r",stdin);
    M = gi(); 
    root = 0; while(M--)Work();
    return 0;
}

HNOI2017 单旋

标签:节点   date   strong   cut   div   直接   www.   reg   top   

原文地址:https://www.cnblogs.com/Guess-hahaha/p/8148679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!