SplayTree

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #define KT ch[ch[root][1]][0]
  6 #define LC ch[x][0]
  7 #define RC ch[x][1]
  8 #define N 310001
  9 using namespace std;
 10
 11 struct SplayTree{
 12     int root;
 13     int fa[N];
 14     int ch[N][2];
 15     int sz[N];
 16     int top1;
 17     int top2;
 18     int ss[N];
 19     int que[N];
 20
 21
 22     void rotate(int x,bool f){
 23         int y=fa[x];
 24         int z=fa[y];
 25         pushdown(y);
 26         pushdown(x);
 27         ch[y][!f]=ch[x][f];
 28         fa[ch[x][f]]=y;
 29         fa[x]=z;
 30         if (z) {
 31             ch[z][ch[z][1]==y] = x;
 32         }
 33         ch[x][f] = y;
 34         fa[y]=x;
 35         pushup(y);
 36     }
 37     void splay(int x, int g) {
 38         int y = fa[x];
 39         pushdown(x);
 40         while(y!=g){
 41             int z= fa[y];
 42             bool f = (ch[y][0]==x);
 43             if(z != g && f == (ch[z][0]==y)){
 44                 rotate(y,f);
 45             }
 46             rotate(x,f);
 47             y=fa[x];
 48         }
 49         pushup(x);
 50         if(g==0) root = x;
 51     }
 52     void rotateTo(int k,int g) {
 53         int x=root;
 54         pushdown(x);
 55         while(sz[LC] != k){
 56             if(k<sz[LC]){
 57                 x=LC;
 58             }else{
 59                 k -= sz[LC] + 1;
 60                 x = RC;
 61             }
 62             pushdown(x);
 63         }
 64         splay(x,g);
 65     }
 66     void build(int l,int r,int f){
 67         if(l>r){
 68             return ;
 69         }
 70         int x = (l + r) >> 1;
 71         LC = (x - 1 >= l)? (x - 1 + l)>> 1 :0;
 72         RC = (r >= x + 1)? (x + 1 + r)>> 1 :0;
 73         fa[x] = f;
 74         build(l,x - 1,x);
 75         build(x + 1,r,x);
 76         pushup(x);
 77     }
 78     void erase(int x){
 79         if(x==0)
 80             return;
 81         int father= fa[x];
 82         int head = 0, tail=0;
 83         for(que[tail++] =x ; head < tail; head++){
 84             ss[top2++] = que[head];
 85             if(ch[que[head]][0]){
 86                 que[tail++]=ch[que[head]][0];
 87             }
 88             if(ch[que[head]][1]){
 89                 que[tail++] = ch[que[head]][1];
 90             }
 91         }
 92         ch[father][ch[father][1]==x]=0;
 93         pushup(father);
 94     }
 95     void makeTree(int &x, int l, int r, int f){
 96         if(l > r){
 97             return;
 98         }
 99         int m=(l+r)>>1;
100         newNode(x, m);
101         makeTree(LC,l,m-1,x);
102         makeTree(RC,m+1,r,x);
103         fa[x]=f;
104         pushup(x);
105     }
106     void treaval(int x){
107         if (x) {
108             pushdown(x);
109             treaval(LC);
110             //printf("@%d",val[x]);
111             ans[cnt++]=val[x];
112             treaval(RC);
113         }
114     }
115
116     void newNode(int &x,int c){
117         if(top2){
118             x = ss[--top2];
119         } else {
120             x = ++top1;
121         }
122         LC = RC = fa[x] =0;
123         sz[x] = 1;
124     }
125     void pushdown(int x){
126
127     }
128     void pushup(int x){
129         sz[x]=1+sz[LC]+sz[RC];
130     }
131
132     void debug(){
133         treaval(root);
134         cout<<endl;
135     }
136     void cutTo(int l,int r,int c){
137         rotateTo(l-1,0);
138         rotateTo(r+1,root);
139         int tmp=KT;
140         KT=0;
141         pushup(ch[root][1]);
142         pushup(root);
143
144         rotateTo(c,0);
145         rotateTo(c+1,root);
146         fa[tmp]=ch[root][1];
147         KT=tmp;
148         pushup(ch[root][1]);
149         pushup(root);
150     }
151
152     void init(int n){
153
154         top1=top2=root=0;
155         newNode(root,0);
156         newNode(ch[root][1],0);
157         fa[ch[root][1]]=root;
158         //for(int i=1;i<=n;i++)scanf("%d",&num[i]);
159         makeTree(KT,1,n,ch[root][1]);
160         pushup(ch[root][1]);
161         pushup(root);
162     }
163     void solve(int n,int m){
164
165     }
166
167 }spt;
时间: 2024-08-06 05:49:05

