Cod sursa(job #764818)

Utilizator mi5humihai draghici mi5hu Data 6 iulie 2012 13:23:15
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include <stdio.h>
#include <vector>
using namespace std;

#define max_el 500000
int n;
vector<int> v[max_el];

int get_hash(int nr) {
     return (nr % max_el);
}

int find_(int hash, int val) {
    vector<int>::iterator it;
    for (it = v[hash].begin(); it != v[hash].end(); it++) {
        if (*it == val) {
           return it - v[hash].begin();
        } 
    } 
    return -1;
}

void insert_(int val) {
     int hash = get_hash(val);
     if (find_(hash, val) == -1) {
        v[hash].push_back(val);
     }
}

void delete_(int val) {
     int hash = get_hash(val);
     int poz = find_(hash, val);
     if (poz != -1) {
        v[hash].erase(poz + v[hash].begin());        
     }  
}

void contains_(int val) {
     int hash = get_hash(val);
     if (find_(hash, val) == -1) {
        printf("0\n");
        return;
     }         
     printf("1\n");    
}

void print_tmp(int nr, int op, int el) {
     printf("\nnr: %d ; op: %d; el: %d\n", nr, op, el);
     for (int i = 0; i < max_el; i++) {
         if (v[i].size() != 0) {
            printf("<%d ;", i);
            vector<int>::iterator it;
            for (it = v[i].begin(); it != v[i].end(); it++) {
                printf("%d, ", *it);
            }                
            printf(">\n");
         }
     }
}

void citeste() {
      int a, b;
      scanf("%d", &n);
      for (int i = 0; i < n; i++) {
          scanf("%d%d", &a, &b);
          //print_tmp(i, a, b);
          switch (a) {
                 case 1:
                      insert_(b);
                      break;
                 case 2:
                      delete_(b);
                      break;
                 case 3:
                      contains_(b);
                      break;
                 default:
                      break;
          }                      
      }     
}

int main() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    
    citeste();
    
    return 0;
}