結果

問題 No.2611 Count 01
ユーザー MitarushiMitarushi
提出日時 2024-01-07 15:35:32
言語 Nim
(2.0.2)
結果
AC  
実行時間 877 ms / 6,000 ms
コード長 3,281 bytes
コンパイル時間 3,971 ms
コンパイル使用メモリ 67,028 KB
実行使用メモリ 229,952 KB
最終ジャッジ日時 2024-01-19 19:09:40
合計ジャッジ時間 22,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 864 ms
229,952 KB
testcase_04 AC 877 ms
229,952 KB
testcase_05 AC 870 ms
229,952 KB
testcase_06 AC 850 ms
229,952 KB
testcase_07 AC 853 ms
229,952 KB
testcase_08 AC 857 ms
229,952 KB
testcase_09 AC 857 ms
229,952 KB
testcase_10 AC 846 ms
229,952 KB
testcase_11 AC 857 ms
229,952 KB
testcase_12 AC 860 ms
229,952 KB
testcase_13 AC 857 ms
229,952 KB
testcase_14 AC 852 ms
229,952 KB
testcase_15 AC 853 ms
229,952 KB
testcase_16 AC 849 ms
229,952 KB
testcase_17 AC 856 ms
229,952 KB
testcase_18 AC 859 ms
229,952 KB
testcase_19 AC 860 ms
229,952 KB
testcase_20 AC 866 ms
229,952 KB
testcase_21 AC 869 ms
229,952 KB
testcase_22 AC 862 ms
229,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, deques
# import nimprof

type stdinReader = object
    buf: Deque[string]

proc newStdinReader(): stdinReader =
    let buf = stdin.readAll.split.toDeque
    result = stdinReader(buf: buf)

proc readString(sr: var stdinReader): string =
    sr.buf.popFirst

proc readInt(sr: var stdinReader): int =
    readString(sr).parseInt

type SegmentTree[T] = object
    n: int
    data: seq[T]
    e: T

func init[T](n: int, e: T): SegmentTree[T] =
    var n2 = 1
    while n2 < n:
        n2 *= 2
    SegmentTree[T](n: n2, data: newSeqWith[T](n2 * 2, e), e: e)

proc build[T](st: var SegmentTree[T], a: seq[T]) =
    for i, x in a:
        st.data[i + st.n] = x
    for i in countdown(st.n - 1, 1):
        st.data[i] = st.data[i * 2] + st.data[i * 2 + 1]

proc update[T](st: var SegmentTree[T], i: int, x: T) =
    var k = i + st.n
    st.data[k] = x
    while k > 1:
        k = k div 2
        st.data[k] = st.data[k * 2] + st.data[k * 2 + 1]

func query[T, U](st: SegmentTree[T], l: int, r: int, ls, rs: U): auto =
    var 
        l = l + st.n
        r = r + st.n
    var 
        ls = ls
        rs = rs
    while l < r:
        if (l and 1) == 1:
            ls = ls + st.data[l]
            l += 1
        if (r and 1) == 1:
            r -= 1
            rs = st.data[r] + rs
        l = l div 2
        r = r div 2
    ls + rs

const MOD = 998244353

type Vec = array[6, int]
type Matrix = array[6, array[6, int]]

func `+`(a, b: Matrix): Matrix =
    for i in 0 ..< a.len:
        for j in 0..i:
            for k in j..i:
                result[i][j] += a[i][k] * b[k][j]
    for i in 0 ..< a.len:
        for j in 0..i:
            result[i][j] = result[i][j] mod MOD

func `+`(a: Vec, b: Matrix): Vec =
    for i in 0 ..< a.len:
        for j in 0..i:
            result[j] += a[i] * b[i][j]
    for i in 0 ..< a.len:
        result[i] = result[i] mod MOD

func `+`(a: Matrix, b: Vec): Vec =
    for i in 0 ..< a.len:
        for j in 0..i:
            result[i] += a[i][j] * b[j]
    for i in 0 ..< a.len:
        result[i] = result[i] mod MOD

func `+`(a, b: Vec): int =
    for i in 0..<a.len:
        result += a[i] * b[i]
    result = result mod MOD

let node: array[2, Matrix] = [
    [
        [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],
    ],
    [
        [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],
    ]
]

let identity: Matrix = [
    [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],
]

var reader = newStdinReader()
let (n, q) = (reader.readInt, reader.readInt)
let s = reader.readString
var sSeq = s.mapIt(int(it) - int('0'))
var st = init[Matrix](n, identity)
st.build(sSeq.mapIt(node[it]))
for _ in 0..<q:
    let t = reader.readInt
    if t == 1:
        let i = reader.readInt - 1
        sSeq[i] = 1 - sSeq[i]
        st.update(i, node[sSeq[i]])
    else:
        let (l, r) = (reader.readInt - 1, reader.readInt)
        let ans = st.query(l, r, [0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0])
        echo ans
0