結果

問題 No.631 Noelちゃんと電車旅行
ユーザー H3PO4H3PO4
提出日時 2021-05-28 09:40:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 509 ms / 2,000 ms
コード長 3,632 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 102,440 KB
最終ジャッジ日時 2024-04-10 08:08:18
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 347 ms
78,392 KB
testcase_01 AC 509 ms
102,176 KB
testcase_02 AC 504 ms
102,440 KB
testcase_03 AC 501 ms
101,860 KB
testcase_04 AC 494 ms
101,904 KB
testcase_05 AC 504 ms
102,004 KB
testcase_06 AC 273 ms
86,304 KB
testcase_07 AC 208 ms
88,880 KB
testcase_08 AC 415 ms
98,528 KB
testcase_09 AC 435 ms
87,748 KB
testcase_10 AC 355 ms
98,196 KB
testcase_11 AC 344 ms
89,928 KB
testcase_12 AC 378 ms
100,044 KB
testcase_13 AC 345 ms
80,440 KB
testcase_14 AC 230 ms
78,080 KB
testcase_15 AC 325 ms
81,944 KB
testcase_16 AC 43 ms
59,648 KB
testcase_17 AC 46 ms
60,032 KB
testcase_18 AC 47 ms
60,928 KB
testcase_19 AC 44 ms
59,648 KB
testcase_20 AC 45 ms
60,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from array import array

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 = array('q', [ide_ele] * 2 * self.num)
        self.lazy = array('q', [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.data[1] + req_time * (N - 1))
0