splay的板子。。

由于被LCT榨干了。。所以昨天去学了数组版的splay,现在整理一下板子。。

以BZOJ3224和3223为例题。。暂时只有这些,序列的话等有时间把维修序列给弄上来!!

BZOJ 3224 平衡树的操作

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 300005;
int ch[N][2], val[N], size[N], num[N], fa[N], root, tot; int newNode(int p, int v){
val[++tot] = v, fa[tot] = p;
size[tot] = num[tot] = 1, ch[tot][0] = ch[tot][1] = 0;
return tot;
} void push_up(int x){
size[x] = size[ch[x][0]] + size[ch[x][1]] + num[x];
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
fa[x] = z, ch[z][ch[z][1] == y] = x, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x, int goal){
if(x == goal) return;
while(fa[x] != goal){
int y = fa[x], z = fa[y];
if(z != goal){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
push_up(x);
if(goal == 0) root = x;
} void insert(int x){
if(!root){
root = newNode(0, x);
return;
}
int cur = root;
while(ch[cur][x > val[cur]]){
if(val[cur] == x) break;
cur = ch[cur][x > val[cur]];
}
if(val[cur] == x) size[cur] ++, num[cur] ++, splay(cur, 0);
else ch[cur][x > val[cur]] = newNode(cur, x), splay(ch[cur][x > val[cur]], 0);
} void find(int x){
if(!root) return;
int cur = root;
while(val[cur] != x && ch[cur][x > val[cur]])
cur = ch[cur][x > val[cur]];
splay(cur, 0);
} int precursor(int x){
find(x);
if(val[root] < x) return root;
int cur = ch[root][0];
while(ch[cur][1]) cur = ch[cur][1];
return cur;
} int successor(int x){
find(x);
if(val[root] > x) return root;
int cur = ch[root][1];
while(ch[cur][0]) cur = ch[cur][0];
return cur;
} void del(int x){
int pre = precursor(x), suc = successor(x);
splay(pre, 0), splay(suc, root);
int key = ch[suc][0];
if(num[key] > 1) num[key] --, size[key] --, splay(key, 0);
else ch[suc][0] = 0;
push_up(suc), push_up(root);
} int ranks(int x){
find(x);
return size[ch[root][0]] + 1;
} int select(int k){
int cur = root, m = size[ch[cur][0]];
while(1){
if(k > m + num[cur]){
k -= m + num[cur];
cur = ch[cur][1];
}
else{
//cout << m << endl;
if(m >= k) cur = ch[cur][0];
else return val[cur];
}
m = size[ch[cur][0]];
}
} void inOrder(int x){
if(!x) return;
inOrder(ch[x][0]);
cout << val[x] << " ";
inOrder(ch[x][1]);
} int main(){ int n = read();
insert(-INF), insert(INF);
for(int i = 0; i < n; i ++){
int opt = read(), x = read();
if(opt == 1) insert(x);
else if(opt == 2) del(x);
else if(opt == 3) printf("%d\n", ranks(x) - 1);
else if(opt == 4) printf("%d\n", select(x + 1));
else if(opt == 5) printf("%d\n", val[precursor(x)]);
else if(opt == 6) printf("%d\n", val[successor(x)]);
//inOrder(root);puts("");
}
return 0;
}

BZOJ 3223 文艺平衡树,虽然只有一个反转。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 100005;
int ch[N][2], val[N], fa[N], size[N], rev[N], tot, root; int newNode(int p, int v){
val[++tot] = v, fa[tot] = p, size[tot] = 1;
ch[tot][0] = ch[tot][1] = rev[tot] = 0;
return tot;
} void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
} void push_up(int x){
if(!x) return;
size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
} void push_down(int x){
if(rev[x]){
reverse(ch[x][0]), reverse(ch[x][1]);
rev[x] ^= 1;
}
} int buildTree(int p, int l, int r){
if(l > r) return 0;
int mid = (l + r) >> 1;
int cur = newNode(p, mid);
ch[cur][0] = buildTree(cur, l, mid - 1);
ch[cur][1] = buildTree(cur, mid + 1, r);
push_up(cur);
return cur;
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
push_down(y), push_down(x);
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
fa[x] = z, ch[z][ch[z][1] == y] = x, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x, int goal){
if(x == goal) return;
while(fa[x] != goal){
int y = fa[x], z = fa[y];
if(z != goal){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
rotate(y);
}
rotate(x);
}
push_up(x);
if(goal == 0) root = x;
} int find(int k){
if(!root) return 0;
int cur = root, m = size[ch[cur][0]];
while(1){
push_down(cur);
if(m < k){
k -= m + 1;
cur = ch[cur][1];
}
else{
if(m >= k + 1) cur = ch[cur][0];
else return cur;
}
m = size[ch[cur][0]];
}
} void re(int l, int r){
int x = find(l - 1), y = find(r + 1);
splay(x, 0), splay(y, root);
reverse(ch[ch[root][1]][0]);
} void inOrder(int x){
if(!x) return;
push_down(x);
inOrder(ch[x][0]);
if(val[x]) printf("%d ", val[x]);
inOrder(ch[x][1]);
} int main(){ int n = read(), m = read();
root = newNode(0, 0), ch[root][1] = newNode(root, 0);
ch[ch[root][1]][0] = buildTree(ch[root][1], 1, n);
while(m --){
int l = read(), r = read();
re(l, r);
}
inOrder(root);
return 0;
}

个人整理的数组splay板子,指针的写的太丑了就不放了。。的更多相关文章

  1. C语言基础知识点整理(函数/变量/常量/指针/数组/结构体)

    函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  2. php整理(二): 数组

    数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...

  3. char[]数组与char *指针的区别

    char[]数组与char *指针的区别 问题描述 虽然很久之前有看过关于char指针和char数组的区别,但是当时没有系统的整理,到现在频繁遇到,在string,char[], char *中迷失了 ...

  4. 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用

    [源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, con ...

  5. C/C++中数组转换成指针的情况

    数组转换成指针:在大多数用到数组的表达式中,数组自动转换成指向数组首元素的指针.比如: int ia[10]; int *p = ia; //ia转换成指向数组首元素的指针 以下情况上述转换不会发生: ...

  6. 【C语言】12-指向一维数组元素的指针

    一.用指针指向一维数组的元素 1 // 定义一个int类型的数组 2 int a[2]; 3 4 // 定义一个int类型的指针 5 int *p; 6 7 // 让指针指向数组的第0个元素 8 p ...

  7. php部分--数组(包含指针思想遍历数组);

    1.创建并输出数组 (1)相同数据类型的数组$attr=array(1,2,3,4,5); print_r($attr); echo "<br>"; $sttr1=ar ...

  8. C/C++数组名与指针的区别详解

    1.数组名不是指针我们看下面的示例: #include <iostream> int main() { ]; char *pStr = str; cout << sizeof( ...

  9. C语言中字符数组和字符串指针分析

    这几天搞Unix上的C程序,里面用到了很多字符数组和字符串指针,我记得在学完C语言后相当一段时间里,对指针这个东西还是模模糊糊,后来工作也没怎么 用到过C,虽然网上这类的文章也有很多,还是决定自己在这 ...

随机推荐

  1. Taro开发微信小程序的初体验

    了解Taro 听说Taro是从几个星期前开始的,在一次饭桌上,一个小伙伴说:"Hey, 你听说了Taro么,听说只需要写一套程序就可以生成H5,小程序以及RN的代码模板,并且类似于React ...

  2. 剑指offer 第十二天

    58.对称的二叉树 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. /* public class TreeNode { int val ...

  3. Python_每日习题_0003_完全平方数

    # 题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? # 程序分析 因为168对于指数爆炸来说实在太小了,所以可以直接省略数学分析,用最朴素的方法来获取 ...

  4. Day15 Python基础之logging模块(十三)

    参考源:http://www.cnblogs.com/yuanchenqi/articles/5732581.html logging模块 (****重点***) 一 (简单应用) import lo ...

  5. TCP 原理

    一.分组交换网络   古老的电话通信,一根电缆,两个用户设备通信 计算机中的两个设备节点通信:分组网络 计算机网络采取分组交换技术,意思就是我有[一块数据]要发给对方,那我会把这[一块数据]分成N份[ ...

  6. JMeter学习FTP测试计划(转)

    FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划的例子. 1.创建一个线程组 2.线程组--->添加--->配置元件 ...

  7. [转帖]LCD与LED的区别之背光原理与优缺点对比介绍

    LCD与LED的区别之背光原理与优缺点对比介绍 http://m.elecfans.com/article/620376.html 时下液晶面板与液晶电视技术已经达到炉火纯青的境界,并已经成为大屏幕平 ...

  8. java从request中获取GET和POST请求参数

    URL和参数列表 一 获取请求方式 request.getMethod(); get和post都可用, 二 获取请求类型 request.getContentType(); get和post都可用,示 ...

  9. NIO和经典IO

    NIO未必更快,在Linux上使用Java6完成的测试中,多线程经典I/O设计胜出NIO30%左右 异步I/O强于经典I/O:服务器需要支持超大量的长期连接,比如10000个连接以上,不过各个客户端并 ...

  10. python之路--触发器, 储存过程, 事务

    一. 触发器 使用触发器可以定制用户对某一张表的数据进行 [增, 删  ,改] 操作时前后的行为, (注意 没有查询),在进行增删改的时候出发的某个动作叫做 触发器. 其实就是在增删改的时候另外执行了 ...