Cod sursa(job #285031)

Utilizator mika17Mihai Alex Ionescu mika17 Data 22 martie 2009 12:02:58
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int HSIZE = 41521;
vector <int> H[HSIZE];
const double kth = 0.618033;
typedef vector<int>::iterator vptr;

void hash_add(int);
bool hash_find(int);
void hash_erase(int);
     
int main() {
    
    int N, op, X;
    fin>>N;
    
    while(N--) {
               
               fin>>op>>X;
               switch(op) {
                          
                          case 1: hash_add(X); break;
                          case 2: hash_erase(X); break;
                          case 3: fout<<hash_find(X)<<'\n'; break;
                          }
               }
}

int hash(int K) {
    
    return int( (kth * K - int(kth * K) ) * HSIZE );   
}

void hash_add(int K) {

     if(hash_find(K)) return;
     
     H[ hash(K) ] . push_back(K);
     }

void hash_erase(int K) {
     
     int h = hash(K);
     
     for(vptr p = H[h].begin(); p != H[h].end(); )
              
              if(*p == K) { H[h].erase(p); return; }
               else ++p;
     }
     
bool hash_find(int K) {
     
     int h = hash(K);
     
     for(vptr p = H[h].begin(); p != H[h].end(); ++p)
     
              if(*p == K) return 1;
     return 0;
     }