Cod sursa(job #2924812)

Utilizator george_buzasGeorge Buzas george_buzas Data 11 octombrie 2022 16:10:00
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.69 kb
#include <fstream>
#include <map>
#include <vector>
#define MODULO 250000
using namespace std;

int main() {
	ifstream fin("hashuri.in");
	ofstream fout("hashuri.out");
	map<int, vector<int>> hash_map;
	int nr_operations, operation_type, number;
	fin >> nr_operations;
	for (int i = 0; i < nr_operations; ++i) {
		fin >> operation_type >> number;
		int key = number % MODULO;
		if (operation_type == 1) {
			if (hash_map.find(key) == hash_map.end()) {
				vector<int> mapped_values;
				mapped_values.push_back(number);
				hash_map.insert({key, mapped_values});
			} else {
				vector<int>& mapped_values = hash_map.at(key);
				int length = mapped_values.size();
				bool is_found = false;
				for (int i = 0; i < length; ++i) {
					if (mapped_values[i] == number) {
						is_found = true;
						break;
					}
				}
				if (!is_found) {
					mapped_values.push_back(number);
				}
			}
		} else if (operation_type == 2) {
			if (hash_map.find(key) != hash_map.end())  {
				vector<int>& mapped_values = hash_map.at(key);
				int length = mapped_values.size();
				for (int i = 0; i < length; ++i) {
					if (mapped_values[i] == number) {
						mapped_values.erase(mapped_values.begin() + i);
						break;
					}
				}
			}
		} else {
			if (hash_map.find(key) != hash_map.end()) {
				vector<int> mapped_values = hash_map.at(key);
				int length = mapped_values.size();
				bool is_found = false;
				for (int i = 0; i < length; ++i) {
					if (mapped_values[i] == number) {
						fout << 1 << '\n';
						is_found = true;
						break;
					}
				}
				if (!is_found) {
					fout << 0 << '\n';
				}
			} else {
				fout << 0 << '\n';
			}
		}
	}
	return 0;
}