Pagini recente » Cod sursa (job #2726174) | Cod sursa (job #2192538) | Cod sursa (job #146174) | Cod sursa (job #459027) | Cod sursa (job #2418354)
#include <bits/stdc++.h>
using namespace std;
#define LIKELY(condition) __builtin_expect(static_cast<bool>(condition), 1)
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
class parser {
public:
parser() {}
parser(const char *file_name)
{
input_file.open(file_name,ios::in | ios::binary);
input_file.sync_with_stdio(false);
index&=0;
input_file.read(buffer,SIZE);
}
inline parser &operator >>(int &n)
{
for (; buffer[index]<'0' or buffer[index]>'9'; inc());
n&=0;
//sign&=0;
//sign|=(buffer[index-1]=='-');
for (; '0'<=buffer[index] and buffer[index]<='9'; inc())
n=10*n+buffer[index]-'0';
//n^=((n^-n)&-sign);
return *this;
}
~parser()
{
input_file.close();
}
private:
fstream input_file;
static const int SIZE=0x400000; ///4MB
char buffer[SIZE];
int index/*,sign*/;
inline void inc()
{
if(++index==SIZE)
index=0,input_file.read(buffer,SIZE);
}
};
class writer {
public:
writer() {};
writer(const char *file_name)
{
output_file.open(file_name,ios::out | ios::binary);
output_file.sync_with_stdio(false);
index&=0;
}
inline writer &operator <<(bool target)
{
aux&=0;
/*sign=1;
if (target<0)
sign=-1;*/
n=target;
if (n==0)
nr[aux++]='0';
for (; n; n/=10)
nr[aux++]=/*(sign**/n/*)*/%10+'0';
/*if (sign==-1)
buffer[index]='-',inc();*/
for(; aux; inc())
buffer[index]=nr[--aux];
return *this;
}
inline writer &operator <<(const char *target)
{
aux&=0;
while (target[aux])
buffer[index]=target[aux++],inc();
return *this;
}
~writer()
{
output_file.write(buffer,index);
output_file.close();
}
private:
fstream output_file;
static const int SIZE=0x200000; ///2MB
int index,aux,n/*,sign*/;
char buffer[SIZE],nr[24];
inline void inc()
{
if(++index==SIZE)
index&=0,output_file.write(buffer,SIZE);
}
};
parser f ("hashuri.in");
writer t ("hashuri.out");
#ifdef old
template <class Tkey, class Tinfo>
class hashtable {
public:
hashtable() {
container = new data_cell[SIZE];
}; //C-TOR
inline Tinfo add(Tkey target, Tinfo info) {
set_index_to(target);
while(true) {
if (container[pointer].key == target) {
occupied[pointer] = 2;
return container[pointer].info;
} else if (occupied[pointer] < 2) {
container[pointer].key = target;
container[pointer].info = info;
} else {
inc();
}
}
}
void remove(Tkey target) {
set_index_to(target);
while(true) {
if (container[pointer].key == target) {
occupied[pointer] = 1;
return;
} else if (!occupied[pointer]) {
return;
} else {
inc();
}
}
}
[[gnu::hot]] inline Tinfo find(Tkey target) {
set_index_to(target);
while(true) {
if (LIKELY(container[pointer].key == target && occupied[pointer] == 2)) {
return container[pointer].info;
} else if ((container[pointer].key == target && occupied[pointer] == 1)|| !occupied[pointer]) {
return Tinfo();
} else {
inc();
}
}
}
bool check(Tkey target) {
set_index_to(target);
while(true) {
if (container[pointer].key == target && occupied[pointer] == 2) {
return true;
} else if ((container[pointer].key == target && occupied[pointer] == 1)|| !occupied[pointer]) {
return false;
} else {
inc();
}
}
}
~hashtable () {
delete[] container;
}//D-TOR
private:
static const unsigned SIZE = 1048576, MOD = SIZE - 1, /// will work with less ( 2 << 17)
PRIME1 = 21313, PRIME2 = 19937, mask = 0xFFF00000;
struct data_cell {
Tkey key;
Tinfo info;
} *container;
bool string_hash, int_hash, generic_hash;
unsigned pointer;
int_fast8_t occupied[SIZE];
std::hash<Tkey> default_hsh;
void inc() {
if (UNLIKELY(++pointer == SIZE))
pointer &= 0;
}
void set_index_to(Tkey target) {
pointer = default_hsh(target),
pointer &= MOD;
}
};
#else
template <class _key, class _information>
class hashtable {
public:
/// makes no sense to implement rule of three
hashtable() {
storage = new data_cell[MOD + 10];
}
[[gnu::hot]] void add(_key key, _information info) {
uint_fast32_t index = hash_key(key);
index &= MOD;
for (; UNLIKELY(storage[index].free == false && storage[index].key != key);
index = (index + 1) & MOD) {}
storage[index].free = false,
storage[index].key = key,
storage[index].info = info;
}
[[gnu::hot]] _information add_unique(_key key, _information info) {
uint_fast32_t index = hash_key(key);
index &= MOD;
for (; UNLIKELY(storage[index].free == false && storage[index].key != key);
index = (index + 1) & MOD) {}
if (storage[index].free) {
storage[index].free = false,
storage[index].key = key,
storage[index].info = info;
return info;
} else {
return storage[index].info;
}
}
[[gnu::hot]] void remove(_key key) {
uint_fast32_t index = hash_key(key);
index &= MOD;
for (; UNLIKELY(storage[index].free == false && storage[index].key != key);
index = (index + 1) & MOD) {}
storage[index].free = true;
}
[[gnu::hot]] _information find(_key key) {
uint_fast32_t index = hash_key(key);
index &= MOD;
for (; UNLIKELY(storage[index].free == false && storage[index].key != key);
index = (index + 1) & MOD) {}
return storage[index].info;
}
bool check(_key key) {
uint_fast32_t index = hash_key(key);
index &= MOD;
for (; UNLIKELY(storage[index].free == false && storage[index].key != key);
index = (index + 1) & MOD) {}
return (storage[index].free ^ 1);
}
~hashtable() {
delete[] storage;
}
private:
struct data_cell{
bool free = true;
_key key;
_information info;
} *storage;
std::hash<_key> hash_key;
static const uint32_t MOD = (2 << 17) - 1;
};
#endif
hashtable <int, bool> h;
void handle(int type, int target) {
if (type == 1)
h.add(target, true);
else if (type == 2)
h.remove(target);
else
t << h.check(target) << "\n";
}
int main() {
int n;
f >> n;
for (int op, target; n; --n)
f >> op >> target,
handle(op, target);
}