Cod sursa(job #2703692)

Utilizator kitkat007Graphy kitkat007 Data 8 februarie 2021 23:53:29
Problema Hashuri Scor 30
Compilator java Status done
Runda Arhiva educationala Marime 1.72 kb
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class Main {

    public static class BRGIntHash {
        private static final int PRIME = 666013;

        public Vector<Integer>[] hashBuckets = new Vector[PRIME];

        public void push(int n) {
            int key = n % PRIME;
            if (contains(n)) return;
            if (hashBuckets[key] == null) {
                hashBuckets[key] = new Vector<>();
            }
            hashBuckets[key].add(n);
        }

        public void erase(int n) {
            int key = n % PRIME;
            if (hashBuckets[key] == null) {
                return;
            }
            hashBuckets[key].remove(Integer.valueOf(n));
        }

        public boolean contains(int n) {
            int key = n % PRIME;
            if (hashBuckets[key] == null) {
                return false;
            }
            for (Integer val : hashBuckets[key]) {
                if (val == n) {
                    return true;
                }
            }
            return false;
        }
    }


    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(new FileReader("hashuri.in"));
        PrintWriter pw = new PrintWriter("hashuri.out");
        Set<Integer> set = new HashSet<>();
        BRGIntHash hash = new BRGIntHash();
        int n = sc.nextInt();
        while (n-- > 0) {
            int op = sc.nextInt();
            int nbr = sc.nextInt();
            if (op == 1) hash.push(nbr);
            if (op == 2) hash.erase(nbr);
            if (op == 3) pw.println(hash.contains(nbr) ? 1 : 0);
        }
        pw.flush();
    }
}