結果

問題 No.631 Noelちゃんと電車旅行
ユーザー H3PO4H3PO4
提出日時 2022-02-01 09:56:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 3,721 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 95,588 KB
最終ジャッジ日時 2023-09-02 02:28:48
合計ジャッジ時間 8,921 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 355 ms
78,872 KB
testcase_01 AC 475 ms
95,588 KB
testcase_02 AC 484 ms
95,264 KB
testcase_03 AC 476 ms
95,508 KB
testcase_04 AC 473 ms
95,200 KB
testcase_05 AC 470 ms
95,256 KB
testcase_06 AC 286 ms
83,664 KB
testcase_07 AC 221 ms
86,064 KB
testcase_08 AC 400 ms
91,960 KB
testcase_09 AC 437 ms
85,264 KB
testcase_10 AC 360 ms
91,748 KB
testcase_11 AC 325 ms
87,524 KB
testcase_12 AC 369 ms
93,000 KB
testcase_13 AC 355 ms
80,368 KB
testcase_14 AC 239 ms
79,144 KB
testcase_15 AC 331 ms
81,604 KB
testcase_16 AC 73 ms
75,732 KB
testcase_17 AC 73 ms
75,816 KB
testcase_18 AC 77 ms
76,264 KB
testcase_19 AC 73 ms
75,596 KB
testcase_20 AC 74 ms
76,000 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)
    """

    __slots__ = ["segfunc", "ide_ele", "num", "data", "lazy"]

    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

    def all_prod(self):
        return self.data[1]


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.all_prod() + req_time * (N - 1))
0