51nod1175 区间中第K大的数

裸的主席树。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
}
const int nmax=5e4+5;
struct node{
	node *l, *r;int sm;
};
node *rt[nmax],ns[nmax*20],*pt=ns;
int a[nmax],b[nmax];
node* build(int l,int r){
	node *p=pt++;p->sm=0;
	if(l==r) return p;
	int mid=(l+r)>>1;
	p->l=build(l,mid);p->r=build(mid+1,r);
	return p;
}
node* update(int x,int add,node *t,int l,int r){
	node *p=pt++;p->sm=t->sm+add;
	if(l==r) return p;
	int mid=(l+r)>>1;
	if(x<=mid) p->r=t->r,p->l=update(x,add,t->l,l,mid);
	else p->l=t->l,p->r=update(x,add,t->r,mid+1,r);
	return p;
}
int query(node *t,node *s,int x,int l,int r){
	if(l==r) return l;
	int mid=(l+r)>>1;
	if(t->l->sm-s->l->sm>=x) return query(t->l,s->l,x,l,mid);
	return query(t->r,s->r,x-t->l->sm+s->l->sm,mid+1,r);
}
int main(){
	int n=read(),u,v,d;
	rep(i,1,n) a[i]=b[i]=read();
	sort(b+1,b+n+1);
	int cnt=unique(b+1,b+n+1)-b-1;
	rt[0]=build(1,cnt);
	rep(i,1,n) {
		u=lower_bound(b+1,b+cnt+1,a[i])-b;
		rt[i]=update(u,1,rt[i-1],1,cnt);
	}
	int m=read();
	rep(i,1,m){
		u=read()+1,v=read()+1,d=read();
		printf("%d
",b[query(rt[v],rt[u-1],v-u-d+2,1,cnt)]);
	}
	return 0;
}

  

基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
 收藏
 关注
一个长度为N的整数序列,编号0 - N - 1。进行Q次查询,查询编号i至j的所有数中,第K大的数是多少。
例如: 1 7 6 3 1。i = 1, j = 3,k = 2,对应的数为7 6 3,第2大的数为6。
 
Input
第1行:1个数N,表示序列的长度。(2 <= N <= 50000)
第2 - N + 1行:每行1个数,对应序列中的元素。(0 <= S[i] <= 10^9)
第N + 2行:1个数Q,表示查询的数量。(2 <= Q <= 50000)
第N + 3 - N + Q + 2行:每行3个数,对应查询的起始编号i和结束编号j,以及k。(0 <= i <= j <= N - 1,1 <= k <= j - i + 1)
Output
共Q行,对应每一个查询区间中第K大的数。
Input示例
5
1
7
6
3
1
3
0 1 1
1 3 2
3 4 2
Output示例
7
6
1
相关问题
区间中最大的数 
0
 
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5861209.html