Cod sursa(job #2030769)

Utilizator zeboftwAlex Mocanu zeboftw Data 2 octombrie 2017 10:59:43
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.63 kb
#include <fstream>

using namespace std;

const int p = 99991;

struct lista {
    int val;
    lista * next = NULL;
}* hashu[100000];

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

int main()
{
    int n, op, x;

    fin >> n;

    for (int i=0; i <2000; i++) hashu[i] = NULL;

    for (int i=1; i<=n; i++) {
        fin >> op >> x;
        if (op == 1)
        {
            if (hashu[x%p] != NULL)
            {
                lista *aux = hashu[x%p];
                while (aux->next != NULL)
                {
                    aux = aux->next;
                }
                lista *newlista = new lista;
                newlista->val = x / p;
                newlista->next = NULL;
                aux->next = newlista;
            }
            else
            {
                lista *newlista = new lista;
                newlista->val = x / p;
                hashu[x%p] = newlista;
            }
        }
        else if (op == 2) {
            if (hashu[x%p] != NULL)
            {
                lista *aux = hashu[x%p], *ant;
                if (aux->val == x / p && aux->next != NULL) {
                    hashu[x % p] = aux->next;
                    delete aux;
                }
                else if (aux->val == x / p) {
                    hashu[x%p] = NULL;
                }
                else
                {
                    while (aux->next != NULL)
                    {
                        ant = aux;
                        aux = aux->next;
                        if (aux->val == x / p) break;
                    }
                    if (aux->val == x / p && aux->next)
                        ant->next = aux->next;
                    else if (aux->val == x) {
                        ant->next = NULL;
                        delete aux;
                    }
                }
            }
        }
        else {
            if (hashu[x%p] != NULL)
            {
                lista *aux = hashu[x%p];
                if (aux->val == x / p) {
                    fout << 1 << '\n';
                }
                else
                {
                    while (aux->next != NULL)
                    {
                        aux = aux->next;
                        if (aux->val==x/p)
                            break;
                    }
                    if (aux->val == x / p)
                        fout << 1 << '\n';
                    else
                        fout << 0 << '\n';
                }
            }
            else
                fout << 0 << '\n';
        }
    }
    return 0;
}