标签:style blog http color io os ar for art
再撸一发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 100005 using 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更常用于区间操作,待我再去挖掘。
标签:style blog http color io os ar for art
原文地址:http://www.cnblogs.com/Mathics/p/3970504.html