首页 > 其他分享 >Splay 板子

Splay 板子

时间:2024-04-09 15:25:00浏览次数:20  
标签:cur rs int 板子 Splay il key ls

普通:

bool _Start;
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define Tp template<typename T>
#define Ts template<typename T,typename... _T>
Tp il void read(T& x) {
	x=0;bool f=0;char c=getchar();
	for(;!isdigit(c);c=getchar()) f|=c=='-';
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	x=(f?-x:x);
}Ts il void read(T& x,_T&... y) {read(x),read(y...);}
const int N=1e5+5;
int n;
struct Splay {
	int s[2],fa,siz,key;
}t[N];int rt,idx;
#define ls(x) t[x].s[0]
#define rs(x) t[x].s[1]
#define fa(x) t[x].fa
il int newnode(int x) {t[++idx].key=x,t[idx].siz=1;return idx;}
il void upd(int x) {t[x].siz=t[ls(x)].siz+t[rs(x)].siz+1;}
il void clear(int x) {ls(x)=rs(x)=fa(x)=t[x].siz=t[x].key=0;}
il bool get(int x) {return x==rs(fa(x));}
il void rotate(int x) {
	int y=fa(x),z=fa(y),c=get(x);
	if(t[x].s[c^1]) fa(t[x].s[c^1])=y;
	t[y].s[c]=t[x].s[c^1],t[x].s[c^1]=y,fa(y)=x,fa(x)=z;
	if(z) t[z].s[y==t[z].s[1]]=x;
	upd(y),upd(x);
}
il void splay(int x) {
	for(int f;f=fa(x);rotate(x))
		if(fa(f)) rotate(get(f)==get(x)?f:x);
	rt=x;
}
il void ins(int x) {
	int p=rt,f=0;
	while(p) {
		f=p;
		p=t[p].s[x>t[p].key];
	}
	p=newnode(x),fa(p)=f,t[f].s[x>t[f].key]=p;
	splay(p);
}
il void del(int x) {
	int p=rt,f=0;
	while(p&&t[p].key!=x) {
		f=p;
		p=t[p].s[x>t[p].key];
	}
	if(!p) {
		splay(f);
		return ;
	}
	splay(p);int cur=ls(p);
	if(!cur) {
		rt=rs(p),fa(rt)=0,clear(p);
		return ;
	}
	while(rs(cur)) cur=rs(cur);
	rs(cur)=rs(p),fa(rs(p))=cur,fa(ls(p))=0,clear(p);
	upd(cur),splay(cur);
}
il int rnk(int x) {
	int ans=1,p=rt,f;
	while(p) {
		f=p;
		if(t[p].key<x) ans+=t[ls(p)].siz+1,p=rs(p);
		else p=ls(p);
	}
	return splay(f),ans;
}
il int kth(int k) {
	int p=rt;
	while(p) {
		int siz=t[ls(p)].siz+1;
		if(siz==k) break;
		if(siz>k) p=ls(p);
		else k-=siz,p=rs(p);
	}
	return splay(p),t[p].key;
}
il int pre(int x) {
	int p=rt,ans,f;
	while(p) {
		f=p;
		if(t[p].key>=x) p=ls(p);
		else ans=t[p].key,p=rs(p);
	}
	return splay(f),ans;
}
il int suc(int x) {
	int p=rt,ans,f;
	while(p) {
		f=p;
		if(t[p].key<=x) p=rs(p);
		else ans=t[p].key,p=ls(p);
	}
	return splay(f),ans;
}
bool _End;
int main() {
	fprintf(stderr,"Memory: %.4lf Mib\n",abs(&_End-&_Start)/1048576.0);
	read(n);
	for(int i=1;i<=n;i++) {
		int op,x;read(op,x);
		if(op==1) ins(x); 
		if(op==2) del(x);
		if(op==3) printf("%d\n",rnk(x));
		if(op==4) printf("%d\n",kth(x));
		if(op==5) printf("%d\n",pre(x));
		if(op==6) printf("%d\n",suc(x));
	}
	return 0;
}


文艺:

