結果

問題 No.619 CardShuffle
ユーザー maspymaspy
提出日時 2020-04-09 02:53:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 968 ms / 3,000 ms
コード長 2,982 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 123,772 KB
最終ジャッジ日時 2023-09-26 13:00:07
合計ジャッジ時間 15,420 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,264 KB
testcase_01 AC 70 ms
70,880 KB
testcase_02 AC 70 ms
71,356 KB
testcase_03 AC 70 ms
71,216 KB
testcase_04 AC 69 ms
71,316 KB
testcase_05 AC 69 ms
71,268 KB
testcase_06 AC 70 ms
71,380 KB
testcase_07 AC 69 ms
71,120 KB
testcase_08 AC 69 ms
71,296 KB
testcase_09 AC 70 ms
71,292 KB
testcase_10 AC 68 ms
71,252 KB
testcase_11 AC 71 ms
71,264 KB
testcase_12 AC 69 ms
71,360 KB
testcase_13 AC 70 ms
71,124 KB
testcase_14 AC 71 ms
71,140 KB
testcase_15 AC 70 ms
71,268 KB
testcase_16 AC 858 ms
113,220 KB
testcase_17 AC 355 ms
95,684 KB
testcase_18 AC 878 ms
113,484 KB
testcase_19 AC 381 ms
95,208 KB
testcase_20 AC 525 ms
111,596 KB
testcase_21 AC 870 ms
123,308 KB
testcase_22 AC 370 ms
98,392 KB
testcase_23 AC 868 ms
123,412 KB
testcase_24 AC 394 ms
100,100 KB
testcase_25 AC 609 ms
123,772 KB
testcase_26 AC 715 ms
96,204 KB
testcase_27 AC 313 ms
91,728 KB
testcase_28 AC 819 ms
99,276 KB
testcase_29 AC 319 ms
91,888 KB
testcase_30 AC 394 ms
95,540 KB
testcase_31 AC 68 ms
71,164 KB
testcase_32 AC 506 ms
112,648 KB
testcase_33 AC 968 ms
116,828 KB
testcase_34 AC 679 ms
98,500 KB
testcase_35 AC 489 ms
83,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7


class SegTree:
    """ segment tree with point modification and range product access. """
    data_unit = None
    @classmethod
    def data_f(cls, A, B):
        if A is None:
            return B
        if B is None:
            return A
        n = len(A)
        m = len(B)
        if (n, m) == (1, 1):  # A[0] * B[0] *
            return (A[0] * B[0] % MOD,)
        elif (n, m) == (1, 2):  # A[0] * B[0] + B[1] +
            return (A[0] * B[0] % MOD, B[1])
        elif (n, m) == (1, 3):  # A[0] * B[0] + B[1] + B[2] *
            return (A[0] * B[0] % MOD, B[1], B[2])
        elif (n, m) == (2, 1):  # A[0] + A[1] + B[0] *
            return (A[0], A[1], B[0])
        elif (n, m) == (2, 2):  # A[0] + A[1] + B[0] + B[1] +
            return (A[0], A[1] + B[0] + B[1])
        elif (n, m) == (2, 3):  # A[0] + A[1] + B[0] + B[1] + B[2] *
            return (A[0], A[1] + B[0] + B[1], B[2])
        elif (n, m) == (3, 1):  # A[0] + A[1] + A[2] * B[0] *
            return (A[0], A[1], A[2] * B[0] % MOD)
        elif (n, m) == (3, 2):  # A[0] + A[1] + A[2] * B[0] + B[1] +
            return (A[0], A[1] + A[2] * B[0] % MOD + B[1])
        elif (n, m) == (3, 3):  # A[0] + A[1] + A[2] * B[0] + B[1] + B[2] *
            return (A[0], A[1] + A[2] * B[0] % MOD + B[1], B[2])

    def __init__(self, N):
        self.N = N
        self.data = [self.data_unit] * (N + N)

    def build(self, raw_data):
        data = self.data
        f = self.data_f
        N = self.N
        data[N:] = raw_data[:]
        for i in range(N - 1, 0, -1):
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def set_val(self, i, x):
        data = self.data
        f = self.data_f
        i += self.N
        data[i] = x
        while i > 1:
            i >>= 1
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def fold(self, L, R):
        """ compute for [L, R) """
        vL = vR = self.data_unit
        data = self.data
        f = self.data_f
        L += self.N
        R += self.N
        while L < R:
            if L & 1:
                vL = f(vL, data[L])
                L += 1
            if R & 1:
                R -= 1
                vR = f(data[R], vR)
            L >>= 1
            R >>= 1
        return f(vL, vR)


def cards_to_tpl(i):
    x, y = C[2 * i: 2 * i + 2]
    if y == b'+':
        return (int(x), 0)
    else:
        return (int(x),)


N = int(readline())
N = (N + 1) // 2
seg = SegTree(N)

C = readline().split() + [b'+']
seg.build([cards_to_tpl(i) for i in range(N)])

Q = int(readline())
for _ in range(Q):
    t, x, y = readline().split()
    x = int(x) - 1
    y = int(y) - 1
    if t == b'!':
        C[x], C[y] = C[y], C[x]
        for i in (x // 2, y // 2):
            seg.set_val(i, cards_to_tpl(i))
    else:
        print(sum(seg.fold(x // 2, y // 2 + 1)) % MOD)
0