【悬2关】20pts求调Ac on#1,2
查看原帖
【悬2关】20pts求调Ac on#1,2
373198
WhileTrueRP楼主2023/11/26 14:39
#include<bits/stdc++.h> 
#define mid ((l+r)>>1)
using namespace std;
const int N = 2e5+5;
int a[N],tmp[N],root[N];
struct node{
	int l,r;
	int sum;
}tree[N<<5];
int tot = 0;
bool cmp(int x,int y){
	return x < y;
}
int build(int l,int r){
	int p = ++tot;
	if(l == r){
		return p;
	}
	tree[p].l = build(l,mid);
	tree[p].r = build(mid+1,r);
	return p;
}
int change(int now,int l,int r,int x){
	int p = ++tot;
	tree[p] = tree[now];
	if(l == r){
		tree[p].sum = 1;
		return p;
	}
	if(x <= mid){
		tree[p].l = change(tree[now].l,l,mid,x);
	}else{
		tree[p].r = change(tree[now].r,mid+1,r,x);
	}
	tree[p].sum = tree[tree[p].l].sum + tree[tree[p].r].sum;
	return p;
}
int solve(int p1,int p2,int l,int r,int x){
//	printf("(%d,%d,%d,%d)",l,r,tree[])
	if(l == r){
		return r;
	}
	int t = tree[tree[p2].l].sum - tree[tree[p1].l].sum;
//	printf("(%d,%d,%d,%d)",l,r,x,t);
	if(t >= x){
		return solve(tree[p1].l,tree[p2].l,l,mid,x);
	}else{
		return solve(tree[p1].r,tree[p2].r,mid+1,r,x-t);
	}
}
int main(){
//	freopen("1.txt","r",stdin);
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		tmp[i] = a[i];
	}
	sort(tmp+1,tmp+1+n,cmp);
	int t = unique(tmp+1,tmp+1+n) - tmp-1;
//	cout<<t<<endl;
	for(int i=1;i<=n;i++){
		a[i] = upper_bound(tmp+1,tmp+1+t,a[i])-tmp-1;
//		printf("%d\n",a[i]);
	}
	root[0] = build(1,n);
	for(int i=1;i<=n;i++){
		root[i] = change(root[i-1],1,n,a[i]);
	}
	for(int i=1;i<=n;i++){
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		printf("%d\n",tmp[solve(root[l-1],root[r],1,t,x)]);
	}
}
2023/11/26 14:39
加载中...