Cod sursa(job #2418245)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 4 mai 2019 14:50:27
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.02 kb
#include <bits/stdc++.h>
#include <typeinfo>

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 (){};

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

    //find/// TODO

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

    bool check(unsigned target){
        hashfct(pointer,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 (){}

private:
    static const int SIZE = 1048575, /// 2 ^ 20 - 1
                     PRIM1 = 21313, PRIM2 = 19937;
    unsigned container[SIZE], pointer = 0;
    bitset <SIZE> flagged;
    std::hash<_key> funct;

    void inc(){
        if (UNLIKELY(++pointer == SIZE))
            pointer &= 0;
    }

    unsigned djb2(std::string str)
    {
        unsigned hash = 5381;

        for (auto i: str) {
            hash = ((hash << 5) + hash) + i;
            hash &= SIZE;
        }

        return hash;
    }

    void hashfct(unsigned &key, _key target) {
        /*if (typeid(std::string) == typeid(_key)) {
            key = djb2(target);
        */
        if (typeid(int) == typeid(_key)) {
            key = target & SIZE;
            unsigned mask = 0xF0000000;
            key = (1LL * key * PRIM1) & SIZE;
            key= (1LL * key * PRIM2) & SIZE;
            target |= mask;
            target >>= 28;
            key <<= 4;
            key += target;
            key &= SIZE;
        } else {
            key = funct(target);
        }
    }

};

hashtable<int, bool> 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);
    }
}