結果

問題 No.1024 Children in a Row
ユーザー chocoruskchocorusk
提出日時 2020-04-08 12:45:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 884 ms / 2,000 ms
コード長 1,632 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 137,792 KB
最終ジャッジ日時 2023-10-12 07:42:25
合計ジャッジ時間 20,728 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
71,744 KB
testcase_01 AC 100 ms
71,352 KB
testcase_02 AC 100 ms
71,740 KB
testcase_03 AC 125 ms
76,780 KB
testcase_04 AC 120 ms
76,188 KB
testcase_05 AC 124 ms
76,788 KB
testcase_06 AC 119 ms
76,768 KB
testcase_07 AC 119 ms
76,476 KB
testcase_08 AC 119 ms
76,660 KB
testcase_09 AC 123 ms
76,540 KB
testcase_10 AC 829 ms
123,480 KB
testcase_11 AC 808 ms
121,148 KB
testcase_12 AC 805 ms
122,192 KB
testcase_13 AC 798 ms
122,964 KB
testcase_14 AC 818 ms
122,916 KB
testcase_15 AC 857 ms
124,688 KB
testcase_16 AC 809 ms
120,948 KB
testcase_17 AC 692 ms
127,896 KB
testcase_18 AC 666 ms
132,808 KB
testcase_19 AC 872 ms
130,000 KB
testcase_20 AC 875 ms
130,812 KB
testcase_21 AC 884 ms
130,428 KB
testcase_22 AC 881 ms
131,072 KB
testcase_23 AC 829 ms
128,296 KB
testcase_24 AC 835 ms
128,040 KB
testcase_25 AC 816 ms
127,444 KB
testcase_26 AC 705 ms
134,648 KB
testcase_27 AC 673 ms
137,792 KB
testcase_28 AC 733 ms
133,200 KB
testcase_29 AC 437 ms
132,424 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)
for i in range(n+m):
    cnt[i+1]=cnt[i]
    if c[cr[rev[i]]]==rev[i]:
        cnt[i+1]+=1
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[bisect.bisect_left(cnt, k[i])-1]]
        elif k[i]<=cnt[l1]+cnt[l2]-cnt[r1]:
            ans=cr[rev[bisect.bisect_left(cnt, k[i]-cnt[l1]+cnt[r1])-1]]
        else:
            ans=cr[rev[bisect.bisect_left(cnt, k[i]-cnt[l2]+cnt[r1])-1]]
    else:
        if k[i]<=cnt[l2] or k[i]>cnt[r1]:
            ans=cr[rev[bisect.bisect_left(cnt, k[i])-1]]
        elif k[i]<=cnt[l2]+cnt[r1]-cnt[l1]:
            ans=cr[rev[bisect.bisect_left(cnt, k[i]-cnt[l2]+cnt[l1])-1]]
        else:
            ans=cr[rev[bisect.bisect_left(cnt, k[i]-cnt[r1]+cnt[l1])-1]]
    print(ans+1)
0