Cod sursa(job #1764023)

Utilizator priboiraduPriboi Radu Bogdan priboiradu Data 24 septembrie 2016 21:49:33
Problema Hashuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1000000
#define MOD 666013

int v[MAXN], lista[MOD], next[MAXN];

void adauga( int x, int p ) {
    v[p] = x;
    next[p] = lista[x%MOD];
    lista[x%MOD] = p;
}

void sterge( int x ) {
    int p;
    p = lista[x%MOD];
    if ( v[p] == x )
        lista[x%MOD] = next[p];
    else {
        while ( v[next[p]] != x && next[p] != 0 )
            p = next[p];
        if ( v[next[p]] == x )
            next[p] = next[next[p]];
    }
}

int cauta( int x ) {
    int p;
    p = lista[x%MOD];
    while ( p != 0 && v[p] != x )
        p = next[p];
    if ( v[p] == x )
        return 1;
    return 0;
}

int main() {
    FILE *fin, *fout;
    int n, i, op, x;
    fin = fopen( "hashuri.in", "r" );
    fout = fopen( "hashuri.out", "w" );
    fscanf( fin, "%d", &n );
    for ( i = 1; i <= n; i++ ) {
        fscanf( fin, "%d%d", &op, &x );
        if ( op == 1 ) {
            if ( cauta( x ) == 0 )
                adauga( x, i );
        }
        else {
            if ( op == 2 )
                sterge( x );
            else
                fprintf( fout, "%d\n", cauta( x ) );
        }
    }
    fclose( fin );
    fclose( fout );
    return 0;
}