結果

問題 No.2643 Many Range Sums Problems
ユーザー KoiKoi
提出日時 2024-02-13 11:28:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,749 ms / 8,000 ms
コード長 1,456 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 83,060 KB
最終ジャッジ日時 2024-02-13 15:30:01
合計ジャッジ時間 26,391 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,600 KB
testcase_01 AC 39 ms
55,600 KB
testcase_02 AC 39 ms
55,600 KB
testcase_03 AC 1,239 ms
80,580 KB
testcase_04 AC 1,113 ms
80,344 KB
testcase_05 AC 941 ms
79,996 KB
testcase_06 AC 1,462 ms
81,308 KB
testcase_07 AC 883 ms
83,060 KB
testcase_08 AC 1,410 ms
81,056 KB
testcase_09 AC 1,445 ms
81,424 KB
testcase_10 AC 795 ms
82,136 KB
testcase_11 AC 404 ms
78,600 KB
testcase_12 AC 887 ms
79,440 KB
testcase_13 AC 1,749 ms
81,284 KB
testcase_14 AC 1,236 ms
80,848 KB
testcase_15 AC 1,323 ms
80,844 KB
testcase_16 AC 1,092 ms
80,316 KB
testcase_17 AC 1,177 ms
80,684 KB
testcase_18 AC 715 ms
79,188 KB
testcase_19 AC 176 ms
78,196 KB
testcase_20 AC 360 ms
78,528 KB
testcase_21 AC 142 ms
77,316 KB
testcase_22 AC 298 ms
78,660 KB
testcase_23 AC 359 ms
78,656 KB
testcase_24 AC 672 ms
79,900 KB
testcase_25 AC 532 ms
79,400 KB
testcase_26 AC 184 ms
78,436 KB
testcase_27 AC 185 ms
78,780 KB
testcase_28 AC 38 ms
55,600 KB
testcase_29 AC 39 ms
55,600 KB
testcase_30 AC 556 ms
79,240 KB
testcase_31 AC 1,516 ms
81,280 KB
testcase_32 AC 865 ms
79,360 KB
testcase_33 AC 851 ms
79,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#faster ver
from collections import deque
N,K=map(int,input().split())
R=[]
G1=[[] for _ in range(N+1)]
for i in range(N):
    r,x=map(int,input().split())
    R.append(r)
    G1[i].append([r,x])
    G1[r].append([i,-x])

answers=[]
Group_num=[-1]*(N+1)
que=deque([0])
seen=[False]*(N+1)
seen[0]=True
B=[0]*(N+1)

G2=[[] for _ in range(N+1)]
while len(que):
    p=que.popleft()
    for (q,x) in G1[p]:
        if(not seen[q]):
            que.append(q)
            seen[q]=True
            B[q]=B[p]+x
            G2[p].append(q)
for i in range(N):
    que=deque([0])
    Group_num[0]=1
    diff_L=-10**19
    diff_R=10**19
    ans="Yes"
    while len(que):
        p=que.popleft()
        for q in G2[p]:
            if((p==i and q==R[i]) or (q==i and p==R[i])):
                Group_num[q]=2
            else:
                Group_num[q]=Group_num[p]
            que.append(q)
            seen[q]=True
    for j in range(N):
        if(Group_num[j]==Group_num[j+1]):
            if(B[j+1]-B[j]<0 or B[j+1]-B[j]>K):
                ans="No"
        else:
            if(Group_num[j]==1 and Group_num[j+1]==2):
                Left=B[j+1]-B[j]-K
                Right=B[j+1]-B[j]
            else:
                Left=B[j]-B[j+1]
                Right=K+B[j]-B[j+1]
            diff_L=max(diff_L,Left)
            diff_R=min(diff_R,Right)
            if(diff_L>diff_R):
                ans="No"
    answers.append(ans)
for ans in answers:
    print(ans)
0