bzoj2223: [Coci 2009]PATULJCI

本来感觉是道水题,写完各种调...

最后发现是我主席树模板挂了;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
#define mid ((l+r)>>1)
#define LL long long
#define FILE "dealing"
#define up(i,j,n) for(LL i=(j);i<=(n);i++)
#define pii pair<int,int>
char buf[1<<15],*fs,*ft;
int gc(){return (fs==ft)&&(ft=((fs=buf)+fread(buf,1,1<<15,stdin)),fs==ft)?-1:*fs++;}
int read(){
    int x=0,f=1,ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=gc();
    return f*x;
}
const int maxn=303000,inf=100000000,mod=998244353;
int n,m,lim;
int t[maxn];
namespace tree{
    const int maxn=303000<<4;
    int c[maxn][2],cnt=0,d,rt[maxn],sum[maxn],l,r,key;
    inline void updata(int o){sum[o]=sum[c[o][0]]+sum[c[o][1]];}
    void insert(int pre,int& o){
        if(!o)o=++cnt;
        if(l==r){sum[o]=sum[pre]+1;return;}
        d=key>mid;
        c[o][d^1]=c[pre][d^1];
        if(d)l=mid+1;else r=mid;
        insert(c[pre][d],c[o][d]);
        updata(o);
    }
    void init(int* a){
        up(i,1,n)
        key=a[i],l=1,r=lim,insert(rt[i-1],rt[i]);
    }
    int query(int pre,int o){
        if(l==r)return (sum[o]-sum[pre])>key?l:-1;
        d=sum[c[o][0]]-sum[c[pre][0]];
        if(d>key){r=mid;return query(c[pre][0],c[o][0]);}
        else {l=mid+1;return query(c[pre][1],c[o][1]);}
    }
    int Query(int x,int y){
        l=1,r=lim;key=(y-x+1)/2;
        return query(rt[x-1],rt[y]);
    }
};
int main(){
    n=read(),lim=read();
    up(i,1,n)t[i]=read();
    tree::init(t);
    m=read();
    while(m--){
        int x=read(),y=read();
        int ans=tree::Query(x,y);
        if(ans==-1)printf("no\n");
        else printf("yes %d\n",ans);
    }
    return 0;
}

  

posted @ 2017-01-22 17:48  CHADLZX  阅读(148)  评论(0编辑  收藏  举报