#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修订的更多相关文章

  1. fhq treap最终模板

    新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...

  2. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  3. BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]

    1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1318  Solved: 498[Submit][ ...

  4. 非旋treap模板

    bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...

  5. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  6. [普通平衡树treap]【学习笔记】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9046  Solved: 3840[Submit][Sta ...

  7. UVALive5031 Graph and Queries(Treap)

    反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...

  8. 【Treap】bzoj1588-HNOI2002营业额统计

    一.题目 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司 ...

  9. hdu 4585 Shaolin treap

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

随机推荐

  1. SafeNet推出行业首款白盒password软件保护解决方式

    数据保护领域的全球率先企业SafeNet公司日前宣布,推出行业首款採用白盒安全技术的的软件保护方案.SafeNet 圣天诺 软件授权与保护解决方式如今纳入了新的功能,可在"白盒" ...

  2. [Javascript] String method: endsWith() && startsWith()

    With endsWith && startsWith, you can easily find out whether the string ends or starts with ...

  3. 鼠标双击范围基于Win7

    TP鼠标双击范围主要是修改Registry中的HKEY_CURRENT_USER\Control Panel\Mouse里面的两个参数 DoubleClickHeight跟DoubleClickWid ...

  4. python-Pickle序列化

    [Python之旅]第三篇(二):Pickle序列化   python 序列化 pickle 摘要: 说明:关于Pickle的说明     作如下说明: 1 2 3 4 5 6 7 序列化的概念很简单 ...

  5. Android 连接 SQL Server (jtds方式)——上

    本文将介绍开发Android程序,连接SQL Server,通过第三方包jtds的方式. 如果你有同样的需求,请跟着做一遍,博主将以最详细的方式,进行介绍. 首先说明,Java.Android连接SQ ...

  6. ViewPagerindicator 源码解析

        ViewPagerindicator 源码解析   1. 功能介绍 1.1 ViewPagerIndicator ViewPagerIndicator用于各种基于AndroidSupportL ...

  7. IOS 开发 【序】

    首先说说环境的搭建: 需要有一台搭载开发环境的电脑 其实简单的设备就行,不过好的设备会提高开发效率. 有了设备,剩下的就是需要集成开发环境. 去 app store 下载最新的 xcode. 安装上x ...

  8. DataTable数据与Excel表格的相互转换

    using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...

  9. angular-ui-tree

    angular-ui-tree的github项目地址:https://github.com/angular-ui-tree/angular-ui-tree DEMO目录结构如下: bootstrap. ...

  10. 简单总结焦点事件、Event事件对象、冒泡事件

    每学习一些新的东西,要学会复习,总结和记录. 今天来简单总结一下学到的几个事件:焦点事件.Event事件对象.冒泡事件 其实这几个事件应该往深的说是挺难的,但今天主要是以一个小菜的角度去尝试理解一些基 ...