Cod sursa(job #2165252)

Utilizator Horia14Horia Banciu Horia14 Data 13 martie 2018 11:36:48
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.8 kb
#include<cstdio>
#include<cctype>
#define MOD 660013
#define BUF_SIZE 1 << 19
using namespace std;

char buf[BUF_SIZE];
int n, pos = BUF_SIZE;

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

node* Hash[MOD];

inline char getChar(FILE *fin) {
    if(pos == BUF_SIZE) {
        fread(buf,1,BUF_SIZE,fin);
        pos = 0;
    }
    return buf[pos++];
}

inline int read(FILE *fin) {
    int res = 0;
    char c;
    do {
        c = getChar(fin);
    } while(!isdigit(c));
    do {
        res = 10*res + c - '0';
        c = getChar(fin);
    } while(isdigit(c));
    return res;
}

inline int h(int x) {
    return x % MOD;
}

inline bool searchKey(int x) {
    int k = h(x);
    node* p = Hash[k];
    while(p != NULL && p->val != x)
        p = p->next;
    if(p != NULL)
        return 1;
    return 0;
}

inline void insertKey(int x) {
    int k = h(x);
    node* p = new node;
    p->val = x;
    p->next = Hash[k];
    Hash[k] = p;
}

inline void deleteKey(int x) {
    int k = h(x);
    node *p, *q;
    p = Hash[k];
    if(p->val == x) {
        Hash[k] = Hash[k]->next;
        delete p;
    } else {
        while(p->next->val != x)
            p = p->next;
        q = p->next;
        p->next = p->next->next;
        delete q;
    }
}

int main() {
    int op, x;
    FILE *fin, *fout;
    fin = fopen("hashuri.in","r");
    fout = fopen("hashuri.out","w");
    n = read(fin);
    for(int i = 0; i < n; i++) {
        op = read(fin);
        x = read(fin);
        if(op == 1) {
            if(!searchKey(x))
                insertKey(x);
        } else if(op == 2) {
            if(searchKey(x))
                deleteKey(x);
        } else fprintf(fout,"%d\n",searchKey(x));
    }
    fclose(fin);
    fclose(fout);
    return 0;
}