結果

問題 No.2195 AND Set
ユーザー FromBooskaFromBooska
提出日時 2023-03-07 11:04:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 738 ms / 2,000 ms
コード長 1,267 bytes
コンパイル時間 859 ms
コンパイル使用メモリ 81,552 KB
実行使用メモリ 87,144 KB
最終ジャッジ日時 2023-10-18 05:27:24
合計ジャッジ時間 12,567 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,348 KB
testcase_01 AC 37 ms
53,348 KB
testcase_02 AC 622 ms
80,052 KB
testcase_03 AC 645 ms
82,220 KB
testcase_04 AC 588 ms
80,136 KB
testcase_05 AC 682 ms
81,068 KB
testcase_06 AC 729 ms
82,680 KB
testcase_07 AC 626 ms
78,500 KB
testcase_08 AC 708 ms
81,368 KB
testcase_09 AC 601 ms
78,892 KB
testcase_10 AC 732 ms
87,144 KB
testcase_11 AC 721 ms
86,788 KB
testcase_12 AC 738 ms
86,992 KB
testcase_13 AC 725 ms
86,996 KB
testcase_14 AC 733 ms
86,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# セグメントツリーでfuncをANDにすればいいか
# いや、セグメントツリーにするリストがない、または、長くなりすぎる
# setでどの数字があるかを管理、31桁のバイナリに1が何回出てきたかを管理でどうか

binary_count = [0]*33
visited = set()
add = 0
Q = int(input())
for q in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        x = query[1]
        if x not in visited:
            visited.add(x)
            add += 1
            for shift in range(32):
                if x>>shift & 1 == 1:
                    binary_count[32-shift] += 1
    elif query[0] == 2:
        x = query[1]
        if x in visited:
            visited.discard(x)
            add -= 1
            for shift in range(32):
                if x>>shift & 1 == 1:
                    binary_count[32-shift] -= 1
    elif query[0] == 3:
        if len(visited) == 0:
            print(-1)
        elif add == 0:
            print(0)
        else:
            ans = 0
            for i in range(32, -1, -1):
                if binary_count[i] == add:
                    ans += 2**(32-i)
            print(ans)
        
    #print('add', add, 'visited', visited, 'binary_count', binary_count)

0