Cod sursa(job #3131660)

Utilizator vatau.lorenaVatau Lorena vatau.lorena Data 20 mai 2023 21:41:09
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int N = 1e6 + 10;
vector<vector<int>> buckets(N);

bool existsInBucket(const vector<int>& bucket, int x) {
    return find(bucket.begin(), bucket.end(), x) != bucket.end();
}

void insertIntoBucket(vector<int>& bucket, int x) {
    if (!existsInBucket(bucket, x)) {
        bucket.push_back(x);
    }
}

void removeFromBucket(vector<int>& bucket, int x) {
    auto it = find(bucket.begin(), bucket.end(), x);
    if (it != bucket.end()) {
        bucket.erase(it);
    }
}

int main() {
    int n;
    fin >> n;

    while (n) {
        int op, x;
        fin >> op >> x;

        int key = x % N;

        switch (op) {
            case 1: {
                insertIntoBucket(buckets[key], x);
                break;
            }
            case 2: {
                removeFromBucket(buckets[key], x);
                break;
            }
            case 3: {
                bool found = existsInBucket(buckets[key], x);
                fout << (found ? "1\n" : "0\n");
                break;
            }
        }
        n--;
    }

    fin.close();
    fout.close();
    return 0;
}