Cod sursa(job #2938624)

Utilizator EasyTnsEasyTns EasyTns Data 12 noiembrie 2022 13:42:16
Problema Hashuri Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.46 kb
#include<fstream>
#include<vector>
#include<cmath>
//#include<iostream>
using namespace std;
class InParser {
    vector<char> str;
    int ptr;
    ifstream fin;

    char getChar() {
        if (ptr == int(str.size())) {
            fin.read(str.data(), str.size());
            ptr = 0;
        }
        return str[ptr++];
    }

    template<class T>
    T getInt() {
        char chr = getChar();
        while (!isdigit(chr) && chr != '-')
            chr = getChar();
        int sgn = +1;
        if (chr == '-') {
            sgn = -1;
            chr = getChar();
        }
        T num = 0;
        while (isdigit(chr)) {
            num = num * 10 + chr - '0';
            chr = getChar();
        }
        return sgn * num;
    }

public:
    InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
    ~InParser() { fin.close(); }

    template<class T>
    friend InParser& operator>>(InParser& in, T& num) {
        num = in.getInt<T>();
        return in;
    }
};
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
InParser cin("hashuri.in");
OutParser cout("hashuri.out");
double y=(sqrt(5)-1)/2;
int m;
struct nod{
    int info=0;
    nod *urm=NULL;
}*h[1000002];
void inserare(nod *&prim,int val)
{
    nod*t=new nod;
    t->info=val;
    t->urm=prim;
    prim=t;
}
int get_index(int x)
{
    return m*(x*y-floor(x*y));
}
void cautare(nod *prim,int val)
{
    int gasit=0;
    if(prim==NULL)
        cout<<"0\n";
    else
    {
        nod *p=prim;
        while(p!=NULL)
        {
            if(p->info==val)
            {gasit=1;break;}
            p=p->urm;
        }
        if(gasit==1)
            cout<<"1\n";
        else
            cout<<"0\n";

    }
}
void stergere(nod *&prim,int val)
{
    if(prim==NULL)
        return ;
    nod *t;
    if(prim->info==val)
    {
        t=prim;
        prim=prim->urm;
        delete t;
        return ;
    }
    t=prim;
    while(t->info!=val&&t->urm!=NULL)
    {
        t=t->urm;
    }
    if(t->info!=val)
        return;
    nod *p=t;
    p=p->urm;
    delete t;

}

int main()
{
    int n;
    m=1000000;
    cin>>n;
    int x,j;
    for(;n;n--)
    {
        cin>>x>>j;
        if(x==1)
            inserare(h[get_index(j)],j);
        else
        if(x==2)
        {
            stergere(h[get_index(j)],j);
        }
        else if(x==3)
            cautare(h[get_index(j)],j);
    }

}