SplayTree的相关文章

查找——清晰图解伸展树SplayTree

伸展树 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它由Daniel Sleator和Robert Tarjan创造,后者对其进行了改进. 假设想要对一个二叉查找树执行一系列的查找操作.为了使整个查找时间更小,被查频率高的那些条目就应当经常处于靠近树根的位置.于是想到设计一个简单方法,在每次查找之后对树进行重构,把被查找的条目搬移到离树根近一些的地方.splaytree应运而生.splaytree是一种自调整形式的二叉查找树,它会沿着从某个节点到树根之间的路径,通过一系列的旋转

ACdream 1104 瑶瑶想找回文串(SplayTree + Hash + 二分)

Problem Description 刚学完后缀数组求回文串的瑶瑶(tsyao)想到了另一个问题:如果能够对字符串做一些修改,怎么在每次询问时知道以某个字符为中心的最长回文串长度呢?因为瑶瑶整天只知道LOL,当他知道自己省选成绩的时候就天天在LOL,导致现在的她实在是太弱了,根本解决不了这个问题,于是就来找你帮忙,么么哒~你就帮帮她吗 Input 第一行为一个长度不超过100000字符串s作为初始字符串.第二行一个正整数n,表示操作/询问的个数.接下来n行,每行有如下几种可能出现的操作/询问:

POJ3468-A Simple Problem with Integers(区间更新 + SegmentTree || SplayTree || BinaryIndexTree)

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

平衡树:Splaytree POJ 3580 SuperMemo

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12788   Accepted: 3986 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game

bzoj4889

http://www.lydsy.com/JudgeOnline/problem.php?id=4889 人傻常数大 bzoj上跑不过 洛谷上能过两到三个点 我写的是树套树啊 怎么跑的比分块还慢 每次可以发现交换两个点 只对他们中间的点有影响 所以我们只用计算比x小的数的和 比x大的数的和 比y小的数的和 比y大的数的和 然后计算一下就可以了 很明显可以用各种数据结构维护 #include<bits/stdc++.h> using namespace std; typedef long lon

【HNOI模拟By lyp】Day1

1 xlk1.1 题目描述给定一棵大小为 n 的无根树,求满足以下条件的四元组 (a, b, c, d) 的个数:1. 1 ≤ a < b ≤ n2. 1 ≤ c < d ≤ n3. 不存在一个点,使得这个点同时在点 a 到点 b 的最短路和点 c 到点 d 的最短路上.1.2 输入格式第一行一个数 n .接下来 n − 1 行,每行两个数 s, t ,表示一条连接 a 和 b 的边.1.3 输出格式输出满足条件的四元组的个数.1.4 样例输入41 22 33 41.5 样例输出21.6 数据

BZOJ3669: [Noi2014]魔法森林

传送门 高级数据结构学傻系列 正解似乎是最短路xjb搞,但是用LCT瞎搞搞也是很吼啊. 从贪心开始,按照每条边a的大小随意sort一下. 对于每个边,我们check两点的联通性,如果联通的话取b最大的值,如果大于当前边的b的话就就删除最大边,把这条边加进去. 如果不连通的话直接添加即可. LCT滋次这些操作,所以大力LCT即可. //BZOJ 3669 //by Cydiater //2017.2.16 #include <iostream> #include <queue> #i

【BZOJ-1493】项链工厂 Splay

1493: [NOI2007]项链工厂 Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1440  Solved: 626[Submit][Status][Discuss] Description T公司是一家专门生产彩色珠子项链的公司,其生产的项链设计新颖.款式多样.价格适中,广受青年人的喜爱. 最近T公司打算推出一款项链自助生产系统,使用该系统顾客可以自行设计心目中的美丽项链.该项链自助生产系统包括硬件系统与软件系统,软件系统与用户进行交互并控制

Splay小结

有关论文: 运用伸展树解决数列维护问题 算法合集之<伸展树的基本操作与应用> splay的伸展操作 splay(x,goal)将x节点移到goal节点的下方,通过左旋和右旋基本操作实现,其实现过程在论文中有详细介绍. 对于用splay去维护一个数列,有以下常用操作. 1.splay(x,goal) 将结点k旋转到goal结点的下方 2.getpos(x) 查询第x个节点的在树中的位置. 3.rotateto(x,goal) 将第x个结点旋转到goal结点下方,可以由 rotateto(x,go