結果

問題 No.631 Noelちゃんと電車旅行
ユーザー kohei2019kohei2019
提出日時 2022-07-27 23:31:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 663 ms / 2,000 ms
コード長 3,122 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 86,684 KB
実行使用メモリ 96,156 KB
最終ジャッジ日時 2023-09-24 12:08:36
合計ジャッジ時間 13,730 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 495 ms
88,052 KB
testcase_01 AC 643 ms
96,120 KB
testcase_02 AC 632 ms
96,124 KB
testcase_03 AC 663 ms
96,156 KB
testcase_04 AC 650 ms
96,120 KB
testcase_05 AC 645 ms
96,120 KB
testcase_06 AC 384 ms
85,548 KB
testcase_07 AC 286 ms
86,596 KB
testcase_08 AC 546 ms
92,872 KB
testcase_09 AC 604 ms
90,704 KB
testcase_10 AC 483 ms
92,304 KB
testcase_11 AC 431 ms
87,620 KB
testcase_12 AC 515 ms
94,164 KB
testcase_13 AC 496 ms
87,108 KB
testcase_14 AC 321 ms
83,504 KB
testcase_15 AC 447 ms
86,536 KB
testcase_16 AC 91 ms
76,328 KB
testcase_17 AC 89 ms
76,244 KB
testcase_18 AC 91 ms
76,248 KB
testcase_19 AC 86 ms
76,036 KB
testcase_20 AC 85 ms
76,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegTree():
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
    }

    FUNC = {
        'min': min,
        'max': max,
    }
    def __init__(self,ls,mode='min'): #葉の数N,要素ls
        self.default = self.DEFAULT[mode]
        self.func = self.FUNC[mode]
        self.N = len(ls)
        self.K = (self.N-1).bit_length()
        self.N0 = 1 << self.K 
        self.dat = [self.default]*(2**(self.K+1)) #全体の配列を用意(maxなら-inf,minなら+infで未構築を表現) 
        self.lazy = [0]*(2**(self.K+1)) #遅延評価
        for i in range(self.N): #葉の構築
            self.dat[2**self.K+i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N0-1,0,-1):
            self.dat[j] = self.func(self.dat[j<<1],self.dat[j<<1|1]) #親が持つ条件

    def leafvalue(self,x): #x番目の値を出力
        return self.query(x,x+1)

    def update_add(self,x,y): #x番目にyを足す
        return self.updatel_add(x,x+1,y)

    def gindex(self,l, r): #伝播するインデックス列挙
        L = l + self.N0; R = r + self.N0
        lm = (L // (L & -L)) >> 1
        rm = (R // (R & -R)) >> 1
        lsindex = []
        while L < R:
            if R <= rm:
                lsindex.append(R)
            if L <= lm:
                lsindex.append(L)
            L >>= 1; R >>= 1
        while L:
            lsindex.append(L)
            L >>= 1
        return lsindex

    def propagates(self,ids):
        for i in reversed(ids):
            v = self.lazy[i]
            if not v:
                continue
            self.lazy[i<<1] += v; self.lazy[i<<1|1] += v
            self.dat[i<<1] += v; self.dat[i<<1|1] += v
            self.lazy[i] = 0

    def updatel_add(self,l, r, x):#区間にyを足す
        self.propagates(self.gindex(l, r))
        L = self.N0 + l
        R = self.N0 + r
        while L < R:
            if L & 1:
                self.lazy[L] += x
                self.dat[L] += x
                L += 1
            if R & 1:
                R -= 1
                self.lazy[R] += x
                self.dat[R] += x
            L >>= 1
            R >>= 1       
        for i in self.gindex(l, r):
            self.dat[i] = self.func(self.dat[i<<1],self.dat[i<<1|1])+self.lazy[i]

    def query(self,l, r):
        self.propagates(self.gindex(l, r))
        L = self.N0 + l
        R = self.N0 + r
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL,self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R],vR)
            L >>= 1
            R >>= 1
        return self.func(vL,vR)
    
    def __getitem__(self, x): return self.leafvalue(x)

N = int(input())
lsT = list(map(int,input().split()))
M = int(input())
ans = []
for i in range(N-1):
    lsT[i] -= i*3
LSG = LazySegTree(lsT,mode='max')
time = 3*(N-1)

for i in range(M):
    L,R,D = map(int,input().split())
    LSG.updatel_add(L-1, R, D)
    ans.append(time+LSG.query(0, N-1))
print(*ans,sep='\n')
0