BZOJ 3685 普通van Emde Boas树 zkw线段树

来源:互联网 发布:算法工程师笔试考什么 编辑:程序博客网 时间:2024/05/16 07:54
#include <cstdio>#define N 4000005#define ls(x) (x<<1)#define rs(x) (x<<1|1)#define pa(x) (x>>1)using namespace std;namespace IStream {    char get_char() {        const int L=1<<15;        static char buffer[L];        static char *C,*mempool;        if(C==mempool) mempool=(C=buffer)+fread(buffer,1,L,stdin);        return *C++;    }    int read() {        int re=0;        char c;        do c=get_char();while(c<'0' || c>'9');        while(c>='0' && c<='9') re=(re<<1)+(re<<3)+c-'0',c=get_char();        return re;    }}int n,m,base,s[N];void Insert(int x) {    x+=base;    if(s[x]) return ;    while(x) ++s[x], x>>=1;    return ;}void Delete(int x) {    x+=base;    if(!s[x]) return ;    while(x) --s[x], x>>=1;    return ;}int Querymin() {    if(!s[1]) return 0;    int x=1;    while(x<=base) x=s[ls(x)] ? ls(x) : rs(x);    return x-base;}int Querymax() {    if(!s[1]) return 0;    int x=1;    while(x<=base) x=s[rs(x)] ? rs(x) : ls(x);    return x-base;}int Querylower(int x) {    for(x+=base;x>1;x>>=1)        if((x&1) && s[pa(x)]>s[x]) break;    if(x==1) return 0;    --x;    while(x<=base) x=s[rs(x)] ? rs(x) : ls(x);    return x-base;}int Queryupper(int x) {    for(x+=base;x>1;x>>=1)        if(!(x&1) && s[pa(x)]>s[x]) break;    if(x==1) return 0;    ++x;    while(x<=base) x=s[ls(x)] ? ls(x) : rs(x);    return x-base;}int Queryexist(int x) {    return s[base+x] ? 1 : -1;}int main() {    using namespace IStream;    n=read(), m=read();    base=1;    while(base<=n) base<<=1;    while(m--) {        int mode=read(),x;        if(mode!=3 && mode!=4) x=read()+1;        if(mode==1) Insert(x);        if(mode==2) Delete(x);        if(mode==3) printf("%d\n",Querymin()-1);        if(mode==4) printf("%d\n",Querymax()-1);        if(mode==5) printf("%d\n",Querylower(x)-1);        if(mode==6) printf("%d\n",Queryupper(x)-1);        if(mode==7) printf("%d\n",Queryexist(x));    }    return 0;}
原创粉丝点击