bzoj 3224 普通平衡树
来源:互联网 发布:28周四维彩超标准数据 编辑:程序博客网 时间:2024/05/16 15:31
Treap做法版。。。。
/************************************************************** Problem: 3224 User: Clare Language: C++ Result: Accepted Time:276 ms Memory:3620 kb****************************************************************/ #include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <vector>using namespace std; #define N 100010#define INF 0x7fffffff int n,m,root,tot,Ans;struct Node{ int L,R,size,rnd,value,w;}t[N]; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f;} void Pushup(int k){ t[k].size=t[t[k].L].size+t[t[k].R].size+t[k].w;} void Right_rotate(int &k){ int tmp=t[k].L;t[k].L=t[tmp].R;t[tmp].R=k; t[tmp].size=t[k].size;Pushup(k);k=tmp;} void Left_rotate(int &k){ int tmp=t[k].R;t[k].R=t[tmp].L;t[tmp].L=k; t[tmp].size=t[k].size;Pushup(k);k=tmp;} void Insert(int &k,int x){ if(k==0) { k=++tot;t[k].value=x;t[k].size=t[k].w=1; t[k].rnd=rand();return; } t[k].size++; if(t[k].value==x) t[k].w++; else if(x<t[k].value) { Insert(t[k].L,x); if(t[t[k].L].rnd<t[k].rnd)Right_rotate(k); } else { Insert(t[k].R,x); if(t[t[k].R].rnd<t[k].rnd)Left_rotate(k); }} void Del(int &k,int x){ if(k==0)return; if(t[k].value==x) { if(t[k].w>1) { t[k].w--;t[k].size--;return; } if(t[k].L*t[k].R==0)k=t[k].L+t[k].R; else if(t[t[k].L].rnd<t[t[k].R].rnd) Right_rotate(k),Del(k,x); else Left_rotate(k),Del(k,x); } else if(x>t[k].value) t[k].size--,Del(t[k].R,x); else t[k].size--,Del(t[k].L,x);} int Query(int k,int x){ if(k==0)return 0; int l=t[k].L,r=t[k].R; if(x<=t[l].size) return Query(l,x); else if(x<=t[l].size+t[k].w) return t[k].value; else return Query(r,x-t[l].size-t[k].w);} int Find_rank(int k,int x){ if(k==0)return 0; if(t[k].value==x) return t[t[k].L].size+1; else if(x<t[k].value) return Find_rank(t[k].L,x); else return Find_rank(t[k].R,x)+t[k].w+t[t[k].L].size;} void Query_be(int k,int x){ if(k==0)return; if(t[k].value<x) { Ans=k;Query_be(t[k].R,x); } else Query_be(t[k].L,x);} void Query_af(int k,int x){ if(k==0)return; if(t[k].value>x) { Ans=k;Query_af(t[k].L,x); } else Query_af(t[k].R,x);} int main(){ n=read(); for(int i=1;i<=n;i++) { int kind=read(),x=read(); if(kind==1)Insert(root,x); else if(kind==2)Del(root,x); else if(kind==3)printf("%d\n",Find_rank(root,x)); else if(kind==4)printf("%d\n",Query(root,x)); else if(kind==5) { Ans=0;Query_be(root,x); printf("%d\n",t[Ans].value); } else if(kind==6) { Ans=0;Query_af(root,x); printf("%d\n",t[Ans].value); } } return 0;}
0 0
- bzoj 3224 普通平衡树
- bzoj 3224 普通平衡树
- BZOJ 3224, 普通平衡树
- BZOJ 3224 普通平衡树
- BZOJ 3224 普通平衡树
- 【BZOJ 3224】普通平衡树
- 【BZOJ 3224】普通平衡树
- BZOJ 3224 普通平衡树 Trie树
- [BZOJ][替罪羊树]3224 普通平衡树
- BZOJ 3224: Tyvj 1728 普通平衡树
- BZOJ 3224 Tyvj 1728 普通平衡树
- bzoj 3224: Tyvj 1728 普通平衡树
- 【BZOJ 3224】 Tyvj 1728 普通平衡树
- 3224普通平衡树——bzoj
- bzoj 3224/Tyvj 1728 普通平衡树
- 【splay】BZOJ 3224 普通平衡树
- bzoj 3224,tyvj 1728普通平衡树
- bzoj 3224: Tyvj 1728 普通平衡树
- 初见memcached
- JQuery与HTML元素的获取、设置、添加、删除
- 在子线程中如何使用Handler将一个包含对象的集合传递给主线程
- android application 属性详解
- 文章标题
- bzoj 3224 普通平衡树
- opencv2.4.11在VS2013中的配置(使用配置文件的方式)
- JAVA 序列化 和 反序列化 (Externalizable Serializable) 那些事
- Android客户端与服务器通信方式浅谈
- TableView滑动不加载
- Effective C++(条款35-38)
- linux fedora c sqlite3
- 用linux vconfig配置neutron VLAN试验
- 不看星座和属相,从你喜欢的编程语言看你是什么人