結果
問題 | No.619 CardShuffle |
ユーザー | maspy |
提出日時 | 2020-04-09 02:53:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 952 ms / 3,000 ms |
コード長 | 2,982 bytes |
コンパイル時間 | 173 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 122,936 KB |
最終ジャッジ日時 | 2024-07-19 07:24:29 |
合計ジャッジ時間 | 14,256 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,480 KB |
testcase_01 | AC | 47 ms
52,992 KB |
testcase_02 | AC | 48 ms
53,632 KB |
testcase_03 | AC | 45 ms
53,376 KB |
testcase_04 | AC | 43 ms
53,120 KB |
testcase_05 | AC | 44 ms
52,864 KB |
testcase_06 | AC | 45 ms
53,632 KB |
testcase_07 | AC | 45 ms
53,120 KB |
testcase_08 | AC | 43 ms
53,504 KB |
testcase_09 | AC | 44 ms
53,248 KB |
testcase_10 | AC | 43 ms
52,736 KB |
testcase_11 | AC | 43 ms
52,864 KB |
testcase_12 | AC | 43 ms
53,120 KB |
testcase_13 | AC | 43 ms
53,248 KB |
testcase_14 | AC | 43 ms
53,376 KB |
testcase_15 | AC | 43 ms
52,736 KB |
testcase_16 | AC | 850 ms
110,532 KB |
testcase_17 | AC | 361 ms
93,712 KB |
testcase_18 | AC | 922 ms
111,572 KB |
testcase_19 | AC | 400 ms
94,352 KB |
testcase_20 | AC | 570 ms
111,552 KB |
testcase_21 | AC | 924 ms
120,756 KB |
testcase_22 | AC | 389 ms
97,768 KB |
testcase_23 | AC | 902 ms
120,676 KB |
testcase_24 | AC | 397 ms
97,140 KB |
testcase_25 | AC | 644 ms
122,936 KB |
testcase_26 | AC | 707 ms
95,068 KB |
testcase_27 | AC | 303 ms
90,752 KB |
testcase_28 | AC | 794 ms
95,836 KB |
testcase_29 | AC | 301 ms
90,752 KB |
testcase_30 | AC | 384 ms
94,592 KB |
testcase_31 | AC | 40 ms
52,352 KB |
testcase_32 | AC | 516 ms
112,280 KB |
testcase_33 | AC | 952 ms
115,776 KB |
testcase_34 | AC | 671 ms
94,636 KB |
testcase_35 | AC | 470 ms
81,732 KB |
ソースコード
#!/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)