結果

問題 No.2195 AND Set
ユーザー tentententen
提出日時 2024-04-30 14:51:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,053 ms / 2,000 ms
コード長 2,406 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 79,096 KB
実行使用メモリ 77,104 KB
最終ジャッジ日時 2024-04-30 14:51:32
合計ジャッジ時間 16,051 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,264 KB
testcase_01 AC 47 ms
50,032 KB
testcase_02 AC 825 ms
72,064 KB
testcase_03 AC 932 ms
75,344 KB
testcase_04 AC 746 ms
71,456 KB
testcase_05 AC 839 ms
72,820 KB
testcase_06 AC 881 ms
72,984 KB
testcase_07 AC 638 ms
63,216 KB
testcase_08 AC 823 ms
73,592 KB
testcase_09 AC 527 ms
65,972 KB
testcase_10 AC 973 ms
76,908 KB
testcase_11 AC 968 ms
76,524 KB
testcase_12 AC 973 ms
76,792 KB
testcase_13 AC 1,053 ms
76,628 KB
testcase_14 AC 1,028 ms
77,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] counts = new int[30];
        Set<Integer> store = new HashSet<>();
        StringBuilder sb = new StringBuilder();
        while (n-- > 0) {
            int type = sc.nextInt();
            if (type == 1) {
                int x = sc.nextInt();
                if (store.contains(x)) {
                    continue;
                }
                store.add(x);
                for (int i = 0; i < 30; i++) {
                    counts[i] += x % 2;
                    x >>= 1;
                }
            } else if (type == 2) {
                int x = sc.nextInt();
                if (!store.contains(x)) {
                    continue;
                }
                store.remove(x);
                for (int i = 0; i < 30; i++) {
                    counts[i] -= x % 2;
                    x >>= 1;
                }
            } else {
                if (store.size() == 0) {
                    sb.append("-1\n");
                    continue;
                }
                int ans = 0;
                for (int i = 0; i < 30; i++) {
                    if (counts[i] == store.size()) {
                        ans += (1 << i);
                    }
                }
                sb.append(ans).append("\n");
            }
        }
        System.out.print(sb);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0