37分 求助玄关 权值线段树
查看原帖
37分 求助玄关 权值线段树
1406678
hky_lightning楼主2024/11/12 15:20

提交记录:https://www.luogu.com.cn/record/188296149

#include<bits/stdc++.h>
#define ll long long  
using namespace std;
const int N=1e5+5;
ll a[N];
ll op,l,r,w,n,m,x,q;
namespace Linetree{
	const int M=N<<5;
	ll tot=1;
	#define lson (lt[cur]=(lt[cur]?lt[cur]:(tot+=1)))
	#define rson (rt[cur]=(rt[cur]?rt[cur]:(tot+=1)))
	#define mid ((L+R)>>1) 
	ll h[M],lt[M],rt[M];
	bool unb(int l,int r,int L,int R) 
	{
		return R<l||L>r;
	}
	void up(int cur)
	{
		h[cur]=h[lson]+h[rson];	
	}
	
	
	void add(int cur,int L,int R,int x,int w) //加入
	{
		if(L==R){
			h[cur]+=w;
			return ;
		}
		if(x<=mid)
			add(lson,L,mid,x,w);
		else 
			add(rson,mid+1,R,x,w);
		up(cur);
	}

	int pim(int cur,int L,int R,int x)
	{
		if(L==R) return 0;
		if(x<=mid)
			return pim(lson,L,mid,x);
		return pim(rson,mid+1,R,x)+h[lson];
	}
	int wpm(int cur,int L,int R,int x){
		if(L==R) return L;
		if(x>=h[lson])
			return wpm(rson,mid+1,R,x-h[lson]);
		return wpm(lson,L,mid,x);
	}
	int pion(int cur,int L,int R,int x){
		if(L==R){
			return L;
		}
		if(h[rson]==0||x<=mid){
			return pion(lson,L,mid,x);
		}
		return pion(rson,mid+1,R,x);
	}
	int sucs(int cur,int L,int R,int x){
		if(L==R){
			return L;
		}
		if(h[lson]==0||x>=mid){
			return sucs(rson,mid+1,R,x);
		}
		return sucs(lson,L,mid,x);
	}
}
using namespace Linetree;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>op>>x;
		if(op==1){
			add(1,1,1e7,x,1);
		}else if(op==2){
			add(1,1,1e7,x,-1);
		}else if(op==3){
			cout<<pim(1,1,1e7,x)+1<<'\n';
		}else if(op==4){
			cout<<wpm(1,1,1e7,x-1)<<'\n';
		}else if(op==5){
			cout<<pion(1,1,1e7,x-1)<<'\n';
		}else{
			cout<<sucs(1,1,1e7,x+1)<<'\n';
		}
	}
	return 0;
}
/*
10
1 106465
1 317721
1 460929
1 644985
1 84185
1 89851
1 492737
5 492737
*/

2024/11/12 15:20
加载中...