bool _Start;
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define Tp template<typename T>
#define Ts template<typename T,typename... _T>
Tp il void read(T& x) {
	x=0;bool f=0;char c=getchar();
	for(;!isdigit(c);c=getchar()) f|=c=='-';
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	x=(f?-x:x);
}Ts il void read(T& x,_T&... y) {read(x),read(y...);}
const int N=1e5+5;
int n,m;
int a[N];
struct Splay {
	int s[2],fa,siz,key,lz;
}t[N];int idx,rt;
#define ls(x) t[x].s[0]
#define rs(x) t[x].s[1]
#define fa(x) t[x].fa
il int newid(int x) {t[++idx].key=x,t[idx].siz=1;return idx;}
il void upd(int x) {t[x].siz=t[ls(x)].siz+t[rs(x)].siz+1;}
il bool get(int x) {return x==t[fa(x)].s[1];}
il void pushdown(int x) {
	if(!t[x].lz) return ;
	swap(ls(x),rs(x));
	t[ls(x)].lz^=1,t[rs(x)].lz^=1;
	t[x].lz=0;
}
il void rotate(int x) {
	int y=fa(x),z=fa(y),c=get(x);
	if(t[x].s[c^1]) fa(t[x].s[c^1])=y;
	t[y].s[c]=t[x].s[c^1],t[x].s[c^1]=y,fa(y)=x,fa(x)=z;
	if(z) t[z].s[y==t[z].s[1]]=x;
	upd(y),upd(x); 
}
il void splay(int x,int y) {
	for(int f;(f=fa(x))!=y;rotate(x))
		if(fa(f)!=y) rotate(get(f)==get(x)?f:x);
	if(!y) rt=x;
}
il int bt(int l,int r) {
	if(l>r) return 0;
	int p=++idx,mid=(l+r)>>1;
	t[p].key=mid,t[p].siz=1;
	ls(p)=bt(l,mid-1);
	rs(p)=bt(mid+1,r);
	fa(ls(p))=fa(rs(p))=p;
	upd(p);
	return p;
}
il void ins(int x) {
	int p=rt,f=0;
	while(p) {
		f=p;
		p=t[p].s[x>t[p].key];
	}
	p=newid(x),fa(p)=f,t[f].s[x>t[f].key]=p;
	splay(p,0);
}
il int kth(int x) {
	int p=rt;x++;
	while(p) {
		pushdown(p);
		int siz=t[ls(p)].siz+1;
		if(siz==x) return p;
		else if(siz>x) p=ls(p);
		else x-=siz,p=rs(p);
	}
}
il void myreverse(int l,int r) {
	int x=kth(l-1),y=kth(r+1);
	splay(x,0),splay(y,x);
	t[ls(y)].lz^=1;
}
void traversal(int x) {
	pushdown(x);
	if(ls(x)) traversal(ls(x));
	if(t[x].key>0&&t[x].key<=n) printf("%d ",t[x].key);
	if(rs(x)) traversal(rs(x));
}
bool _End;
int main() {
	fprintf(stderr,"Memory: %.4lf Mib\n",abs(&_End-&_Start)/1048576.0);
	read(n,m);
	rt=bt(0,n+1);
	while(m--) {
		int l,r;read(l,r);
		myreverse(l,r);
	}
	traversal(rt);
	return 0;
}

标签:cur,rs,int,板子,Splay,il,key,ls
From: https://www.cnblogs.com/kbzcz/p/18124029/splay

相关文章

  • 字符串哈希板子
    #include<iostream>#include<cstring>#defineMAX_SIZE100usingnamespacestd;classStringHash{public:intsize;char*array;char*array_forward;unsignedlonglong*pre_base;unsignedlonglong*hash_array;uns......
  • 二分板子
    二分板子#include<iostream>constexprintN=100;intmain(){inta[N];intn;std::cin>>n;for(inti=0;i<n;i++)std::cin>>a[i];intl=0,r=n-1;intt;std::cin>>t;while(l<r){......
  • 【漏洞复现】宏景人力资源信息管理系统 DisplayExcelCustomReport 任意文件读取漏洞
    免责声明:文章来源互联网收集整理,文章仅供参考,此文所提供的信息只为网络安全人员对自己所负责的网站、服务器等(包括但不限于)进行检测或维护参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作。利用此文所提供的信息而造成的直接或间接后果和损失,均由使用者......
  • 数论的各种板子2.0 (约数和 和 约数个数和)
    ​#include<bits/stdc++.h>//求约数和#include<map>usingnamespacestd;constintmod=1e9+10;typedeflonglongll;intmain(){ intn; cin>>n; unordered_map<int,int>primes; while(n--){ intx; cin>>x; for(inti=2......
  • Wpf Combobox display multiple fields columns properties
    <ComboBoxGrid.Row="0"x:Name="cbx"VirtualizingPanel.VirtualizationMode="Recycling"HorizontalAlignment="Stretch"VerticalContentAlignment="Center"FontSize="30"Selec......
  • 数论的各种板子1.0
    仅为了记录所学的知识.boolis_prime(intx){//求是否为素数 if(x<2)returnfalse; for(inti=2;i<=x/i;++i){ if(x%i==0)returnfalse; } returntrue;}voiddivide(intx){//分解质因子 for(inti=2;i<=x/i;++i){ ints=0; while(x%i==0)x/=i,s++; cou......
  • 状压dp板子(cf div4 #937)
    #include<bits/stdc++.h>usingnamespacestd;intn;vector<int>v[20];stringa[20],b[20];booldp[500010][20];voiddfs(ints,intnow){dp[s][now]=true;for(autonxt:v[now]){if(s&(1<<nxt))continue;......
  • kmp板子
    书上讲的感觉不好理解,不如算法竞赛上分析的题目链接:https://www.luogu.com.cn/problem/P3375贴板子:#include<iostream>#include<vector>#include<algorithm>#include<math.h>#include<sstream>#include<string>#include<string.h>#include<i......
  • Ubuntu下本机向minicom板子传文件操作
    1.配置minicomlingd@ubuntu:~$  sudominicom-s    出现这样的配置界面:       +-----[configuration]------+       |Filenamesandpaths   |       |Filetransferprotocols |       |Serialport......
  • 图论板子
    链式前向星的存储模板#include<iostream>#include<vector>#include<algorithm>#include<math.h>#include<sstream>#include<string>#include<string.h>#include<iomanip>#include<stdlib.h>#include<map>#inclu......