Submission #8353977


Source Code Expand

Copy
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N,M = map(int,readline().split())
m = map(int,read().split())
LRC = zip(m,m,m)
R_to_L = [[] for _ in range(N+1)]
R_to_C = [[] for _ in range(N+1)]
for l,r,c in LRC:
R_to_L[r].append(l)
R_to_C[r].append(c)
INF = 10**18
class MinSegTree():
def __init__(self,N):
self.Nelem = N
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,M = map(int,readline().split())
m = map(int,read().split())
LRC = zip(m,m,m)

R_to_L = [[] for _ in range(N+1)]
R_to_C = [[] for _ in range(N+1)]

for l,r,c in LRC:
    R_to_L[r].append(l)
    R_to_C[r].append(c)

INF = 10**18

class MinSegTree():
    def __init__(self,N):
        self.Nelem = N
        self.size = 1<<(N.bit_length()) # 葉の要素数
        
    def build(self,raw_data):
        # raw_data は 0-indexed
        INF = 10**18
        self.data = [INF] * (2*self.size)
        for i,x in enumerate(raw_data):
            self.data[self.size+i] = x
        for i in range(self.size-1,0,-1):
            x = self.data[i+i]; y = self.data[i+i+1]
            self.data[i] = x if x<y else y
    
    def update(self,i,x):
        i += self.size
        self.data[i] = x
        i >>= 1
        while i:
            x = self.data[i+i]; y = self.data[i+i+1]
            self.data[i] = x if x<y else y
            i >>= 1
    
    def get_value(self,L,R):
        # [L,R] に対する値を返す
        L += self.size
        R += self.size + 1
        # [L,R) に変更
        x = 10**18
        while L < R:
            if L&1:
                y = self.data[L]
                if x > y: x = y
                L += 1
                
            if R&1:
                R -= 1
                y = self.data[R]
                if x > y: x = y
            L >>= 1; R >>= 1
        return x

dp = MinSegTree(N+1)
dp.build([INF]  * (N+1))

dp.update(1,0)

for r in range(2,N+1):
    x = INF
    for l,c in zip(R_to_L[r], R_to_C[r]):
        y = dp.get_value(l,r-1) + c
        if x>y:
            x=y
    dp.update(r,x)
    if r == N:
        answer = x

if answer == INF:
    answer = -1
print(answer)

Submission Info

Submission Time
Task D - Shortest Path on a Line
User maspy
Language Python (3.4.3)
Score 600
Code Size 1914 Byte
Status AC
Exec Time 1637 ms
Memory 46344 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 40
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt
All sample01.txt, sample02.txt, sample03.txt, in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, sample01.txt, sample02.txt, sample03.txt
Case Name Status Exec Time Memory
in01.txt AC 24 ms 3192 KB
in02.txt AC 24 ms 3192 KB
in03.txt AC 25 ms 3316 KB
in04.txt AC 24 ms 3316 KB
in05.txt AC 25 ms 3316 KB
in06.txt AC 25 ms 3316 KB
in07.txt AC 1162 ms 35912 KB
in08.txt AC 1102 ms 34832 KB
in09.txt AC 1485 ms 40640 KB
in10.txt AC 1478 ms 40712 KB
in11.txt AC 969 ms 31368 KB
in12.txt AC 1087 ms 34416 KB
in13.txt AC 1049 ms 33640 KB
in14.txt AC 1147 ms 35736 KB
in15.txt AC 1498 ms 41472 KB
in16.txt AC 1530 ms 42104 KB
in17.txt AC 1480 ms 41552 KB
in18.txt AC 1637 ms 42208 KB
in19.txt AC 818 ms 31244 KB
in20.txt AC 842 ms 31404 KB
in21.txt AC 895 ms 32744 KB
in22.txt AC 916 ms 31836 KB
in23.txt AC 757 ms 35976 KB
in24.txt AC 756 ms 36000 KB
in25.txt AC 696 ms 35344 KB
in26.txt AC 834 ms 32288 KB
in27.txt AC 1051 ms 42832 KB
in28.txt AC 941 ms 36536 KB
in29.txt AC 856 ms 33324 KB
in30.txt AC 878 ms 35856 KB
in31.txt AC 1029 ms 46344 KB
in32.txt AC 1084 ms 44816 KB
in33.txt AC 756 ms 31548 KB
in34.txt AC 18 ms 3192 KB
sample01.txt AC 18 ms 3192 KB
sample02.txt AC 18 ms 3192 KB
sample03.txt AC 18 ms 3192 KB


2025-04-12 (Sat)
10:07:02 +00:00