个人整理的数组splay板子,指针的写的太丑了就不放了。。
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板子,指针的写的太丑了就不放了。。的更多相关文章
- C语言基础知识点整理(函数/变量/常量/指针/数组/结构体)
函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- php整理(二): 数组
数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...
- char[]数组与char *指针的区别
char[]数组与char *指针的区别 问题描述 虽然很久之前有看过关于char指针和char数组的区别,但是当时没有系统的整理,到现在频繁遇到,在string,char[], char *中迷失了 ...
- 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用
[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, con ...
- C/C++中数组转换成指针的情况
数组转换成指针:在大多数用到数组的表达式中,数组自动转换成指向数组首元素的指针.比如: int ia[10]; int *p = ia; //ia转换成指向数组首元素的指针 以下情况上述转换不会发生: ...
- 【C语言】12-指向一维数组元素的指针
一.用指针指向一维数组的元素 1 // 定义一个int类型的数组 2 int a[2]; 3 4 // 定义一个int类型的指针 5 int *p; 6 7 // 让指针指向数组的第0个元素 8 p ...
- php部分--数组(包含指针思想遍历数组);
1.创建并输出数组 (1)相同数据类型的数组$attr=array(1,2,3,4,5); print_r($attr); echo "<br>"; $sttr1=ar ...
- C/C++数组名与指针的区别详解
1.数组名不是指针我们看下面的示例: #include <iostream> int main() { ]; char *pStr = str; cout << sizeof( ...
- C语言中字符数组和字符串指针分析
这几天搞Unix上的C程序,里面用到了很多字符数组和字符串指针,我记得在学完C语言后相当一段时间里,对指针这个东西还是模模糊糊,后来工作也没怎么 用到过C,虽然网上这类的文章也有很多,还是决定自己在这 ...
随机推荐
- 六、Xadmin忘记密码
1.如果用的是django自带的User模块,忘记了超级用户的密码,可以通过以下方法找回密码: 终端进入项目根目录,然后输入如下命令: python manage.py shell 然后在python ...
- heb Daz
Asatras soi bib Daz! gos la haik ri, dewoa gos mi haik quri. soi Fong d cuup va ti Chusan, imps Dabo ...
- Atcoder E - Knapsack 2 (01背包进阶版 ex )
E - Knapsack 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...
- python_超级基础
初识计算机 CPU 计算机的大脑.中央处理单元,主要负责数据运算及计算,是运算计算中心. 存储器 内存 临时存储数据,供CPU运算使用. 优点: 读取速度快. 缺点: 容量小,成本高,断电即消失. 硬 ...
- 【学习总结】C-翁恺老师-入门-第0周<程序设计与C>
[学习总结]C-翁恺老师-入门-总 1-首先按视频说的下载编辑器 <DevC++> 并一路默认设置: 安装包下载链接 (我有vc6.0不过预感告诉我老师要用类似CS50里那种命令行编辑器? ...
- 解决scrapy报错:ModuleNotFoundError: No module named 'win32api'
ModuleNotFoundError: No module named 'win32api' 表示win32api未安装 解决办法: 下载对应python版本的win32api,并安装. 下载地址: ...
- C#复习笔记(3)--C#2:解决C#1的问题(结束C#2的内容:最后一些特性)
结束C#2的内容:最后一些新性 这是本章要讲的内容: 分部类型:可以在多个源文件中为 一个类型编写代码. 特别适用于部分代码是自动生成, 而其他部分的代码为手写的类型. 静态类:对工具类进行整理, 以 ...
- day 7-7 线程池与进程池
一. 进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多,这 ...
- Spring boot + mybatis + orcale实战(干货)
废话少说,直接上步骤: 第一步:安装好IDEA(此处省略) 第二步:在IDEA新建springboot工程 第三步:在springboot工程的pom.xml添加oracle和mybait依赖 < ...
- keyvalue对RDD s
scala> val input =sc.textFile("/home/simon/SparkWorkspace/test.txt")input: org.apache.s ...