結果

問題 No.2611 Count 01
ユーザー MitarushiMitarushi
提出日時 2024-01-19 18:03:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,189 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 187,728 KB
最終ジャッジ日時 2024-01-19 19:11:31
合計ジャッジ時間 10,225 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 498 ms
42,932 KB
testcase_01 AC 496 ms
42,928 KB
testcase_02 AC 497 ms
42,928 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

class SegmentTree:
    def __init__(self, n, e):
        n2 = 1
        while n2 < n:
            n2 *= 2
        self.n = n2
        self.data = [e] * (n2 * 2)

    def build(self, a):
        for i, x in enumerate(a):
            self.data[i + self.n] = x
        for i in range(self.n - 1, 0, -1):
            self.data[i] = self.data[i * 2] + self.data[i * 2 + 1]

    def update(self, i, x):
        k = i + self.n
        self.data[k] = x
        while k > 1:
            k //= 2
            self.data[k] = self.data[k * 2] + self.data[k * 2 + 1]

    def query(self, l, r, ls, rs):
        l += self.n
        r += self.n
        while l < r:
            if l & 1:
                ls += self.data[l]
                l += 1
            if r & 1:
                r -= 1
                rs = self.data[r] + rs
            l //= 2
            r //= 2
        return ls + rs


MOD = 998244353

class Matrix:
    def __init__(self, a):
        self.a = a

    def __add__(self, other):
        return Matrix(self.a @ other.a % MOD)

node = [
    Matrix(np.array([
        [1, 0, 0, 0, 0, 0],
        [1, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0],
        [0, 0, 0, 1, 0, 0],
        [0, 0, 0, 1, 1, 0],
        [0, 0, 0, 1, 1, 1],
    ], dtype=np.int64)),
    Matrix(np.array([
        [1, 0, 0, 0, 0, 0],
        [1, 1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0],
        [1, 1, 0, 1, 0, 0],
        [0, 0, 1, 0, 1, 0],
        [0, 0, 1, 0, 1, 1],
    ], dtype=np.int64)),
]

identity = Matrix(np.array([
    [1, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 1],
], dtype=np.int64))


n, q = map(int, input().split())
s = input()
s_seq = [int(x) for x in s]
st = SegmentTree(n, identity)
st.build([node[x] for x in s_seq])
for _ in range(q):
    t, *args = map(int, input().split())
    if t == 1:
        i = args[0] - 1
        s_seq[i] = 1 - s_seq[i]
        st.update(i, node[s_seq[i]])
    else:
        l, r = args
        ans = st.query(l - 1, r, Matrix(np.array([0, 0, 0, 0, 0, 1], dtype=np.int64)), Matrix(np.array([1, 0, 0, 0, 0, 0], dtype=np.int64)))
        print(ans.a)
0