Cod sursa(job #1041938)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 26 noiembrie 2013 13:22:39
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 4.01 kb
#include <iostream>
//#include <unordered_map>
#include <algorithm>
#include <fstream>

std::ifstream fin("hashuri.in");
std::ofstream fout("hashuri.out");

struct nod
{
    int val;
    nod *next;
};

int n;
//std::unordered_map<int, bool> hashu;

nod *prim[666014];// = new nod;
void citire()
{
//    nod *ultim;// = prim;
//    prim->next = NULL;

    int x, y;
    fin>>n;
    for(int i = 0; i < n; i++)
    {
        fin>>x>>y;
//        std::cout<<x<<' '<<y<<'\n';

        if(x == 1)
        {
            //hashu[y] = 1;
            if(prim[y%666013])
            {
                nod *val = prim[y%666013];
                bool found = false;
                while(val)
                {
//                    std::cout<<val->val<<'\n';
                    if(val->val == y)
                    {
                        found = true;
                        break;
                    }
                    val = val->next;
                }
//                val = NULL;
                if(!found)
                {
                    nod *newNod = new nod;
                    newNod->val = y;
                    newNod->next = prim[y%666013];
                    prim[y%666013] = newNod;
    //                ultim->next = newNod;
    //                ultim = newNod;
                }
            }
            else
            {
                prim[y%666013] = new nod;
//                prim[y%666013]->val = -1;
                prim[y%666013]->val = y;
                prim[y%666013]->next = NULL;
//                nod *newN = new nod;
//                newN->val = y;
//                newN->next = NULL;
//                prim[y%666013]->next = newN;
//                std::cout<<y<<'\n';
            }
        }
        else
            if(x == 2)
            {
                if(prim[y%666013])
                {
    //                std::cout<<"here2"<<'\n';
                    nod *val = prim[y%666013];

                    if(val->val == y)
                    {
                        prim[y%666013] = val->next;
                        delete val;
                        continue;
//                        break;
                    }

                    while(val->next)
                    {
                        nod *lastVal = val->next;
//                        std::cout<<val->val<<'\n';
                        if(val->val == y)
                        {
                            lastVal->next = val->next;
                            delete val;
                            break;
                        }
//                        lastVal = val;
                        val = val->next;
                    }
//                    val = NULL;
//                    lastVal = val;
    //                hashu.erase(y);
                }
            }
            else
            {
                if(prim[y%666013])
                {
//                                    std::cout<<"here3"<<'\n';
                    nod *val = prim[y%666013];
                    bool found = false;
                    while(val)
                    {
                        if(val->val == y)
                        {
                            fout<<1<<'\n';
//                            std::cout<<1<<'\n';
                            found = true;
                            break;
                        }
                        val = val->next;
                    }
//                    val = NULL;
                    if(!found)
                    {
                        fout<<0<<'\n';
//                        std::cout<<0<<'\n';
                    }
                    //                fout<<hashu[y]<<'\n';
                }
                else
                {
//                                    std::cout<<"here2"<<'\n';
                    fout<<0<<'\n';
//                    std::cout<<0<<'\n';
                }
            }
    }
}

int main()
{
    citire();
    return 0;
}