1. 程式人生 > >【treap】【bzoj 3224】: Tyvj 1728 普通平衡樹

【treap】【bzoj 3224】: Tyvj 1728 普通平衡樹

加上了前驅和後繼

//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
#define LS T[o].l
#define RS T[o].r
inline const int getint()
{
    int r=0,k=1;char c=getchar();
    for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
    for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';
    return k*r;
}
/////////////////////////////////////////////////
int n,root,sz,ans;
struct data{int l,r,s,cnt,rnd,w;}T[100001];
/////////////////////////////////////////////////
void update(int o){T[o].s=T[LS].s+T[RS].s+T[o].cnt;}
void r_rot(int &o){int t=LS;LS=T[t].r;T[t].r=o;T[t].s=T[o].s;update(o);o=t;}
void l_rot(int &o){int t=RS;RS=T[t].l;T[t].l=o;T[t].s=T[o].s;update(o);o=t;}
void insert(int &o,int x)
{
	if(o==0)
	{
		o=++sz;T[o].s=T[o].cnt=1;T[o].rnd=rand()*rand();T[o].w=x;
		return;
	}
	T[o].s++;
	if(x<T[o].w)
	{
		insert(LS,x);
		if(T[o].rnd>T[LS].rnd) r_rot(o);
	}
	else if(x>T[o].w)
	{
		insert(RS,x);
		if(T[o].rnd>T[RS].rnd) l_rot(o);
	}
	else T[o].cnt++;
}
void del(int &o,int x)
{
	if(o==0)return;
	if(T[o].w==x)
	{
		if(T[o].cnt>1) T[o].s--,T[o].cnt--;
		else if(LS==0||RS==0)o=LS+RS;
		else if(T[LS].rnd<T[RS].rnd) r_rot(o),del(o,x);
		else l_rot(o),del(o,x);
		return;
	}
	T[o].s--;
	if(T[o].w<x) del(RS,x);
	else del(LS,x);
}
int getrank(int o,int x)
{
	if(o==0) return 0;
	if(T[o].w==x) return T[LS].s+1;
	else if(T[o].w<x) return T[LS].s+T[o].cnt+getrank(RS,x);
	else return getrank(LS,x);
}
int getnum(int o,int x)
{
	if(o==0) return 0;
	if(x<=T[LS].s) return getnum(LS,x);
	else if(x>T[LS].s+T[o].cnt) return getnum(RS,x-T[LS].s-T[o].cnt);
	else return T[o].w;
}
void getbefore(int o,int x)
{
	if(o==0) return;
	if(T[o].w<x) ans=T[o].w,getbefore(RS,x);
	else getbefore(LS,x);
}
void getafter(int o,int x)
{
	if(o==0) return;
	if(T[o].w>x) ans=T[o].w,getafter(LS,x);
	else getafter(RS,x);
}
/////////////////////////////////////////////////
void input()
{
	srand(13131);
    scanf("%d",&n);
}
void solve()
{
	int op;
	int x;
    while(n--)
    {
    	scanf("%d%d",&op,&x);
    	if(op==1) insert(root,x);
    	else if(op==2) del(root,x);
    	else if(op==3) printf("%d\n",getrank(root,x));
    	else if(op==4) printf("%d\n",getnum(root,x));
    	else if(op==5) ans=0,getbefore(root,x),printf("%d\n",ans);
    	else if(op==6) ans=0,getafter(root,x),printf("%d\n",ans);
    }
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    input();
    solve();
    return 0;
}


相關推薦

treapbzoj 3224: Tyvj 1728 普通平衡

加上了前驅和後繼 //#define _TEST _TEST #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #inc

Treap[BZOJ 3224]Tyvj 1728 普通平衡 & 非旋轉實現

#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define maxn 2000005 struct Treap{

BZOJ3224: Tyvj 1728 普通平衡

