結果

問題 No.1868 Teleporting Cyanmond
ユーザー brthyyjpbrthyyjp
提出日時 2022-03-12 13:35:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,607 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 94,564 KB
最終ジャッジ日時 2023-10-15 02:20:38
合計ジャッジ時間 6,150 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,196 KB
testcase_01 AC 70 ms
71,072 KB
testcase_02 AC 75 ms
71,336 KB
testcase_03 AC 236 ms
92,312 KB
testcase_04 AC 176 ms
86,360 KB
testcase_05 AC 129 ms
78,584 KB
testcase_06 AC 139 ms
78,400 KB
testcase_07 AC 160 ms
83,812 KB
testcase_08 AC 148 ms
80,524 KB
testcase_09 AC 164 ms
82,972 KB
testcase_10 AC 192 ms
90,340 KB
testcase_11 AC 110 ms
77,920 KB
testcase_12 AC 155 ms
79,960 KB
testcase_13 AC 158 ms
83,984 KB
testcase_14 AC 192 ms
92,504 KB
testcase_15 AC 176 ms
86,420 KB
testcase_16 AC 141 ms
78,764 KB
testcase_17 AC 146 ms
79,112 KB
testcase_18 AC 194 ms
93,928 KB
testcase_19 AC 187 ms
94,240 KB
testcase_20 AC 187 ms
94,084 KB
testcase_21 AC 183 ms
94,352 KB
testcase_22 AC 186 ms
94,004 KB
testcase_23 AC 175 ms
94,256 KB
testcase_24 AC 173 ms
94,564 KB
testcase_25 AC 176 ms
94,116 KB
testcase_26 AC 168 ms
94,344 KB
testcase_27 AC 167 ms
93,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    def __init__(self, init_val, ide_ele, segfunc):
        self.n = len(init_val)
        self.num = 1<<(self.n-1).bit_length()
        self.ide_ele = ide_ele
        self.segfunc = segfunc
        self.seg = [ide_ele]*2*self.num
        # set_val
        for i in range(self.n):
            self.seg[i+self.num] = init_val[i]
        # built
        for i in range(self.num-1, 0, -1):
            self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1])

    def update(self, k, x):
        k += self.num
        self.seg[k] = x
        while k:
            k = k >> 1
            self.seg[k] = self.segfunc(self.seg[2*k], self.seg[2*k+1])

    def query(self, l, r):
        if r <= l:
            return self.ide_ele
        l += self.num
        r += self.num
        lres = self.ide_ele
        rres = self.ide_ele
        while l < r:
            if r & 1:
                r -= 1
                rres = self.segfunc(self.seg[r], rres)
            if l & 1:
                lres = self.segfunc(lres, self.seg[l])
                l += 1
            l = l >> 1
            r = r >> 1
        res = self.segfunc(lres, rres)
        return res

    def __str__(self): # for debug
        arr = [self.query(i,i+1) for i in range(self.n)]
        return str(arr)

n = int(input())
R = list(map(int, input().split()))
R = [r-1 for r in R]
INF = 10**18
seg = SegTree([INF]*n, INF, min)
seg.update(n-1, 0)
for i in reversed(range(n-1)):
    r = R[i]
    x = seg.query(i+1, r+1)
    if x != INF:
        seg.update(i, x+1)
ans = seg.query(0, 1)
if ans < INF:
    print(ans)
else:
    print(-1)
0