結果

問題 No.1024 Children in a Row
ユーザー chocoruskchocorusk
提出日時 2020-04-08 12:50:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 634 ms / 2,000 ms
コード長 1,546 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 86,800 KB
実行使用メモリ 139,508 KB
最終ジャッジ日時 2023-10-12 07:41:01
合計ジャッジ時間 16,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,596 KB
testcase_01 AC 91 ms
71,760 KB
testcase_02 AC 91 ms
71,592 KB
testcase_03 AC 110 ms
76,484 KB
testcase_04 AC 107 ms
76,052 KB
testcase_05 AC 107 ms
76,248 KB
testcase_06 AC 108 ms
76,228 KB
testcase_07 AC 106 ms
76,468 KB
testcase_08 AC 107 ms
76,184 KB
testcase_09 AC 108 ms
76,124 KB
testcase_10 AC 589 ms
123,288 KB
testcase_11 AC 581 ms
121,136 KB
testcase_12 AC 575 ms
122,088 KB
testcase_13 AC 576 ms
122,988 KB
testcase_14 AC 582 ms
122,176 KB
testcase_15 AC 609 ms
123,828 KB
testcase_16 AC 545 ms
120,380 KB
testcase_17 AC 498 ms
128,588 KB
testcase_18 AC 449 ms
133,792 KB
testcase_19 AC 623 ms
130,616 KB
testcase_20 AC 625 ms
130,452 KB
testcase_21 AC 627 ms
130,864 KB
testcase_22 AC 634 ms
130,756 KB
testcase_23 AC 583 ms
127,728 KB
testcase_24 AC 595 ms
128,416 KB
testcase_25 AC 584 ms
127,624 KB
testcase_26 AC 518 ms
135,200 KB
testcase_27 AC 514 ms
139,508 KB
testcase_28 AC 471 ms
133,940 KB
testcase_29 AC 370 ms
133,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import bisect
n, m=map(int, input().split())
a=[0]*m
b=[0]*m
k=[0]*m
c=[i for i in range(n)]
cr=[0]*(n+m)
for i in range(n):
    cr[i]=i
g=[[] for i in range(n+m)]
pr=[0]*m
for i in range(m):
    a[i], b[i], k[i]=map(int, input().split())
    a[i]-=1
    b[i]-=1
    g[c[b[i]]].append(i+n)
    pr[i]=c[a[i]]
    c[a[i]]=i+n
    cr[i+n]=a[i]
st=[]
l=[0]*(n+m)
r=[0]*(n+m)
rev=[0]*(n+m)
t=0
for i in range(n):
    st.append(i)
    while len(st)!=0:
        x=st.pop()
        if len(st)==0:
            if i<n-1:
                r[x]=i+1
            else:
                r[x]=n+m
        else:
            r[x]=st[len(st)-1]
        l[x]=t
        rev[t]=x
        t+=1
        for y in g[x]:
            st.append(y)
for i in range(n+m):
    if r[i]<n+m:
        r[i]=l[r[i]]
cnt=[0]*(n+m+1)
cntr=[0]*(n+1)
for i in range(n+m):
    cnt[i+1]=cnt[i]
    if c[cr[rev[i]]]==rev[i]:
        cnt[i+1]+=1
        cntr[cnt[i+1]]=i
for i in range(m):
    l1=l[i+n]
    r1=r[i+n]
    l2=l[pr[i]]
    ans=0
    if l1<l2:
        if k[i]<=cnt[l1] or k[i]>cnt[l2]:
            ans=cr[rev[cntr[k[i]]]]
        elif k[i]<=cnt[l1]+cnt[l2]-cnt[r1]:
            ans=cr[rev[cntr[k[i]-cnt[l1]+cnt[r1]]]]
        else:
            ans=cr[rev[cntr[k[i]-cnt[l2]+cnt[r1]]]]
    else:
        if k[i]<=cnt[l2] or k[i]>cnt[r1]:
            ans=cr[rev[cntr[k[i]]]]
        elif k[i]<=cnt[l2]+cnt[r1]-cnt[l1]:
            ans=cr[rev[cntr[k[i]-cnt[l2]+cnt[l1]]]]
        else:
            ans=cr[rev[cntr[k[i]-cnt[r1]+cnt[l1]]]]
    print(ans+1)
0