可持久化trie全RE求助
查看原帖
可持久化trie全RE求助
147670
金珂拉楼主2021/5/27 19:38
#include<bits/stdc++.h>
using namespace std;
#define N 200020
template<int lim>
struct trie{

	long long rev(long long x){
long long ans=0;
	for(int i=0;i<lim;++i) ans+=((x>>i)&1)<<(lim-i-1);
	return ans;
    }
int v[N],rt[N],sz[18*N],tot,lf[18*N],ch[2][18*N];
int New(){tot++;return tot;}
int insert(long long x,int r){
	v[r]=x;
	x+=(1<<31);
	x=rev(x);
	rt[r]=New();
	int p=rt[r],q=rt[r-1];
	for(int i=1;i<=lim;i++){
		ch[(x&1)^1][p]=ch[(x&1)^1][q];
		ch[x&1][p]=New();
		sz[ch[x&1][p]]=sz[ch[x&1][q]]+1;
		p=ch[x&1][p];
		q=ch[x&1][q];
		x>>=1;
	}
	lf[p]=r;
}
int kth(int k,int l,int r){
	int q=rt[l-1],p=rt[r];
	while(ch[1][p]  || ch[0][p])
	{
	int temp=sz[ch[0][p]]-sz[ch[0][q]];
		if(temp<k){
			p=ch[1][p];
			q=ch[1][q];
			k-=temp;
		}
		else {
		q=ch[0][q],p=ch[0][p];
		}
	}
	return v[lf[p]];
}
};
trie<34> tr;
int t=0;
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
	int x;
	scanf("%d",&x);
	tr.insert(x,i);
	}
	for(int i=1;i<=m;i++){
		int l,r,k;
		scanf("%d%d%d",&l,&r,&k);
		printf("%d\n",tr.kth(k,l,r));
	}
}
2021/5/27 19:38
加载中...