Cod sursa(job #2418284)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 4 mai 2019 15:42:22
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.02 kb
#include <bits/stdc++.h>

using namespace std;

#define LIKELY(condition) __builtin_expect(static_cast<bool>(condition), 1)
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)

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=10*n+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 <<(bool target)
    {
        aux&=0;
        /*sign=1;
        if (target<0)
            sign=-1;*/
        n=target;
        if (n==0)
            nr[aux++]='0';
        for (; n; n/=10)
            nr[aux++]=/*(sign**/n/*)*/%10+'0';
        /*if (sign==-1)
            buffer[index]='-',inc();*/
        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/*,sign*/;
    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");

template <class _key, class _info>
class hashtable {
public:
    hashtable ():pointer(0) {}; //C-TOR

    void add(unsigned target)
    {
        set_index_to(target);
FLAG1:
        if (container[pointer]==target)
        {
            flagged[pointer]=false;
            return;
        }
        else if (container[pointer]==0)
            container[pointer]=target;
        else
        {
            inc();
            goto FLAG1;
        }
    }

    void remov(unsigned target)
    {
        set_index_to(target);
FLAG2:
        if (container[pointer]==target)
        {
            flagged[pointer]=true;
            return;
        }
        else if (container[pointer]==0)
            return;
        else
        {
            inc();
            goto FLAG2;
        }
    }

    bool check(unsigned target)
    {
        set_index_to(target);
FLAG3:
        if (container[pointer]==target and !flagged[pointer])
            return true;
        else if ((container[pointer]==target and flagged[pointer]) or container[pointer]==0)
            return false;
        else
        {
            inc();
            goto FLAG3;
        }
    }
    ~hashtable () {}//D-TOR

private:
    static const unsigned SIZE = 1048576, MOD = 1048575,
                     PRIME1 = 21313, PRIME2 = 19937;
    unsigned container[SIZE], pointer;
    std::bitset <SIZE> flagged;

    void inc()
    {
        if (UNLIKELY(++pointer == SIZE))
            pointer&=0;
    }
    void set_index_to(unsigned target)
    { /*
        unsigned key=target%SIZE;
        unsigned mask=0xF0000000;
        //std::cout << std::bitset<32>(SIZE) << std::endl << std::bitset<32>(key*PRIME1) << std::endl << std::bitset<32>((key*PRIME1) % SIZE) << std::endl << std::endl;
        //std::cout << ((1LL*key*PRIME1) % SIZE) << " - " << ((1LL*key*PRIME1) & (uint64_t)SIZE) << std::endl;
        key=(1LL*key*PRIME1)%SIZE;
        key=(1LL*key*PRIME2)%SIZE;
        target|=mask;
        target>>=28;
        key<<=4;
        key+=target;
        key%=SIZE;
        pointer = key; */
        unsigned key=target&MOD;
        unsigned mask=0xF0000000;
        key=(1LL*key*PRIME1)&MOD;
        key=(1LL*key*PRIME2)&MOD;
        target|=mask;
        target>>=28;
        key<<=4;
        key+=target;
        key&=MOD;
        pointer = key;
    }
};

hashtable <int, int> h;

void handle(int type,int target) {
    if (type == 1)
        h.add(target);
    else if (type == 2)
        h.remov(target);
    else
        t << h.check(target) << "\n";
}

int main() {
    int n;
    f >> n;
    for (int op, target; n; --n)
        f >> op >> target,
        handle(op, target);
}