Cod sursa(job #2645173)

Utilizator GiosinioGeorge Giosan Giosinio Data 27 august 2020 13:55:32
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <fstream>
#define P 666013
#define h(x) x%P

using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct node{
    int key;
    node *next;
}*L[P];

void add(int x, int y){
    bool exista=0;
    for(node *p = L[x]; p != nullptr; p = p->next)
        if(p->key == y){
            exista = 1;
            break;
        }
    if(!exista){
        node *t = new node;
        t->key = y, t->next = L[x];
        L[x] = t;
    }
}

void del(int x, int key){
    if(L[x]->key == key){
        node *t = L[x];
        L[x] = L[x]->next;
        delete t;
    }
    else{
        node *p = L[x];
        while(p->next != nullptr){
            if(p->next->key == key)
                break;
            p = p->next;
        }
        if(p->next != nullptr){
            node *t = p->next;
            p->next = t->next;
            delete t;
        }
    }
}

bool cautare(int ind, int key){
    for(node *p = L[ind]; p != nullptr; p = p->next)
        if(p->key == key)
            return 1;
    return 0;
}

int main()
{
    int N; f>>N;
    int op,x;
    for(int i=1; i<=N; i++){
        f>>op>>x;
        if(op == 1)
            add(h(x),x);
        if(op == 2){
            if(L[h(x)] != nullptr)
                del(h(x),x);
        }
        if(op == 3)
            g<<cautare(h(x),x)<<"\n";
    }
}