Cod sursa(job #2660777)

Utilizator andrei_ciobanuciobanu andrei andrei_ciobanu Data 20 octombrie 2020 15:25:27
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;

#define N 1000000
#define MOD 666013

vector<int> myhash[MOD];

bool mysearch(int x){
    int h = x % MOD;
    int i = 0;
    while (i < myhash[h].size() && myhash[h][i] != x) i ++;
    return (i < myhash[h].size());
}

void add(int x){
    int h = x % MOD;
    if (!mysearch(x)){
        myhash[h].push_back(x);
    }
}

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

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