Cod sursa(job #1756841)

Utilizator liviu23Liviu Andrei liviu23 Data 13 septembrie 2016 18:43:51
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#define ll long long
#define M 99873
using namespace std;

struct node {
    int num;
    node* next;
};

node* v[M];

void extract(int x,node *p) {
    if(p==NULL) {
        v[x%M]=v[x%M]->next;
        return;
    }
    node *rez=p->next;
    p->next=p->next->next;
    delete rez;
}

void add(int x) {
    node *p=new node;
    p->num=x;
    p->next=v[x%M];
    v[x%M]=p;
}

bool check(int x, node* &last) {
    last=NULL;
    node *p=v[x%M];
    while(p!=NULL) {
        if(p->num==x)
            return true;
        last=p;
        p=p->next;
    }
    return false;
}

int main()
{
    ifstream fin("hashuri.in");
    ofstream fout("hashuri.out");
    int n,op;
    ll x;
    node *p;

    for(int i=0;i<M;i++)
        v[i]=NULL;

    fin>>n;
    for(int i=0;i<n;i++) {
        fin>>op>>x;
        if(check(x,p)) {
            if(op==2)
                extract(x,p);
            else if(op==3)
                fout<<"1\n";
        }
        else {
            if(op==1)
                add(x);
            else if(op==3)
                fout<<"0\n";
        }
    }
    return 0;
}