Cod sursa(job #1921548)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 10 martie 2017 13:08:47
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.62 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

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;
}

//x / y < 2^32 !
inline unsigned fasterLLMod(unsigned long long x, unsigned y) {
    unsigned dummy, r;
    fasterLLDivMod(x, y, dummy, r);
    return r;
}

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;
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            return *this;}
        ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x400000;
        char buffer[SIZE];
        int index=0;
        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;
        int index=0,aux,n;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index=0,output_file.write(buffer,SIZE),memset(buffer,0,sizeof(buffer));}
};

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

int v[10000010];
const int key=43;

inline int hashcode(int x)
{
    return fasterLLMod(1LL*x*key+x/key,10000000);
}

inline void increment(int &a)
{
    if (++a==10000000) a=0;
}

inline void hash_add(int x)
{
    int a=hashcode(x),i;
    for(i=a; v[i]!=0 and v[i]!=x; increment(i));
    if(v[i]==x) return;
    for(i=a; v[i]>0; increment(i));
    v[i]=x;
}

inline void hash_erase(int x)
{
    int i;
    for (i=hashcode(x); v[i]!=0 and v[i]!=x; increment(i));
    if (v[i]==x) v[i]=-1;
}

inline bool hash_verify(int x)
{
    int i;
    for (i=hashcode(x); v[i]!=0 and v[i]!=x; increment(i));
    return (v[i]==x);
}

int main()
{
    int n,q,x;
    f>>n;
    for (int i=0; i<n; ++i)
    {
        f>>q>>x;
        if (q==1)
            hash_add(x);
        else if(q==2)
            hash_erase(x);
        else
            t<<hash_verify(x)<<"\n";
    }
    return 0;
}