Cod sursa(job #1735731)

Utilizator danutbodbodnariuc danut danutbod Data 30 iulie 2016 19:36:58
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.68 kb
#include <cstdio>//var III 100p hash-uri cu vectori de elemente
#include <vector>
#define MAXN 209983 //recomandat un numar mai mare prim ! ex: 209983
using namespace std;
int n,op,x;
vector <int>H[MAXN];
void Add(int v){
    int S=v%MAXN;
    for(int i=0;i<H[S].size();i++)
        if(H[S][i]==v)
            return;
    H[S].push_back(v);
}
void Delete(int v){
    int S=v%MAXN;
    for(int i=0;i<H[S].size();i++)
        if(H[S][i]==v)
        {
            H[S].erase(H[S].begin()+i);
            return;
        }
}
void Search(int v){
    int S=v%MAXN;
    for(int i=0;i<H[S].size();i++)
        if(H[S][i]==v)
        {
            printf("1\n");
            return;
        }
    printf("0\n");
}
int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&op,&x);
        if(op==1)Add(x);
        else if(op==2)Delete(x);
        else Search(x);
    }
    return 0;
}
//var II
//#include <cstdio>//80p cu map - timp depasit
//#include <map> //arbore echilibrat binar O(log(n)) - operatiile
//using namespace std;
//int n,op,x,i,NR;
//map <int, int> M;
//
//int main()
//{
//    freopen("hashuri.in", "r", stdin);
//    freopen("hashuri.out", "w", stdout);
//    scanf("%d ", &n);
//    for (i = 1; i <= n; i++)
//    {
//        scanf("%d %d ", &op, &x);
//        if (op == 1 && M.find(x)==M.end()) M[x] = ++NR;
//        if (op == 2) M.erase(x);
//        if (op == 3) printf("%d\n", M.find(x) != M.end());
//    }
//
//    return 0;
//}
//var I
//#include <cstdio>//80p  cu set - timp depasit
//#include <set>
//using namespace std;
//int n,op,x,i;
//set<int> A; //arbore echilibrat binar operatii O(log(n))-insert, erase,find
//
//int main()
//{
//    freopen("hashuri.in", "r", stdin);
//    freopen("hashuri.out", "w", stdout);
//    scanf("%d", &n);
//    for(i=1;i<=n;i++)
//    {
//        scanf("%d %d", &op, &x);
//        if (op == 1)
//        {   A.insert(x); continue; }
//        if (op == 2)
//        {   A.erase(x);  continue; }
//        printf("%d\n", A.find(x) != A.end());
//    }
//   return 0;
//}



//#include <iostream>
//#include <stdio.h>
//#include <fstream>
//#include <vector>
//using namespace std;
//ifstream f("hashuri.in");
//ofstream g("hashuri.out");
//
//
//vector<int> G[66001];
//int key=66001;
//
//vector<int>::iterator find_element(int val,int list)
//{
//    vector<int>::iterator it;
//    for( it=G[list].begin(); it < G[list].end(); it++)
//    if(*it==val)
//    return it;
//
//    return G[list].end();
//
//}
//
//void insert_element(int val)
//{
//    int list=val%key;
//    if(find_element(val,list)==G[list].end())
//    {
//        G[list].push_back(val);
//    }
//}
//
//void erase_element(int val)
//{
//    int list=val%key;
//     vector<int>::iterator it = find_element(val,list);
//
//    if(it!=G[list].end())
//    G[list].erase(it);
//
//}
//
//void bool_exist(int val)
//{
//    int list=val%key;
//    vector<int>::iterator it = find_element(val,list);
//    if(it==G[list].end())
//    g<<"0"<<'\n';
//    else
//    g<<"1"<<'\n';
//}
//
//int main()
//{
//    int nrOperatii;
//    int op;
//    int val;
//
//    f>>nrOperatii;
//
//    for(int i=1;i<=nrOperatii;i++)
//    {
//        f>>op>>val;
//        if(op==1)
//        insert_element(val);
//        else
//        {
//            if(op==2)
//            erase_element(val);
//            else
//            {
//
//                bool_exist(val);
//            }
//        }
//    }
//
//    f.close();
//    g.close();
//
//    return 0;
//}