Pagini recente » Cod sursa (job #273656) | Cod sursa (job #1761242) | Istoria paginii utilizator/theodor.burlacu | Cod sursa (job #221007) | Cod sursa (job #1527086)
#include <set>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <forward_list>
#include <vector>
#include <algorithm>
using namespace std;
// for list: on test 9 924ms 15832k
class HashSet {
vector<forward_list<int>> buckets;
int capacity;
public:
HashSet(int _capacity) : capacity(_capacity) {
buckets = vector<forward_list<int>>(capacity);
}
void insert(int x) {
forward_list<int>& l = buckets[hash(x)];
bool found = std::find(l.begin(), l.end(), x) != l.end();
if (!found) {
l.push_front(x);
}
}
void erase(int x) {
forward_list<int>& l = buckets[hash(x)];
l.remove(x);
}
bool find(int x) {
forward_list<int>& l = buckets[hash(x)];
return std::find(l.begin(), l.end(), x) != l.end();
}
private:
int hash(int x) {
return x % capacity;
}
};
int main() {
HashSet s(671159);
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int n;
f >> n;
for (int i = 0; i < n; i++) {
int op, x;
f >> op >> x;
switch (op) {
case 1:
s.insert(x);
break;
case 2:
s.erase(x);
break;
case 3:
g << (s.find(x) ? 1 : 0) << "\n";
break;
default:
std::cout << "Not supported!" << std::endl;
break;
}
}
return 0;
}