Pagini recente » Cod sursa (job #2837758) | Cod sursa (job #720959) | Cod sursa (job #2641457) | Cod sursa (job #2981094) | Cod sursa (job #2850601)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
const int MOD = 500009;
struct node{
int value;
node* neighbour;
};
node* theArray[MOD + 5];
long long getHashedValue(long long nr)
{
return (nr * (nr % 3)) % MOD;
}
void addNumber(int number,long long index)
{
if(theArray[index] == NULL)
{
node* newNode = new node();
newNode ->neighbour = NULL;
theArray[index] = newNode;
}
bool wasFound = false;
while(theArray[index] ->neighbour != NULL)
{
theArray[index] = theArray[index] ->neighbour;
if(theArray[index] ->value == number)
wasFound = true;
}
if(wasFound == false)
{
node* newNode = new node();
newNode ->value = number;
theArray[index] ->neighbour = newNode;
}
}
void deleteNumber(int number,long long index)
{
bool wasFound = false;
if(theArray[index] == NULL)
{
return ;
}
node* lastNode = theArray[index];
while(theArray[index] ->neighbour != NULL)
{
lastNode = theArray[index];
theArray[index] = theArray[index] ->neighbour;
if(theArray[index] ->value == number)
{
wasFound = true;
break;
}
}
if(wasFound == true)
{
lastNode ->neighbour = theArray[index] ->neighbour;
delete theArray[index];
}
}
bool findNumber(int number, long long index)
{
if(theArray[index] == NULL)
return false;
while(theArray[index] ->neighbour != NULL)
{
theArray[index] = theArray[index] ->neighbour;
if(theArray[index] ->value == number)
return true;
}
return false;
}
int main()
{
int n;
fin>>n;
while(n--)
{
int command;
long long number;
fin>>command>>number;
long long index = getHashedValue(number);
if(command == 1)
addNumber(number, index);
if(command == 2)
deleteNumber(number, index);
if(command == 3)
{
bool wasFound = findNumber(number, index);
fout<<wasFound<<'\n';
}
}
return 0;
}