Pagini recente » Cod sursa (job #146290) | Cod sursa (job #2643518) | Cod sursa (job #3120516) | Cod sursa (job #143311) | Cod sursa (job #2623264)
#include <vector>
#incude <algorithm>
#include <fstream>
int p;
int h(int& val) {
return val % p;
}
int exists(std::vector<unsigned int>* v, int& key) {
std::vector<unsigned int>& a = v[h(key)];
std::vector<unsigned int>::iterator found = std::find(a.begin(), a.end(), key);
return (found != a.end()) ? 1 : 0;
}
int main()
{
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");
int n;
f >> n;
int command, x;
p = n / 2 + rand() % (n / 2);
std::vector<unsigned int>* a = new std::vector<unsigned int>[p];
for (int i = 0; i < n; i++) {
f >> command >> x;
if (command == 1) {
if (!exists(a, x)) {
a[h(x)].push_back(x);
}
}
if (command == 2) {
std::vector<unsigned int>& v = a[h(x)];
std::vector<unsigned int>::iterator found = std::find(v.begin(), v.end(), x);
if (found != v.end()) {
v.erase(found);
}
}
if (command == 3) {
g << exists(a, x) << "\n";
}
}
delete[] a;
}