結果

問題 No.2762 Counting and Deleting
ユーザー fiblonariafiblonaria
提出日時 2024-05-31 13:49:53
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,126 bytes
コンパイル時間 406 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 67,444 KB
最終ジャッジ日時 2024-05-31 13:49:57
合計ジャッジ時間 2,624 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Matrix:
    def __init__(self, data):
        self.data = data
        self.rows = len(data)
        self.cols = len(data[0]) if data else 0

    def __add__(self, other):
        result = [[self.data[i][j] + other.data[i][j] for j in range(self.cols)] for i in range(self.rows)]
        return Matrix(result)

    def __mul__(self, other):
        result = [[sum(self.data[i][k] * other.data[k][j] for k in range(self.cols)) for j in range(other.cols)] for i in range(self.rows)]
        return Matrix(result)


class SegmentTree:
    def __init__(self, data, func=min):
        self.n = len(data)
        self.func = func
        self.tree = [None] * (2 * self.n)
        for i in range(self.n):
            self.tree[self.n + i] = data[i]
        for i in range(self.n - 1, 0, -1):
            self.tree[i] = self.func(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, index, value):
        pos = index + self.n
        self.tree[pos] = value
        while pos > 1:
            pos //= 2
            self.tree[pos] = self.func(self.tree[2 * pos], self.tree[2 * pos + 1])

    def query(self, left, right):
        res = self.default
        left += self.n
        right += self.n
        while left < right:
            if left % 2:
                res = self.func(self.tree[left], res)
                left += 1
            if right % 2:
                right -= 1
                res = self.func(res, self.tree[right])
            left //= 2
            right //= 2
        return res


N, Q = map(int, input().split())
S = input()
matrices = []
matrices.append(Matrix([[1, 1, 0], [0, 1, 0], [0, 0, 1]]))
matrices.append(Matrix([[1, 0, 0], [1, 1, 1], [0, 0, 1]]))
matrices.append(Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
matrice_array = [matrices(int(S[i])) for i in range(N)]
segment_tree = SegmentTree(matrice_array, func=lambda a, b: a * b)
for _ in range(Q):
    q, l, r = input().split()
    if q == '1':
        for i in range(l - 1, r):
            segment_tree.update(i, matrices[2])
    else:
        result = segment_tree.query(l - 1, r)
        print(result.data[1][2] + result.data[2][2])
0