結果

問題 No.1240 Or Sum of Xor Pair
ユーザー tamatotamato
提出日時 2020-09-25 23:30:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,689 ms / 2,000 ms
コード長 4,223 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 87,560 KB
実行使用メモリ 287,716 KB
最終ジャッジ日時 2023-09-10 17:07:50
合計ジャッジ時間 30,823 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 436 ms
231,824 KB
testcase_01 AC 440 ms
231,256 KB
testcase_02 AC 439 ms
231,580 KB
testcase_03 AC 488 ms
232,708 KB
testcase_04 AC 488 ms
232,624 KB
testcase_05 AC 494 ms
232,672 KB
testcase_06 AC 478 ms
232,428 KB
testcase_07 AC 485 ms
232,436 KB
testcase_08 AC 478 ms
232,304 KB
testcase_09 AC 479 ms
232,744 KB
testcase_10 AC 597 ms
237,052 KB
testcase_11 AC 541 ms
237,876 KB
testcase_12 AC 653 ms
240,976 KB
testcase_13 AC 670 ms
239,468 KB
testcase_14 AC 603 ms
241,276 KB
testcase_15 AC 1,604 ms
267,756 KB
testcase_16 AC 1,329 ms
268,168 KB
testcase_17 AC 1,149 ms
268,156 KB
testcase_18 AC 1,372 ms
268,148 KB
testcase_19 AC 1,191 ms
268,372 KB
testcase_20 AC 1,496 ms
268,800 KB
testcase_21 AC 1,538 ms
268,228 KB
testcase_22 AC 1,510 ms
268,152 KB
testcase_23 AC 1,079 ms
268,436 KB
testcase_24 AC 1,418 ms
268,160 KB
testcase_25 AC 949 ms
268,380 KB
testcase_26 AC 1,689 ms
267,896 KB
testcase_27 AC 450 ms
231,932 KB
testcase_28 AC 445 ms
231,256 KB
testcase_29 AC 505 ms
234,396 KB
testcase_30 AC 584 ms
232,612 KB
testcase_31 AC 551 ms
233,188 KB
testcase_32 AC 897 ms
287,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import Counter
    input = sys.stdin.readline

    def plus(a, b):
        return a+b

    def f(A, B):
        return [a + b for a, b in zip(A, B)]
    ident = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

    class SegmentTree:
        def __init__(self, A, initialize=True, segfunc=f, ident=ident):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.segfunc = segfunc
            self.ident = ident
            if initialize:
                self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.ident] * (self.N0 * 2)

        def update(self, i, x):
            i += self.N0 - 1
            self.data[i] = x
            for _ in range(self.LV):
                i >>= 1
                self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            ret_l = self.ident
            ret_r = self.ident
            while l < r:
                if l & 1:
                    ret_l = self.segfunc(ret_l, self.data[l])
                    l += 1
                if r & 1:
                    ret_r = self.segfunc(self.data[r - 1], ret_r)
                    r -= 1
                l >>= 1
                r >>= 1
            return self.segfunc(ret_l, ret_r)

        # return smallest i(l <= i < r) s.t. check(A[i]) == True
        def binsearch(self, l, r, check):
            if not check(self.query(l, r)):
                return r
            l += self.N0 - 1
            val = self.ident
            while True:
                if check(self.segfunc(val, self.data[l])):
                    break
                if l & 1:
                    val = self.segfunc(val, self.data[l])
                    l += 1
                l >>= 1
            while l < self.N0:
                newval = self.segfunc(val, self.data[l * 2])
                if not check(newval):
                    val = newval
                    l = (l << 1) + 1
                else:
                    l <<= 1
            return l - self.N0 + 1

    N, X = map(int, input().split())
    A = list(map(int, input().split()))

    NN = 2**18
    ST = SegmentTree([ident for _ in range(NN)], initialize=False)
    ST_cnt = SegmentTree([0] * NN, initialize=False, segfunc=plus, ident=0)
    C = Counter(A)
    ST_array = [[0] * 18 for _ in range(2**18)]
    cnt_arrray = [0] * (2**18)
    for a in C:
        tmp = [0] * 18
        for i in range(18):
            tmp[i] = (1 - (a >> i & 1)) * C[a]
        ST_array[a] = tmp
        cnt_arrray[a] = C[a]
    ST = SegmentTree(ST_array)
    ST_cnt = SegmentTree(cnt_arrray, segfunc=plus, ident=0)
    ans = 0
    if X == 2**18:
        for a in A:
            zero_bit = []
            for i in range(18):
                if not a >> i & 1:
                    zero_bit.append(i)
            ans += (NN-1) * N
            num = ST.data[1]
            for j in zero_bit:
                ans -= (1 << j) * num[j]
            ans -= a
        print(ans // 2)
        exit()
    for a in A:
        zero_bit = []
        for i in range(18):
            if not a >> i & 1:
                zero_bit.append(i)
        current_node = 1
        for i in range(17, -1, -1):
            x = X >> i & 1
            aa = a >> i & 1
            if x == 1:
                y = aa
                small_node = current_node * 2 + y
                ans += (NN-1) * ST_cnt.data[small_node]
                num = ST.data[small_node]
                for j in zero_bit:
                    ans -= (1 << j) * num[j]
                current_node = current_node * 2 + (1 - y)
            else:
                current_node = current_node * 2 + aa
            #if a == 1:
            #    print(ST.data[current_node])
        ans -= a
        #print(a, ans)
    print(ans // 2)


if __name__ == '__main__':
    main()
0