MnZn Treap纯模板求调
查看原帖
MnZn Treap纯模板求调
933802
ICU152_lowa_IS8楼主2024/8/5 08:02

调用del函数的时候会RE

#include<bits/stdc++.h>
#define int long long
using namespace std;
int siz[1000005],val[1000005],cnt[1000005],dat[1000005];
int ch[1000005][2];
int tot,root;
inline int New(int v){
	val[++tot]=v;
	siz[tot]=1;
	cnt[tot]=1;
	dat[tot]=rand();
	return tot;
}
inline void pushup(int id){
	siz[id]=siz[ch[id][0]]+siz[ch[id][1]]+cnt[id];
}
inline void build(){
	root=New(-0x3f3f3f3f3f3f3f3f);
	ch[root][1]=New(0x3f3f3f3f3f3f3f3f);
	pushup(root);
}
inline void Rotate(int &id,int d){
	int temp=ch[id][d^1];
	ch[id][d^1]=ch[temp][d];
	ch[temp][d]=id;
	id=temp;
	pushup(ch[id][d]);
	pushup(id);
}
inline void insert(int &id,int v){
//	cout<<id<<endl;
	if(!id){
		id=New(v);
		return;
	}
	if(v==val[id]){
		cnt[id]++;
	}
	else{
		int d=v<val[id]?0:1;
		insert(ch[id][d],v);
		if(dat[ch[id][d]]>dat[id]){
			Rotate(id,d^1);
		}
	}
	pushup(id);
}
inline void del(int &id,int v){
	if(!id){
		return;
	}
//	cout<<id<<endl;
	if(v==val[id]){
		if(cnt[id]>1){
			cnt[id]--;
//			pushup(id)
		}
		else{
			if(ch[id][1]||ch[id][0]){
				if(!ch[id][1]||dat[ch[id][0]]>dat[ch[id][1]]){
					Rotate(id,1);
					del(ch[id][1],v);
				}
				else{
					Rotate(id,0);
					del(ch[id][0],v);
				}
//				cout<<"CHPSID\n";
			}
			else{
				id=0;
//				return;
			}
		}
	}
	else{
		int d=v<val[id]?0:1;
		del(ch[id][d],v);
	}
	pushup(id);
}
inline int getrank(int id,int rank){
//	if(!id)cout<<"JDSIOFJLSKF\n"; 
	if(rank<=siz[ch[id][0]])return getrank(ch[id][0],rank);
	else if(rank<=siz[ch[id][0]]+cnt[id])return val[id];
	else return getrank(ch[id][1],rank-siz[ch[id][0]]-cnt[id]);
}
int a[1000005];
struct node{
	int l,r,k,id,ans;
}ask[1000005];
bool cmp(node a,node b){
	return a.l<b.l;
}
bool ccmp(node a,node b){
	return a.id<b.id;
}
signed main(){
	srand(time(NULL));
	int n,m;
	cin>>n>>m;
	build();
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=m;i++){
		cin>>ask[i].l>>ask[i].r>>ask[i].k;
		ask[i].id=i;
	}
	sort(ask+1,ask+m+1,cmp);
	int l=1,r=0;
	for(int i=1;i<=m;i++){
		while(r<ask[i].r)insert(root,a[++r]);
//		cout<<i<<"ASDSA\n"<<endl;
		while(l<ask[i].l)del(root,a[l--]);
		ask[i].ans=getrank(root,ask[i].k+1);
	}
	sort(ask+1,ask+m+1,ccmp);
	for(int i=1;i<=m;i++){
		cout<<ask[i].ans<<endl;
	}
	return 0;
}
2024/8/5 08:02
加载中...