首页 > 代码库 > POJ 2352 splay
POJ 2352 splay
再撸一发splay。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <utility>#include <vector>#include <queue>#include <map>#include <set>#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)>(y)?(y):(x))#define INF 0x3f3f3f3f#define MAXN 100005using namespace std;int cnt=1, rt=0;struct Tree{ int key, size, fa, son[2]; void set(int _key, int _size, int _fa) { key=_key; size=_size; fa=_fa; son[0]=son[1]=0; }}T[MAXN];inline void PushUp(int x){ T[x].size=T[T[x].son[0]].size+T[T[x].son[1]].size+1;}inline void Rotate(int x, int p) //0左旋 1右旋{ int y=T[x].fa; T[y].son[!p]=T[x].son[p]; T[T[x].son[p]].fa=y; T[x].fa=T[y].fa; if(T[x].fa) T[T[x].fa].son[T[T[x].fa].son[1] == y]=x; T[x].son[p]=y; T[y].fa=x; PushUp(y); PushUp(x);}void Splay(int x, int To) //将x节点插入到To的子节点中{ while(T[x].fa != To) { if(T[T[x].fa].fa == To) Rotate(x, T[T[x].fa].son[0] == x); else { int y=T[x].fa, z=T[y].fa; int p=(T[z].son[0] == y); if(T[y].son[p] == x) Rotate(x, !p), Rotate(x, p); else Rotate(y, p), Rotate(x, p); } } if(To == 0) rt=x;}void Insert(int key){ if(rt == 0) T[rt = cnt++].set(key, 1, 0); else { int x=rt, y=0; while(x) { y=x; x=T[x].son[key > T[x].key]; } T[x = cnt++].set(key, 1, y); T[y].son[key > T[y].key]=x; Splay(x, 0); }}int GetRank(int key){ if(!rt) return 0; int x=rt, ret=0, y; while(x) { y=x; if(T[x].key <= key) { ret+=T[T[x].son[0]].size+1; x=T[x].son[1]; } else x=T[x].son[0]; } Splay(y, 0); return ret;}int n,m,a[MAXN],u[MAXN],x,y,ans[MAXN];int main(){ scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d%d", &x, &y); ans[GetRank(x)]++; Insert(x); } for(int i=0; i<n; i++) printf("%d\n", ans[i]); return 0;}
听说splay更常用于区间操作,待我再去挖掘。
POJ 2352 splay
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。