結果
問題 |
No.3167 [Cherry 7th Tune C] Cut in Queue
|
ユーザー |
![]() |
提出日時 | 2025-06-16 02:27:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 910 ms / 2,500 ms |
コード長 | 2,453 bytes |
コンパイル時間 | 757 ms |
コンパイル使用メモリ | 81,932 KB |
実行使用メモリ | 205,548 KB |
最終ジャッジ日時 | 2025-06-16 02:28:26 |
合計ジャッジ時間 | 32,291 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 43 |
ソースコード
import sys input = sys.stdin.readline class Bit_indexed_tree(): def __init__(self, LEN): self.BIT = [0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。 self.LEN = LEN def update(self,v,w): # index vにwを加える while v<=self.LEN: self.BIT[v]+=w v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4 def getvalue(self,v): # [1,v]の区間の和を求める ANS=0 while v!=0: ANS+=self.BIT[v] v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ return ANS def bisect_on_BIT(self,x): # [1,ind]の和がはじめてx以上になるindexを探す if x<=0: return 0 ANS=0 h=1<<((self.LEN).bit_length()-1) # LEN以下の最小の2ベキ while h>0: if ANS+h<=self.LEN and self.BIT[ANS+h]<x: x-=self.BIT[ANS+h] ANS+=h h//=2 return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意 N=int(input()) A0=list(map(int,input().split())) A=A0[:] Q=int(input()) Query=[list(map(int,input().split())) for i in range(Q)] FR=[[] for i in range(N+Q+2)] Parent=[-1]*(N+Q+2) qt=N+1 for L in Query: if L[0]==1: y=L[1] if y==0: A.append(qt) else: FR[y].append(qt) Parent[qt]=y qt+=1 INDLIST=[len(FR[i])-1 for i in range(N+Q+2)] USE=[0]*(N+Q+2) B=[] for a in A: X=[] now=a while True: #print(now,end=" ") if USE[now]==0: X.append(now) USE[now]=1 if INDLIST[now]>=0 and FR[now][INDLIST[now]]: INDLIST[now]-=1 now=FR[now][INDLIST[now]+1] else: now=Parent[now] if now==-1: break B+=X[::-1] INV=[-1]*(len(B)+1) for i in range(len(B)): INV[B[i]]=i+1 #print(B) BIT=Bit_indexed_tree(len(B)+1) for a in A0: ind=INV[a] BIT.update(ind,1) qt=N+1 for L in Query: if L[0]==1: ind=INV[qt] BIT.update(ind,1) qt+=1 elif L[0]==2: ind=BIT.bisect_on_BIT(1) BIT.update(ind,-1) else: x=L[1] ind=BIT.bisect_on_BIT(x) print(B[ind-1])