treap修订
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<queue>
#include<cctype>
#include<cstring>
#include<iostream>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define CH for(int d=0;d<=1;d++) if(ch[d])
using namespace std;
const int maxn=+;
struct node{
node*ch[];int r,v,siz;
void init(){r=rand();siz=;ch[]=ch[]=NULL;return;}
void update(){siz=;CH{siz+=ch[d]->siz;}return;}
}treap[maxn],*root[maxn],*nodecnt=treap;
queue<node*>RAM;
node*newnode(){
node*x;
if(!RAM.empty()) x=RAM.front(),RAM.pop();
else x=nodecnt++;
x->init();return x;
}
void del(node*&x){RAM.push(x);return;}
void rotate(node*&x,int d){
node*k=x->ch[d^];x->ch[d^]=k->ch[d];k->ch[d]=x;
x->update();k->update();x=k;return;
}
void insert(node*&x,int v){
if(!x) x=newnode(),x->v=v;
else{
int d=v>x->v;insert(x->ch[d],v);
if(x->ch[d]->r>x->r) rotate(x,d^);
else x->update();
} return;
}
void remove(node*&x,int v){
if(x->v==v){
if(x->ch[]&&x->ch[]){
int d=x->ch[]->r>x->ch[]->r;
rotate(x,d);remove(x->ch[d],v);
}
else{
node*k=x;
if(x->ch[]) x=x->ch[];
else x=x->ch[];
del(k);
}
}
else remove(x->ch[v>x->v],v);
if(x) x->update();return;
}
void print(node*&x){
if(!x) return;
print(x->ch[]);
printf("%d ",x->v);
print(x->ch[]);
return;
}
int find(node*x,int rank){
if(x->siz<rank||rank<) return -;
int kth=x->ch[]?x->ch[]->siz+:;
if(kth==rank) return x->v;
if(kth>rank) return find(x->ch[],rank);
else return find(x->ch[],rank-kth);
}
int fa[maxn];
int findset(int x){return x==fa[x]?x:fa[x]=findset(fa[x]);}
void merge(node*&x,node*&y){
if(!y)return;if(!x){x=y;del(y);return;}
merge(x,y->ch[]);
merge(x,y->ch[]);
insert(x,y->v);
return;
}
void merge(int a,int b){
a=findset(a);b=findset(b);
if(a==b) return;
if(root[a]->siz<root[b]->siz) swap(a,b);
merge(root[a],root[b]);fa[b]=a;
return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline char readc(){
char ch=getchar();
while(!isalpha(ch)) ch=getchar();
return ch;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
int n,q,v[maxn];
void init(){
srand(time());
n=read();q=read();
for(int i=;i<=n;i++) root[i]=newnode(),v[i]=root[i]->v=read(),fa[i]=i;
return;
}
void work(){
int a,b;char tp;
while(q--){
tp=readc();a=read();b=read();
if(tp=='b') merge(a,b);
else if(tp=='q') write(find(root[findset(a)],b)),ENT;
else{
remove(root[findset(a)],v[a]);
insert(root[findset(a)],b);
v[a]=b;
}
}
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
treap修订的更多相关文章
- fhq treap最终模板
新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- 非旋treap模板
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- [普通平衡树treap]【学习笔记】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9046 Solved: 3840[Submit][Sta ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- 【Treap】bzoj1588-HNOI2002营业额统计
一.题目 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司 ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
随机推荐
- 开启.htaccess重写之前先来看看mod_rewrite(转)
Apache的Mode Rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.在大多数情况下,它和.htaccess文件配合使用.就是说,.htaccess文件的一个主要功能 ...
- 挖掘微信Web版通信的全过程
昨天是周末,在家闲得无聊,于是去weiphone.com逛了一圈,偶然发现有人发了一帖叫<微信 for Mac>,这勾起了我的好奇心,国内做Mac开发的人确实很少,对于那些能够独自开发一些 ...
- Linux基础系列—Linux体系结构和Linux内核结构
/** ****************************************************************************** * @author 暴走的小 ...
- 使用CSS、JS的几种方法
引用CSS到html网页里的4种方法 1.行内式 在标记的style属性中设定CSS样式,这种方式本质上没有体现出CSS的优势,因此不推荐使用 <div style="widt ...
- HUD2087
#include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using name ...
- MVC4使用EF6连接mysql数据库
1.需要安装MySql.Data.Entity.EF6,此dll可以在项目——>管理NuGet程序包里联机搜索MySql.Data.Entity.EF6并安装即可 2.连接字符串需要添加prov ...
- HDU3853
题意:给R*C的迷宫,起点为1,1 终点为R,C 且给定方格所走方向的概率,分别为原地,下边,右边,求到终点的期望. 思路:既然是求到终点的期望,那么DP代表期望,所以DP[i][j]=原地的概率*D ...
- get the text value of a selected option.
<select id="myselect"> <option value="1">Mr</option> <optio ...
- iOS和hybird移动端性能
作为一名写了⑦年代码的程序员,目前我最擅长的领域是IOS的客户端开发,在移动领域的开发时间2年. ⑦年前,我刚入行的时候,曾经认为自己将会永远做一个LINUX 服务端C++程序员,于是花了大 ...
- iOS在Cocoa Touch Static Library使用CocoaPods
1.在XCode中新建静态库工程:DDLogLib. 2.添加对外暴露接口的头文件DDLogLibHeader.h 3.命令行进入DDLogLib目录,运行pod init,并修改Podfile 4. ...