【主席树】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;
}
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/

浙公网安备 33010602011771号
