結果

問題 No.1995 CHIKA Road
ユーザー ygd.ygd.
提出日時 2022-07-01 22:10:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 2,734 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 179,428 KB
最終ジャッジ日時 2023-08-17 09:42:10
合計ジャッジ時間 12,569 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,540 KB
testcase_01 AC 73 ms
71,376 KB
testcase_02 AC 73 ms
71,252 KB
testcase_03 AC 76 ms
71,484 KB
testcase_04 AC 72 ms
71,288 KB
testcase_05 AC 79 ms
75,444 KB
testcase_06 AC 156 ms
81,680 KB
testcase_07 AC 224 ms
96,444 KB
testcase_08 AC 85 ms
76,020 KB
testcase_09 AC 79 ms
75,724 KB
testcase_10 AC 310 ms
85,456 KB
testcase_11 AC 537 ms
179,428 KB
testcase_12 AC 334 ms
128,972 KB
testcase_13 AC 252 ms
108,612 KB
testcase_14 AC 261 ms
108,900 KB
testcase_15 AC 479 ms
160,260 KB
testcase_16 AC 169 ms
85,772 KB
testcase_17 AC 344 ms
117,332 KB
testcase_18 AC 461 ms
126,304 KB
testcase_19 AC 458 ms
126,932 KB
testcase_20 AC 312 ms
114,768 KB
testcase_21 AC 295 ms
109,532 KB
testcase_22 AC 450 ms
131,004 KB
testcase_23 AC 232 ms
95,884 KB
testcase_24 AC 416 ms
128,504 KB
testcase_25 AC 184 ms
87,644 KB
testcase_26 AC 241 ms
97,896 KB
testcase_27 AC 397 ms
127,476 KB
testcase_28 AC 293 ms
109,348 KB
testcase_29 AC 455 ms
132,388 KB
testcase_30 AC 178 ms
87,380 KB
testcase_31 AC 153 ms
80,640 KB
testcase_32 AC 196 ms
89,976 KB
testcase_33 AC 388 ms
127,076 KB
testcase_34 AC 157 ms
82,100 KB
testcase_35 AC 234 ms
98,116 KB
testcase_36 AC 255 ms
100,736 KB
testcase_37 AC 453 ms
131,804 KB
testcase_38 AC 345 ms
116,280 KB
testcase_39 AC 154 ms
81,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

#和のセグ木にするときは以下の関数を定義する必要あり。
def segfunc(x,y):
  return x+y

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)

def compress(S):
    S = set(S); S = list(S)
    S.sort()
    dic_comp_ori = {}
    dic_ori_comp = {}
    for i,a in enumerate(S):
        dic_ori_comp[a] = i
        dic_comp_ori[i] = a
    return dic_comp_ori, dic_ori_comp 

def main():
    N,M = map(int,input().split()); INF = pow(10,18)
    roads = []
    S = set([])
    for _ in range(M):
        A,B = map(int,input().split())
        S.add(A)
        S.add(B)
        roads.append((A,B))

    dic_comp_ori, dic_ori_comp = compress(S)
    for i,road in enumerate(roads):
        a = dic_ori_comp[road[0]]
        b = dic_ori_comp[road[1]]
        roads[i] = (a,b)
    oriN = N
    N = len(dic_ori_comp)
    roads.sort(key=lambda x: x[1])
    #print(roads)
    
    Tree = SegmentTree([0]*N,max,0)
    for s,t in roads:
        v = Tree.sum(0,s+1) + 1 #[0,s]
        Tree.update(t, max(Tree[t],v))
    
    ans = (oriN-1)*2 - Tree.sum(0,N)
    print(ans)

if __name__ == '__main__':
    main()
0