結果

問題 No.2195 AND Set
ユーザー tentententen
提出日時 2023-08-08 18:00:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,200 ms / 2,000 ms
コード長 2,765 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 89,552 KB
実行使用メモリ 66,648 KB
最終ジャッジ日時 2024-04-26 10:31:28
合計ジャッジ時間 18,465 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
36,940 KB
testcase_01 AC 49 ms
37,128 KB
testcase_02 AC 904 ms
61,876 KB
testcase_03 AC 1,126 ms
65,636 KB
testcase_04 AC 827 ms
60,312 KB
testcase_05 AC 980 ms
62,916 KB
testcase_06 AC 966 ms
63,376 KB
testcase_07 AC 725 ms
63,360 KB
testcase_08 AC 1,004 ms
63,540 KB
testcase_09 AC 676 ms
51,944 KB
testcase_10 AC 1,135 ms
66,376 KB
testcase_11 AC 1,200 ms
66,572 KB
testcase_12 AC 960 ms
66,420 KB
testcase_13 AC 1,143 ms
66,044 KB
testcase_14 AC 1,122 ms
66,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        HashSet<Integer> current = new HashSet<>();
        int[] counts = new int[30];
        while (n-- > 0) {
            int type = sc.nextInt();
            if (type == 1) {
                int x = sc.nextInt();
                if (current.contains(x)) {
                    continue;
                }
                current.add(x);
                for (int i = 0; i < 30; i++) {
                    counts[i] += x % 2;
                    x >>= 1;
                }
            } else if (type == 2) {
                int x = sc.nextInt();
                if (!current.contains(x)) {
                    continue;
                }
                current.remove(x);
                for (int i = 0; i < 30; i++) {
                    counts[i] -= x % 2;
                    x >>= 1;
                }
            } else {
                if (current.size() == 0) {
                    sb.append("-1\n");
                    continue;
                }
                int ans = 0;
                for (int i = 0; i < 30; i++) {
                    if (counts[i] == current.size()) {
                        ans += (1 << i);
                    }
                }
                sb.append(ans).append("\n");
            }
        }
        System.out.print(sb);
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0