BZOJ3685: 普通van Emde Boas树

SB 题 然而一直RE  发现是数组开小了


#include<cstdio>
#include<iostream>
using namespace std;
 
char c;
inline void read(int &a)
{
    a=0;do c=getchar();while(c<'0'||c>'9');
    while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}
int Rec[6000001];
int con;
inline void Del(int x)
{
    Rec[x]?con--:con;
    Rec[x]=0;
    while((!Rec[x^1])&&x^1)
       x>>=1,Rec[x]=0; 
}
inline void insert(int x)
{
     
    Rec[x]?con:con++;
    Rec[x]=1;
    while((!Rec[x^1])&&x^1)
       x>>=1,Rec[x]=1; 
}
int base,n;
inline int Pre(int x)
{
    int data=-1;
    while(x^1)
      if(x&1)
         if(Rec[x^1])break;
        else x>>=1;
     else x>>=1;
    x^=1;
    if(!x)
      return -1;
    while(x<base+n)
     if(Rec[x<<1|1])x=x<<1|1;
        else x=x<<1;
    x>>=1;
    return x-base;
}
 
inline int Aft(int x)
{
    int data=-1;
    while(x^1)
      if(x&1)x>>=1;
      else if(Rec[x^1])break;
       else x>>=1;
    x^=1;
    if(!x)
      return -1;
    while(x<base+n)
     if(Rec[x<<1])x=x<<1;
        else x=x<<1|1;
    x>>=1;
    return x-base;
}
inline int Has(int x)
{return Rec[x]?1:-1;}
inline void print(int a)
{
    if(a<0)putchar('-'),a=-a;
    if(a<10){putchar('0'+a),putchar('\n');return;    }
    int base=1;
    while(base<=a)base=(base<<3)+(base<<1);
    base/=10;
    while(base)putchar('0'+a/base),a%=base,base/=10;
    putchar('\n');
}
 
inline int Min()
{
    if(con==0)return -1;
     int x=1;
     while(x<base+n)
        if(Rec[x<<1])
     x<<=1;else x=x<<1|1;
     x>>=1;
     return x-base; 
}
 
 
inline int Max()
{
 
    if(con==0)return -1;
     int x=1;
     while(x<base+n)
        if(Rec[x<<1|1])
             x=x<<1|1;
     else x=x<<1;
     x>>=1;
     return x-base; 
}
 
int main()
{
     
    base=1058576;
    read(n);
    int m;
    int x,y;
    read(m);
    while(m--)
     {
        read(x);
        if(x==1)
           read(y),insert(base+y);
        else if(x==2)
           read(y),Del(base+y);
        else if(x==3)
           print(Min());
        else if(x==4)
           print(Max());
        else if(x==5)
           read(y),print(Pre(y+base));
        else if(x==6)
           read(y),print(Aft(y+base));
        else if(x==7)
           read(y),print(Has(y+base));
     }
    return 0;
}


你可能感兴趣的:(BZOJ3685: 普通van Emde Boas树)