Cod sursa(job #750325)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 21 mai 2012 21:18:26
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>
#include <vector>
#include <cstdlib>
#include <ctime>
#define LE 10005
#include <algorithm>
#define ll long long
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int ind [LE+100];
vector <int> keys[LE+100];
vector <int> :: iterator it;
int i,tip,val,n,poZ;





////
int get_poz(int key,int poz)
{
  if (poz*2<=LE)
    {
      if (key<=ind[poz])
        return get_poz(key,poz*2);
      else return get_poz(key,poz*2+1);
    }
  else return poz;
}

int push(int key)
{
  int k=get_poz(key,1);
  keys[get_poz(key,1)].push_back(key);
}

void build_tree(int st,int dr,int poz)
{
  int mij=(st+dr)/2;
  ind[poz]=mij;
  if (2*poz>LE) return;
  build_tree(st,mij,2*poz);
  build_tree(mij+1,dr,2*poz+1);
}

int Delete(int key)
{
  poZ=get_poz(key,1);
  for(it=keys[poZ].begin(); it!=keys[poZ].end(); ++it)
    if (*it==key)
      {
        keys[poZ].erase(it);
        break;
      }
}
int Search(int key)
{
  poZ=get_poz(key,1);

  for(it=keys[poZ].begin(); it!=keys[poZ].end(); ++it)
    if (*it==key)
      return 1;
  return 0;
}
int main()
{
  f>>n;
  build_tree(1,LE,1);

  for(i=1; i<=n; ++i)
    {
      f>>tip>>val;
      if (tip==1) push(val);
      if (tip==2) Delete(val);
      if (tip==3) g<<Search(val)<<'\n';
    }


  f.close();
  g.close();
  return 0;
}