Pagini recente » Cod sursa (job #1717224) | Cod sursa (job #1392550) | Cod sursa (job #2806780) | Cod sursa (job #1236494) | Cod sursa (job #2418262)
#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");
class hash {
public:
hash () {}; //C-TOR
void add(unsigned target)
{
hashfct(pointer,target);
FLAG1:
if (container[pointer]==target)
{
flagged[pointer]=false;
return;
}
else if (container[pointer]==0)
container[pointer]=target;
else
{
inc();
goto FLAG1;
}
}
void remov(unsigned target)
{
hashfct(pointer,target);
FLAG2:
if (container[pointer]==target)
{
flagged[pointer]=true;
return;
}
else if (container[pointer]==0)
return;
else
{
inc();
goto FLAG2;
}
}
bool check(unsigned target)
{
hashfct(pointer,target);
FLAG3:
if (container[pointer]==target and !flagged[pointer])
return true;
else if ((container[pointer]==target and flagged[pointer]) or container[pointer]==0)
return false;
else
{
inc();
goto FLAG3;
}
}
~hash () {}//D-TOR
private:
static const int SIZE = 1048575,PRIM1=21313,PRIM2=19937;
unsigned container[SIZE],pointer=0;
bitset <SIZE> flagged;
void inc()
{
if (UNLIKELY(++pointer==SIZE))
pointer&=0;
}
void hashfct(unsigned &key,unsigned target)
{
key = target % SIZE;
unsigned mask = 0xF0000000;
key = (1LL * key * PRIM1) % SIZE;
key = (1LL * key * PRIM2) % SIZE;
target |= mask;
target >>= 28;
key <<= 4;
key += target;
key %= SIZE;
}
} h;
void handle(int type,int target) {
if (type == 1)
h.add(target);
else if (type == 2)
h.remov(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);
}