Cod sursa(job #2670715)

Utilizator teodorescunicolasteodorescu nicolas alexandru teodorescunicolas Data 10 noiembrie 2020 16:20:40
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <stdio.h>
#include <vector>
#include <ctype.h>

using namespace std;

#define NMAXX 1000000
#define MOD 666013

FILE *fin, *fout;
int v[NMAXX];
vector<int> HashH[MOD];

int readInt() {
    int numar = 0;
    char ch;
    while ( isspace( ch = fgetc( fin ) ) );
    do {
        numar = numar * 10 + ch - '0';
    } while ( isdigit( ch = fgetc( fin ) ) );
    return numar;
}

bool find( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < hash[cod].size() && hash[cod][i] != x ) {
        i++;
    }
    return i < hash[cod].size();
}

void push( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < hash[cod].size() && hash[cod][i] != x ) {
        i++;
    }
    if ( i == hash[cod].size() ) {
        hash[cod].push_back( x );
    }
}

void erase( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < hash[cod].size() && hash[cod][i] != x ) {
        i++;
    }
    if ( i < hash[cod].size() ) {
        hash[cod].erase( hash[cod].begin() + i );
    }
}

int main()
{
    int n, cer, i;
    fin = fopen( "hashuri.in", "r" );
    fout = fopen( "hashuri.out", "w" );
    fscanf( fin, "%d", &n );
    for ( i = 0; i < n; i++ ) {
        cer = readInt();
        v[i] = readInt();
        if ( cer == 1 ) {
            push( v[i] );
        } else if ( cer == 2 ) {
            erase( v[i] );
        } else {
            fprintf( fout, "%d\n", find( v[i] ) );
        }
    }
    fclose( fin );
    fclose( fout );
    return 0;
}