mes closed hid splay scanf 輸出 div spa opened 【題意】 1. 插入x數 2. 刪除x數(若有多個相同的數,因只刪除一個) 3. 查詢x數的排名(若有多個相同的數,因輸出最小的排名) 4. 查詢排名為x的數 5. 求x的前驅(前驅定

BZOJ3224 Tyvj 1728 普通平衡 平衡模板

好吧,我承認,我患有帕金森:手賤啊,打錯一個字元,把if (t[k].w<x)打成了if (t[k].w>x),結果只得了20分。(淚流滿面,55555……) 這道題考察的是平衡樹的模板,只有一些平衡樹的基本操作,相信只要對平衡樹有一些瞭解的同學就能把這題A掉。

bzoj 3224Tyvj 1728 普通平衡

Tyvj 1728 普通平衡樹 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 11767 Solved: 5021 [Submit][Status][Discuss] Description 您需要

Splaybzoj3224 Tyvj 1728 普通平衡

insert std mes dma space ota stream highlight can #include<cstdio> #include<iostream> #include<cstring> #include<al

bzoj 3224: Tyvj 1728 普通平衡

print href 5% lin des pac amp lower tyvj Description 您需要寫一種數據結構(可參考題目標題),來維護一些數,其中需要提供以下操作:1. 插入x數2. 刪除x數(若有多個相同的數,因只刪除一個)3. 查詢x數的排名(若有多

bzoj 3224 Tyvj 1728 普通平衡

大小 題目 tput sub 最大的 數據結構 color see 相同 Tyvj 1728 普通平衡樹 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17187 Solved: 7501[Submit][Statu

[bzoj] 3224 Tyvj 1728 普通平衡 || 平衡板子題

pan out mark blog make class while post bzoj #!/bin/bash g++ make.cpp -o make -Wall g++ 1.cpp -o ac -Wall g++ sb.cpp -o sb -Wall while

bzoj 3224: Tyvj 1728 普通平衡 && loj 104 普通平衡 (splay

break lov pri pac erase names std namespace uniq 題目鏈接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay樹模板題: 推薦博客:h

BZOJ - 3224 Tyvj 1728 普通平衡 splay

uil switch color rect 技術分享 onclick open des del splay的一道模板題 #include <algorithm> #include <iterator> #include <io

3224: Tyvj 1728 普通平衡

兩個 數據 std ont log 答案 數字 spl printf 3224: Tyvj 1728 普通平衡樹 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 14480 Solved: 6275 Descript

3224: Tyvj 1728 普通平衡(新板子)

pri ati urn 多個 stdin 目標 一個 題目 page 3224: Tyvj 1728 普通平衡樹 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17048 Solved: 7429[Submit][S

7.Bzoj3224: Tyvj 1728 普通平衡(Treap)

Bzoj3224: Tyvj 1728 普通平衡樹(Treap) 終於自己敲出來了Treap。。。。。。拿來當板子還是挺好用的. 操作1,顯然根據二叉搜尋樹的性質直接建就好了. 操作2,如果有兩個兒子,則把這個點下旋下去,直到只有一個兒子或者沒有. 操作3,查詢比他小的數,記錄一個size,表示這個點有多少

Tyvj 1728 普通平衡

查詢 pre 序號 med esc desc 5% 其中 數據結構 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 13242 Solved: 5675[Submit][Status][Discuss] Descripti

bzoj3224 Tyvj 1728 普通平衡

stat 基本 class rotate reg color space ins view 傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 【題解】 寫起來跟*一樣,但是還是挺快調出來了。 主要就是每個數可以

[補檔][Tyvj 1728]普通平衡

name logs 題目 esp 最小 || 後繼 printf names 題目 您需要寫一種數據結構(可參考題目標題),來維護一些數,其中需要提供以下操作:  1. 插入x數  2. 刪除x數(若有多個相同的數,因只刪除一個)  3. 查詢x數的排名(若有多個相同的數,

絕對是全網最好的Splay 入門詳解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡 包教包會

平衡樹是什麼東西想必我就不用說太多了吧。 百度百科:     一個月之前的某天晚上,yuli巨佬為我們初步講解了Splay,當時接觸到了平衡樹裡的旋轉等各種騷操作,感覺非常厲害。而第二天我調Splay的模板竟然就搞了一天,最後還是失敗告終,只能CV了事,而Splay也成了我心中的一個心

bzoj3224Tyvj 1728 普通平衡 treap

amp turn clas algo 。。 pac rip problem upd 3224: Tyvj 1728 普通平衡樹Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17706 Solved: 7764[Submit

模板bzoj-3224普通平衡(splay&treap&SBT)

先吐槽一下,要找塊好模板真不容易,畢竟大家都有各自的程式碼風格,強迫自己去看非自己風格的程式碼真是痛苦,所以在網上新增一種型別的模板 最近才發現這題A了三遍,分別是以三種平衡樹的模板題A的,所以稍微彙總一下,讓後來者有個型別的模板可以依靠 相信看模板的人都看