結果

問題 No.631 Noelちゃんと電車旅行
ユーザー H3PO4H3PO4
提出日時 2021-05-28 09:35:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 773 ms / 2,000 ms
コード長 3,589 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 94,400 KB
最終ジャッジ日時 2024-04-10 08:07:36
合計ジャッジ時間 11,560 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 543 ms
79,728 KB
testcase_01 AC 751 ms
93,896 KB
testcase_02 AC 773 ms
94,176 KB
testcase_03 AC 757 ms
94,360 KB
testcase_04 AC 676 ms
94,400 KB
testcase_05 AC 717 ms
94,176 KB
testcase_06 AC 374 ms
82,848 KB
testcase_07 AC 261 ms
85,116 KB
testcase_08 AC 575 ms
90,752 KB
testcase_09 AC 615 ms
84,508 KB
testcase_10 AC 470 ms
90,524 KB
testcase_11 AC 422 ms
86,364 KB
testcase_12 AC 491 ms
92,036 KB
testcase_13 AC 504 ms
82,020 KB
testcase_14 AC 285 ms
78,424 KB
testcase_15 AC 467 ms
82,812 KB
testcase_16 AC 46 ms
64,112 KB
testcase_17 AC 46 ms
62,400 KB
testcase_18 AC 47 ms
61,060 KB
testcase_19 AC 49 ms
63,084 KB
testcase_20 AC 47 ms
63,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


class LazySegmentTree:
    """
    https://qiita.com/takayg1/items/b7b3f7d458915bcc7a4e から拝借しています。
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    add(l, r, x): 区間[l, r)にxを加算 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        num: n以上の最小の2のべき乗
        data: 値配列(1-index)
        lazy: 遅延配列(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.data = [ide_ele] * 2 * self.num
        self.lazy = [0] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.data[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])

    def gindex(self, l, r):
        """
        伝搬する対象の区間を求める
        lm: 伝搬する必要のある最大の左閉区間
        rm: 伝搬する必要のある最大の右開区間
        """
        l += self.num
        r += self.num
        lm = l >> (l & -l).bit_length()
        rm = r >> (r & -r).bit_length()

        while r > l:
            if l <= lm:
                yield l
            if r <= rm:
                yield r
            r >>= 1
            l >>= 1
        while l:
            yield l
            l >>= 1

    def propagates(self, *ids):
        """
        遅延伝搬処理
        ids: 伝搬する対象の区間
        """
        for i in reversed(ids):
            v = self.lazy[i]
            if not v:
                continue
            self.lazy[2 * i] += v
            self.lazy[2 * i + 1] += v
            self.data[2 * i] += v
            self.data[2 * i + 1] += v
            self.lazy[i] = 0

    def add(self, l, r, x):
        """
        区間[l, r)の値にxを加算
        l, r: index(0-index)
        x: additional value
        """
        *ids, = self.gindex(l, r)
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                self.lazy[l] += x
                self.data[l] += x
                l += 1
            if r & 1:
                self.lazy[r - 1] += x
                self.data[r - 1] += x
            r >>= 1
            l >>= 1
        for i in ids:
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1]) + self.lazy[i]

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        *ids, = self.gindex(l, r)
        self.propagates(*ids)

        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.data[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.data[r - 1])
            l >>= 1
            r >>= 1
        return res


N = int(input())
T = map(int, input().split())
M=int(input())

req_time = 3
init_val = [0] + [t - req_time * i for i, t in enumerate(T)]
seg = LazySegmentTree(init_val, max, 0)
for _ in range(M):
    l, r, d = map(int, input().split())
    seg.add(l, r + 1, d)
    print(seg.query(1, N + 1) + req_time * (N - 1))
0