Cod sursa(job #1446038)

Utilizator dm1sevenDan Marius dm1seven Data 31 mai 2015 19:14:43
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.47 kb
/*
 * e_030_hashuri.cpp
 *
 *  Created on: May 31, 2015
 *      Author: Marius
 */

#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
using namespace std;

namespace e_030_hashuri_nms
{
    int N, n, m1, m2;
    //vector<set<int>> hash_set;
    map<int, set<int>> hash_map_set;
    
    inline int hash_func1(int x)
    {
        return x / m1;
    }
    
    inline int hash_func2(int x)
    {
        int xm1 = x / m1;
        return xm1 * (m2 + 1) + xm1/m2;
    }
    
    inline int hash_size1() 
    {
        return m1 + 1;
    }
    
    inline int hash_size2()
    {
        return (m1 + 1) * (m2 + 1);
    }
}

int main()
{
    using namespace e_030_hashuri_nms;
    
    ifstream ifs("hashuri.in");
    ofstream ofs("hashuri.out");
    
    ifs >> N;
    n = 2000000000;
    m1 = (int) sqrt(n) + 1;
    m2 = (int) sqrt(m1) + 1;
    
    //int sz = hash_size1();
    //hash_set.resize(sz);
    
    for (int i = 1; i <= N; i++)
    {
        int op, x;
        ifs >> op >> x;
        int pos = hash_func1(x); //the position in the hash_set
        map<int, set<int>>::iterator it = hash_map_set.find(pos);
        bool pos_found_in_map = true;
        if (it == hash_map_set.end()) pos_found_in_map = false;//not found        
        
        switch (op)
        {
        case 1:
            //hash_set[pos].insert(x);
            if (!pos_found_in_map)
            {
                set<int> set_;
                set_.insert(x);
                hash_map_set.insert(make_pair(pos, set_));
            }
            else
            {
                set<int>& set_ = it->second;
                set_.insert(x);
            }
            break;
        case 2:
            //hash_set[pos].erase(x);
            if (pos_found_in_map)
            {
                set<int>& set_ = it->second;
                set_.erase(x);
            }
            break;
        default:
            //set<int>::iterator it = hash_set[pos].find(x);
            //if (it != hash_set[pos].end()) in_set = 1;            
            int in_set = 0;
            if (pos_found_in_map)
            {
                set<int>& set_ = it->second;
                set<int>::iterator it = set_.find(x);
                if (it != set_.end()) in_set = 1;
            }
            ofs << in_set << '\n';
        }
    }
    
    ifs.close();
    ofs.close();
    
    return 0;
}