【BZOJ2223/3524】[Coci 2009]PATULJCI

时间:2021-12-11 10:50:38

Description

【BZOJ2223/3524】[Coci 2009]PATULJCI

Input

 

Output

10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10

Sample Input

no
yes 1
no
yes 1
no
yes 2
no
yes 3

Sample Output

 

HINT

Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim。

依旧主席树模板,无需离散化。

1<=Lim<=10000

Source

 #include <iostream>
#include <cstdio>
#include <algorithm>
#define N 10000010
using namespace std;
int sum[N],root[],ls[N],rs[N];
int n,m,lim,sz;
void updata(int l,int r,int x,int &y,int v)
{
y=++sz;
sum[y]=sum[x]+;
ls[y]=ls[x]; rs[y]=rs[x];
if (l==r) return;
int mid=(l+r)>>;
if (v<=mid) updata(l,mid,ls[x],ls[y],v);
else updata(mid+,r,rs[x],rs[y],v);
} int query(int L,int R)
{
int l=,r=n,temp=(R-L+)>>,x=root[L-],y=root[R],mid;
while (l<r)
{
if (sum[y]-sum[x]<=temp) return ;
mid=(l+r)>>;
if (sum[ls[y]]-sum[ls[x]]>temp)
{
r=mid; x=ls[x]; y=ls[y];
}
else if (sum[rs[y]]-sum[rs[x]]>temp)
{
l=mid+, x=rs[x]; y=rs[y];
}
else return ;
}
return l;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
updata(,n,root[i-],root[i],a);
}
// scanf("%d",&m);
for (int i=;i<=m;i++)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
int pos=query(aa,bb);
printf("%d\n",pos);
// if (pos==0) printf("no\n");
//else printf("yes %d\n",pos);
}
return ;
}