Pagini recente » Cod sursa (job #1509971) | Borderou de evaluare (job #1036991) | Cod sursa (job #2798295) | Cod sursa (job #1576168) | Cod sursa (job #2756835)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
final Scanner in = new Scanner(new BufferedReader(new FileReader("hashuri.in")));
final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("hashuri.out")));
final Hash hash = new Hash();
final int n = in.nextInt();
for (int i = 0; i < n; ++i) {
final int op = in.nextInt();
final int x = in.nextInt();
switch (op) {
case 1:
hash.add(x);
break;
case 2:
hash.remove(x);
break;
case 3:
final boolean contains = hash.contains(x);
out.println(contains ? 1 : 0);
}
}
in.close();
out.close();
}
private static class Hash {
private final int hashModule = 666013;
private final List<List<Integer>> buckets = new ArrayList<>(hashModule);
public Hash() {
for (int i = 0; i < hashModule; ++i) {
buckets.add(new ArrayList<>());
}
}
public boolean add(Integer x) {
final int bucket = hashValue(x);
if (buckets.get(bucket) == null) {
final List<Integer> bucketList = new ArrayList<>();
bucketList.add(x);
buckets.set(bucket, bucketList);
return true;
} else {
if (!buckets.get(bucket).contains(x)) {
buckets.get(bucket).add(x);
return true;
}
return false;
}
}
public boolean contains(Integer x) {
return getBucket(x)
.map(b -> b.contains(x))
.orElse(false);
}
public boolean remove(Integer x) {
return getBucket(x)
.map(b -> b.remove(x))
.orElse(false);
}
private Optional<List<Integer>> getBucket(Integer x) {
final int bucketIdx = hashValue(x);
return Optional.ofNullable(buckets.get(bucketIdx));
}
private int hashValue(Integer x) {
return x % hashModule;
}
}
}