結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー yuly3yuly3
提出日時 2020-09-13 15:06:46
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 6,252 bytes
コンパイル時間 6,470 ms
コンパイル使用メモリ 73,592 KB
実行使用メモリ 250,244 KB
最終ジャッジ日時 2023-09-03 15:43:58
合計ジャッジ時間 35,263 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,380 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import algorithm, deques, heapqueue, math, sets, sequtils, strutils, sugar, tables

proc input*(): string =
    return stdin.readLine
proc chmax*[T: SomeNumber](num0: var T, num1: T) =
    num0 = max(num0, num1)
proc chmin*[T: SomeNumber](num0: var T, num1: T) =
    num0 = min(num0, num1)
proc `%=`*[T: SomeInteger](num0: var T, num1: T) =
    num0 = num0 mod num1


proc bit_length(n: Natural): Natural =
    const BIT_SIZE = 24
    if n == 0:
      return 0
    let s = toBin(n, BIT_SIZE)
    return BIT_SIZE - s.find('1')


type
    LazySegmentTree*[T, K] = ref object
        LV: Natural
        N0: Positive
        ide_ele: T
        lazy_ide_ele: K
        data: seq[T]
        lazy_data: seq[K]
        fold: proc (a, b: T): T
        eval: proc (a: T, b: K): T
        merge: proc (a, b: K): K

proc initLazySegmentTree*[T, K](size: Positive, ide_ele: T, lazy_ide_ele: K, fold: proc (a, b: T): T, eval: proc (a: T, b: K): T, merge: proc (a, b: K): K): LazySegmentTree[T, K] =
    var
        LV = bit_length(size - 1)
        N0 = 1 shl LV
        data = newSeqWith(2*N0, ide_ele)
        lazy_data = newSeqWith(2*N0, lazy_ide_ele)
    return LazySegmentTree[T, K](LV: LV, N0: N0, ide_ele: ide_ele, lazy_ide_ele: lazy_ide_ele, data: data, lazy_data: lazy_data, fold: fold, eval: eval, merge: merge)

proc toLazySegmentTree*[T, K](init_value: openArray[T], ide_ele: T, lazy_ide_ele: K, fold: proc (a, b: T): T, eval: proc (a: T, b: K): T, merge: proc (a, b: K): K): LazySegmentTree[T, K] =
    var
        LV = bit_length(init_value.len - 1)
        N0 = 1 shl LV
        data = newSeqWith(2*N0, ide_ele)
        lazy_data = newSeqWith(2*N0, lazy_ide_ele)
    for i, x in init_value:
        data[i + N0 - 1] = x
    for i in countdown(N0 - 2, 0):
        data[i] = fold(data[2*i + 1], data[2*i + 2])
    return LazySegmentTree[T, K](LV: LV, N0: N0, ide_ele: ide_ele, lazy_ide_ele: lazy_ide_ele, data: data, lazy_data: lazy_data, fold: fold, eval: eval, merge: merge)

iterator gindex*[T, K](self: var LazySegmentTree[T, K], left, right: Natural): Natural =
    var
        L = (left + self.N0) shr 1
        R = (right + self.N0) shr 1
        lc = if (left and 1) == 1: 0 else: bit_length(L and -L)
        rc = if (right and 1) == 1: 0 else: bit_length(R and -R)
    for i in 0..<self.LV:
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L = L shr 1
        R = R shr 1

proc propagates*[T, K](self: var LazySegmentTree[T, K], ids: seq[Natural]) =
    var
        idx: Natural
        v: K
    for id in reversed(ids):
        idx = id - 1
        v = self.lazy_data[idx]
        if v == self.lazy_ide_ele:
            continue
        # v = v shr 1
        self.data[2*idx + 1] = self.eval(self.data[2*idx + 1], v)
        self.data[2*idx + 2] = self.eval(self.data[2*idx + 2], v)
        self.lazy_data[2*idx + 1] = self.merge(self.lazy_data[2*idx + 1], v)
        self.lazy_data[2*idx + 2] = self.merge(self.lazy_data[2*idx + 2], v)
        self.lazy_data[idx] = self.lazy_ide_ele

