Cod sursa(job #2112102)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 23 ianuarie 2018 00:06:56
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 6.53 kb
#include <fstream>

#define PRIM1 21313
#define PRIM2 19937
#define D_MAX 1000010

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__       FORTIS FORTUNA ADIUVAT       __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

class parser{
    public:
        parser() {}
        parser(const char *file_name){
            input_file.open(file_name,ios::in | ios::binary);
            input_file.sync_with_stdio(false);
            index&=0;
            input_file.read(buffer,SIZE);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n&=0;
            //sign&=0;
            //sign|=(buffer[index-1]=='-');
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=(n<<1)+(n<<3)+buffer[index]-'0';
            //n^=((n^-n)&-sign);
            return *this;}
        ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x400000; ///4MB
        char buffer[SIZE];
        int index/*,sign*/;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class writer{
    public:
        writer() {};
        writer(const char *file_name){
            output_file.open(file_name,ios::out | ios::binary);
            output_file.sync_with_stdio(false);
            index&=0;}
        inline writer &operator <<(int target){
            aux&=0;
            n=target;
            if (!n)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=n%10+'0';
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline writer &operator <<(const char *target){
            aux&=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        fstream output_file;
        static const int SIZE=0x200000; ///2MB
        int index,aux,n;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index&=0,output_file.write(buffer,SIZE);}
};

parser f ("hashuri.in");
writer t ("hashuri.out");

int v[D_MAX];

inline void fasterLLDivMod(unsigned long long x, unsigned y, unsigned &out_d, unsigned &out_m) {
    unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
#ifdef __GNUC__
    asm(
        "divl %4; \n\t"
        : "=a" (d), "=d" (m)
        : "d" (xh), "a" (xl), "r" (y)
    );
#else
    __asm {
        mov edx, dword ptr[xh];
        mov eax, dword ptr[xl];
        div dword ptr[y];
        mov dword ptr[d], eax;
        mov dword ptr[m], edx;
    };
#endif
    out_d = d; out_m = m;
}

inline unsigned fasterLLMod(unsigned long long x, unsigned y) {
    unsigned dummy, r;
    fasterLLDivMod(x, y, dummy, r);
    return r;
}

int_fast64_t hash_function(int x){
    int primer=fasterLLMod(x,D_MAX),mask=0xF0000000;
    primer=fasterLLMod(1LL*primer*PRIM1,D_MAX);
    primer=fasterLLMod(1LL*primer*PRIM2,D_MAX);
    x|=mask;
    x>>=28;
    primer<<=4;
    primer+=x;
    return fasterLLMod(primer,D_MAX);
}

void inc(int& i){
    if (++i==D_MAX-10) i&=0;
}

void add(int x){
    int key=hash_function(x);
    while (v[key]>0) inc(key);
    v[key]=x;
}

void rmv(int x){
    int key=hash_function(x);
    while (v[key]!=x and v[key]!=0) inc(key);
    if (v[key]==x) v[key]=-1;
}

bool chk(int x){
    int key=hash_function(x);
    while (v[key]!=x and v[key]!=0) inc(key);
    return (v[key]==x);
}

int main()
{
    int n;
    f>>n;
    for (int q,x,i=0;i<n;++i){
        f>>q>>x;
        switch (q){
            case 1:{add(x);break;}
            case 2:{rmv(x);break;}
            case 3:{t<<chk(x)<<"\n";break;}
        }
    }
    return 0;
}