【主席树】bzoj3524 [Poi2014]Couriers

删除了作死的建树函数。

#include<cstdio>
using namespace std;
#define N 500001
struct Node{int v,lc,rc;}T[N*21];
int root[N],e;
void Insert(int pre,int cur,int p,int l,int r)
{
	if(l==r)
	  {
	  	T[cur].v=T[pre].v+1;
	  	return;
	  }
	int m=(l+r>>1);
	if(p<=m)
	  {
	  	T[cur].lc=++e;
	  	T[cur].rc=T[pre].rc;
	  	Insert(T[pre].lc,T[cur].lc,p,l,m);
	  }
	else
	  {
	  	T[cur].rc=++e;
	  	T[cur].lc=T[pre].lc;
	  	Insert(T[pre].rc,T[cur].rc,p,m+1,r);
	  }
	T[cur].v=T[T[cur].lc].v+T[T[cur].rc].v;
}
int Goal;
int Query(int L,int R,int l,int r)
{
	if(l==r) return l;
	int m=(l+r>>1);
	if(T[T[R].lc].v-T[T[L].lc].v>Goal) return Query(T[L].lc,T[R].lc,l,m);
	else if(T[T[R].rc].v-T[T[L].rc].v>Goal) return Query(T[L].rc,T[R].rc,m+1,r);
	return 0;
}
int n,m;
int main()
{
	int x,y;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
	  {
	  	scanf("%d",&x);
	  	root[i]=++e;
	  	Insert(root[i-1],root[i],x,1,n);
	  }
	for(;m;--m)
	  {
	  	scanf("%d%d",&x,&y);
	  	Goal=(y-x+1>>1);
	  	printf("%d\n",Query(root[x-1],root[y],1,n));
	  }
	return 0;
}
posted @ 2015-04-13 19:53  AutSky_JadeK  阅读(254)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト