結果

問題 No.59 鉄道の旅
ユーザー kohei2019kohei2019
提出日時 2021-05-22 10:44:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 194 ms / 5,000 ms
コード長 2,386 bytes
コンパイル時間 325 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 101,520 KB
最終ジャッジ日時 2024-04-18 17:32:17
合計ジャッジ時間 2,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
83,584 KB
testcase_01 AC 60 ms
83,456 KB
testcase_02 AC 60 ms
83,456 KB
testcase_03 AC 63 ms
84,224 KB
testcase_04 AC 194 ms
101,356 KB
testcase_05 AC 69 ms
87,168 KB
testcase_06 AC 70 ms
87,808 KB
testcase_07 AC 63 ms
84,480 KB
testcase_08 AC 120 ms
100,848 KB
testcase_09 AC 121 ms
100,352 KB
testcase_10 AC 124 ms
100,224 KB
testcase_11 AC 105 ms
100,160 KB
testcase_12 AC 149 ms
101,248 KB
testcase_13 AC 178 ms
101,388 KB
testcase_14 AC 180 ms
101,520 KB
testcase_15 AC 58 ms
83,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
sys.setrecursionlimit(10**7)

class segki():
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
        'sum': 0,
        'prd': 1,
        'gcd': 0,
        'lmc': 1,
        '^': 0,
        '&': (1 << 60) - 1,
        '|': 0,
    }

    FUNC = {
        'min': min,
        'max': max,
        'sum': (lambda x, y: x + y),
        'prd': (lambda x, y: x * y),
        'gcd': math.gcd,
        'lmc': (lambda x, y: (x * y) // math.gcd(x, y)),
        '^': (lambda x, y: x ^ y),
        '&': (lambda x, y: x & y),
        '|': (lambda x, y: x | y),
    }

    def __init__(self, N, ls, mode='min'):
        """
        葉の数N, 要素ls, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|)
        """
        self.default = self.DEFAULT[mode]
        self.func = self.FUNC[mode]
        self.N = N
        self.K = (N - 1).bit_length()
        self.N2 = 1 << self.K
        self.dat = [self.default] * (2**(self.K + 1))
        for i in range(self.N):  # 葉の構築
            self.dat[self.N2 + i] = ls[i]
        self.build()

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

    def leafvalue(self, x):  # リストのx番目の値
        return self.dat[x + self.N2]

    def update(self, x, y):  # index(x)をyに変更
        i = x + self.N2
        self.dat[i] = y
        while i > 0:  # 親の値を変更
            i >>= 1
            self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])
        return

    def query(self, L, R):  # [L,R)の区間取得
        L += self.N2
        R += self.N2
        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)

N,K = map(int,input().split())
lsW = [int(input()) for i in range(N)]
SG = segki(10**6+1,[0]*(10**6+1),mode='sum')

for i in range(N):
    if lsW[i] < 0:
        if SG.leafvalue(-lsW[i]) > 0:
            SG.update(-lsW[i],SG.leafvalue(-lsW[i])-1)
    else:
        if SG.query(lsW[i],10**6+1) < K:
             SG.update(lsW[i],SG.leafvalue(lsW[i])+1)
print(SG.dat[1])
0