博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ - 3224 Tyvj 1728 普通平衡树 splay
阅读量:4661 次
发布时间:2019-06-09

本文共 4224 字,大约阅读时间需要 14 分钟。

splay的一道模板题

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;//#pragma GCC optimize(3)//#pragma comment(linker, "/STACK:102400000,102400000") //c++// #pragma GCC diagnostic error "-std=c++11"// #pragma comment(linker, "/stack:200000000")// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)#define lson (l , mid , rt << 1)#define rson (mid + 1 , r , rt << 1 | 1)#define debug(x) cerr << #x << " = " << x << "\n";#define pb push_back#define pq priority_queuetypedef long long ll;typedef unsigned long long ull;typedef pair
pll;typedef pair
pii;typedef pair
p3;//priority_queue
q;//这是一个大根堆q//priority_queue
,greater
>q;//这是一个小根堆q#define fi first#define se second//#define endl '\n'#define OKC ios::sync_with_stdio(false);cin.tie(0)#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行#define REP(i , j , k) for(int i = j ; i < k ; ++i)#define max3(a,b,c) max(max(a,b), c);//priority_queue
, greater
>que;const ll mos = 0x7FFFFFFF; //2147483647const ll nmos = 0x80000000; //-2147483648const int inf = 0x3f3f3f3f;const ll inff = 0x3f3f3f3f3f3f3f3f; //18// const int mod = 10007;const double esp = 1e-8;const double PI=acos(-1.0);const double PHI=0.61803399; //黄金分割点const double tPHI=0.38196601;template
inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x;}/*-----------------------showtime----------------------*/const int maxn = 100000 + 6;struct item { int val, id;}; /// valuebool operator < (item A, item B) { if(A.val != B.val) return A.val < B.val; return A.id < B.id;}namespace splay { item key[maxn]; int fa[maxn], ch[maxn][2], siz[maxn]; int root, ncnt; int dir(int o) { return ch[fa[o]][0]==o ? 0 : 1; } void maintain(int o) { if(o) siz[o] = 1 + siz[ch[o][0]] + siz[ch[o][1]]; } void link(int f, int s, int d) { if(f) ch[f][d] = s; if(s) fa[s] = f; } void rotate(int o) { int f = fa[o], g = fa[f], d = dir(o), s = ch[o][d^1]; /// about s !!! link(g, o, dir(f)); link(o, f, d^1); link(f, s, d); /// g - o * o - f * f - ch[o][d^1] maintain(f); maintain(o); } void splay(int x, int tar) { while(fa[x] != tar) { if(fa[fa[x]] != tar) { if(dir(x) == dir(fa[x])) rotate(fa[x]); else rotate(x); } rotate(x); } if(!tar) root = x; } int rnk(item k) { int o = root, ans = 0, last; while(o) { if(key[o] < k) ans += siz[ch[o][0]] + 1, o = ch[last = o][1]; else o = ch[last = o][0]; } splay(last, 0); return ans + 1; } item kth(int k) { int o = root; while(o) { if(siz[ch[o][0]] + 1 == k) { splay(o, 0); return key[o]; } if(k < siz[ch[o][0]] + 1) o = ch[o][0]; else { k -= siz[ch[o][0]] + 1; o = ch[o][1]; } } return k<=0 ? (item){-2147483647, 0} : (item){ 2147483647, 0}; } void merge(int x, int y) { if(x==0 || y==0) {root = x+y; return;} while(ch[y][0]) { siz[y] += siz[x]; y = ch[y][0]; } siz[y] += siz[x]; link(y, x, 0); splay(x, 0); } void insert(item k) { key[++ ncnt] = k; siz[ncnt] = 1; ch[ncnt][0] = ch[ncnt][1] = 0; if(root == 0) {root = ncnt; return;} int o = root, last; while(o) { ++ siz[last = o]; if(k < key[o]) { o = ch[o][0]; }else{ o = ch[o][1]; } } link(last, ncnt, k
View Code

 

转载于:https://www.cnblogs.com/ckxkexing/p/9737468.html

你可能感兴趣的文章
《美人心计》评论
查看>>
简单的图形学(三)——光源
查看>>
通过Tomcat Manager拿shell
查看>>
Mysql性能优化一:SQL语句性能优化
查看>>
设置EditText是否可编辑
查看>>
Huginn实现自动通过slack推送豆瓣高分电影
查看>>
javascript构造器模式创建对象
查看>>
linux centos7 文件挂载学习记录
查看>>
INSERT
查看>>
五子棋
查看>>
nodejs中路径问题
查看>>
如何从官网下载STM32固件库(转)
查看>>
Android应用-底部导航栏的使用
查看>>
C#中按模板操作Word —— 如何向Word中插入图片
查看>>
异常,finally,gc
查看>>
git基础操作
查看>>
Java基础之J2EE规范
查看>>
How to Cope with Deadlocks
查看>>
base64上传图片
查看>>
2018/12/05 PAT刷题 L1-015 跟奥巴马一起画方块 Java
查看>>