bzoj2223 [Coci 2009]PATULJCI(同bzoj3524)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 500010
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,a[N],rt[N],owo=0;
struct node{
int x,lc,rc;
}tree[N*30];
inline void change(int &p,int l,int r,int x){
tree[++owo]=tree[p];p=owo;tree[p].x++;
if(l==r) return;int mid=l+r>>1;
if(x<=mid) change(tree[p].lc,l,mid,x);
else change(tree[p].rc,mid+1,r,x);
}
inline int ask(int p1,int p2,int l,int r,int x){
if(tree[p2].x-tree[p1].x<x) return 0;
if(l==r) return l;int mid=l+r>>1;
int val=tree[tree[p2].lc].x-tree[tree[p1].lc].x;
if(val>=x) return ask(tree[p1].lc,tree[p2].lc,l,mid,x);
else return ask(tree[p1].rc,tree[p2].rc,mid+1,r,x);
}
int main(){
// freopen("a.in","r",stdin);
n=read();int nn=read();
for(int i=1;i<=n;++i) a[i]=read();m=read();
for(int i=1;i<=n;++i){
rt[i]=rt[i-1];change(rt[i],1,nn,a[i]);
}while(m--){
int l=read(),r=read();
int ans=ask(rt[l-1],rt[r],1,nn,(r-l+1>>1)+1);
if(!ans) puts("no");
else printf("yes %d\n",ans);
}return 0;
}
PATULJCI线段树区间查询

本文介绍了一个基于线段树实现的区间查询算法,用于解决Coci2009竞赛中的PATULJCI问题。通过动态开点的方式维护区间内元素的数量,并能够快速查询指定区间内的第k大元素。
155

被折叠的 条评论
为什么被折叠?