proc update*[T, K](self: var LazySegmentTree[T, K], left, right: Natural, x: K) =
    let ids = toSeq(self.gindex(left, right))
    self.propagates(ids)
    var
        L = left + self.N0
        R = right + self.N0
        # x = x
    
    while L < R:
        if (L and 1) == 1:
            self.lazy_data[L - 1] = self.merge(self.lazy_data[L - 1], x)
            self.data[L - 1] = self.eval(self.data[L - 1], x)
            inc L
        if (R and 1) == 1:
            dec R
            self.lazy_data[R - 1] = self.merge(self.lazy_data[R - 1], x)
            self.data[R - 1] = self.eval(self.data[R - 1], x)
        L = L shr 1
        R = R shr 1
        # x = x shl 1
    var idx: Natural
    for id in ids:
        idx = id - 1
        self.data[idx] = self.fold(self.data[2*idx + 1], self.data[2*idx + 2])

proc query*[T, K](self: var LazySegmentTree[T, K], left, right: Natural): T =
    self.propagates(toSeq(self.gindex(left, right)))
    var
        L = left + self.N0
        R = right + self.N0
        res = self.ide_ele
    
    while L < R:
        if (L and 1) == 1:
            res = self.fold(res, self.data[L - 1])
            inc L
        if (R and 1) == 1:
            dec R
            res = self.fold(res, self.data[R - 1])
        L = L shr 1
        R = R shr 1
    return res


const MOD = 10^18 + 9
var
    queries: seq[(int, int, int)]
    val_set: HashSet[int]
    sorted_val: seq[int]
    init_val: seq[(int, int)]
    compress: Table[int, int]
    five_lazy_seg_tree: array[5, LazySegmentTree[(int, int), int]]

proc solve() =
    let
        _ = input().parseInt
        Q = input().parseInt
    val_set = initHashSet[int]()
    var xi, li, ri: int
    for _ in 0..<Q:
        (xi, li, ri) = input().split.map(parseInt)
        val_set.incl(li)
        val_set.incl(ri)
        queries.add((xi, li, ri))
    
    sorted_val = toSeq(sorted(toSeq(val_set.items)))
    for i in 0..<sorted_val.len - 1:
        init_val.add((0, sorted_val[i + 1] - sorted_val[i]))
    init_val.add((0, 1))

    for i in 0..4:
        five_lazy_seg_tree[i] = toLazySegmentTree(init_val, (0, 0), -1, (a, b) => ((a[0] + b[0]) mod MOD, a[1] + b[1]),
                                                  (a, b) => ((a[0] + a[1])*b mod MOD, a[1]), (a, b) => b)

    compress = initTable[int, int]()
    for idx, val in sorted_val:
        compress[val] = idx

    var ans = [0, 0, 0, 0, 0]
    for (xi, li, ri) in queries:
        if xi == 0:
            var X = [0, 0, 0, 0, 0]
            for i in 0..4:
                X[i] = five_lazy_seg_tree[i].query(compress[li], compress[ri] + 1)[0]
            let v = max(X)
            if X.count(v) != 1:
                continue
            let idx = X.find(v)
            ans[idx] += max(X)
            ans[idx] %= MOD
        else:
            for i in 0..4:
                if i == xi - 1:
                    continue
                five_lazy_seg_tree[i].update(compress[li], compress[ri] + 1, 0)
            five_lazy_seg_tree[xi - 1].update(compress[li], compress[ri] + 1, 1)
    
    for i in 0..4:
        ans[i] += five_lazy_seg_tree[i].query(0, compress.len)[0]
        ans[i] %= MOD
    echo ans.join(" ")

when is_main_module:
    solve()
0