个人整理的数组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,虽然网上这类的文章也有很多,还是决定自己在这 ...
随机推荐
- RPM包制作过程(一)
本机环境:centos7,64位 1. 首先安装工具,rpmbuild可能在rpmdevtools里已经包含 #yum install rpm-devel.x86_64 #yum install rp ...
- 爬虫(三)之scrapy核心组件
01-核心组件 ·五大核心组件的工作流程: 引擎(Scrapy) 用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler) 用来接受引擎发过来的请求, 压入队列中, 并在引擎 ...
- 第4次oo作业
作业概述 作业1:多项式加法 第一次作业理解上并不困难,简言之是一个多项式合并同类项,但对于我这个第一次使用java进行编程的小白,还是充满了血和泪. 在这次课程之前,我稍微对java有一些了解,但也 ...
- Django之admin中管理models中的表格
Django之admin中管理models中的表格 django中使用admin管理models中的表格时,如何将表格注册到admin中呢? 具体操作就是在项目文件夹中的app文件夹中的admin中注 ...
- MYSQL行号
mysql 实现行号的方法——如何获取当前记录所在行号 - senly - 博客园http://www.cnblogs.com/xinlei/archive/2011/12/16/2290349.ht ...
- sqlServer问题记录
1.sql 2008 无法绑定由多个部分绑定的标示符 连接中的多个表中存在同名字段,通过设置别名访问即可 2.远程无法连接到sqlserver 计算机管理->服务与应用程序->SQL Se ...
- phantomjs 了解
转自:http://www.cnblogs.com/lei0213/ PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引擎.它原生支持多种web 标准:DOM 操作,CSS选择器,JS ...
- tailf、tail -f、tail -F三者区别(转)
tail -f 等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止 tail -F 等同于--follow=name --retry,根 ...
- react中如何使用动画效果
在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...
- C++加载动态库的形式来实现封装
目录结构 └── test ├── CMakeLists.txt ├── base.h //设置接口 ├── drive.cpp //具体实现 └── main.cpp //test CMakeLis ...