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 ...
随机推荐
- 第一个 Python 程序 - Email Manager Demo
看了一些基础的 Python 新手教程后,深深感觉到 Python 的简洁与强大,这是我的第一个 Python Demo.下面是完整代码与执行截图. 代码: # encoding: utf-8 ''' ...
- S3C2440 I2C总线控制
概述:话不多说,直接上图 多主机IIC总线控制(IICCON): IIC控制总线状态(IICSTAT): IIC总线地址(IICADD): IIC发送,接收总线寄存器(IICDS) IIC总线控制寄存 ...
- iOS工具种之16进制颜色转为UIColor
#define DEFAULT_VOID_COLOR [UIColor whiteColor] + (UIColor *)colorWithHexString:(NSString *)stringT ...
- win32 api 文件操作!
CreateFile打开文件要对文件进行读写等操作,首先必须获得文件句柄,通过该函数可以获得文件句柄,该函数是通向文件世界的大门. ReadFile从文件中读取字节信息.在打开文件获得了文件句柄之后, ...
- codevs4189字典(字典树)
/* 本字典树较弱 只支持插入单词 查询单词. 特殊的 bool变量w 标记此字母是不是某个单词的结束 (然而这个题并没卵用) */ #include<iostream> #include ...
- 获取Html中所有img的src
/// <summary> /// 获取所有Img中的Src /// </summary> /// <param name="htmlText"> ...
- 设置cookie倒计时让让表单自动提交
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%&g ...
- Ci 简单分页,保证能实现
某晚,自己写项目的时候去找资料,关于CI分页的, 发现百度出来的前几名的基本都是写的都是垃圾, 要么是实现不了,要么就是坑逼 所以我自己在这里写一个,不是很完美,只是说是简单的实现了原理 有了最基本的 ...
- JNI type
ref: JNI type The mapping between the Java type and C type is: Type Signature Java Type Z boolean B ...
- iOS开发中常用的宏
前言 今天将一些简化工程代码的宏定义拿出来分享一下,自定义一些宏可以有效的简化代码,提高编码效率. Application #define APPLICATION [UIApplication sha ...