結果
問題 | No.1914 Directed by Two Sequences |
ユーザー | chineristAC |
提出日時 | 2021-10-31 18:00:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,722 ms / 3,000 ms |
コード長 | 5,794 bytes |
コンパイル時間 | 1,032 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 156,920 KB |
最終ジャッジ日時 | 2024-12-16 01:43:52 |
合計ジャッジ時間 | 59,428 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
class SegmentTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num self.size = n for i in range(n): self.tree[self.num + i] = init_val[i] for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: k >>= 1 self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1]) def query(self, l, r): if r==self.size: r = self.num res = self.ide_ele l += self.num r += self.num right = [] while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: right.append(self.tree[r-1]) l >>= 1 r >>= 1 for e in right[::-1]: res = self.segfunc(res,e) return res def solve(N,A,B,M,E): A = [a-1 for a in A] B = [b-1 for b in B] res = set() edge = [[] for v in range(N)] for u,v in E: edge[u-1].append(v-1) edge[v-1].append(u-1) group = [-1 for v in range(N)] for v in range(N): mex = [False for i in range(len(edge[v])+1)] for nv in edge[v]: if nv < v and group[nv] < len(mex): mex[group[nv]] = True for i in range(len(mex)): if not mex[i]: group[v] = i break n = max(group) + 1 clique = [[] for g in range(n)] for v in range(N): clique[group[v]].append(v) def direct(i,j): if i < j: return A[i] < B[j] else: return B[i] < A[j] def hamilton_path(V): n = len(V) if n <= 1: return V A = hamilton_path(V[:n//2]) B = hamilton_path(V[n//2:]) res = [] bi = 0 for ai in range(len(A)): while bi<len(B) and direct(B[bi],A[ai]): res.append(B[bi]) bi += 1 res.append(A[ai]) res += B[bi:] return res idx = [-1 for v in range(N)] for g in range(n): clique[g] = hamilton_path(clique[g]) for i in range(len(clique[g])): idx[clique[g][i]] = i for u,v in zip(clique[g],clique[g][1:]): res.add((u,v)) memo_to = [N for v in range(N)] memo_from = [-1 for v in range(N)] ci = [i for i in range(n)] ci.sort(lambda g:len(clique[g])) idx_on_ci = [-1 for i in range(n)] for i in range(n): idx_on_ci[ci[i]] = i ban = [[] for v in range(N)] Vs = [] seg_to_large = SegmentTree([N]*2*N,min,N) seg_to_small = SegmentTree([N]*2*N,min,N) seg_from_large = SegmentTree([-1]*2*N,max,-1) seg_from_small = SegmentTree([-1]*2*N,max,-1) for i in range(n): target = ci[i] Vs += clique[target] Vs.sort() for nv in clique[target]: for v in edge[nv]: if idx_on_ci[group[v]] <= i: ban[v].append(nv) for v in Vs[::-1]: for nv in ban[v]: if v < nv: seg_to_large.update(B[nv],N) memo_to[v] = min(memo_to[v],seg_to_large.query(A[v],2*N)) for nv in ban[v]: if v < nv: seg_to_large.update(B[nv],idx[nv]) if group[v]==target: seg_to_large.update(B[v],idx[v]) for v in Vs: for nv in ban[v]: if nv < v: seg_to_small.update(A[nv],N) memo_to[v] = min(memo_to[v],seg_to_small.query(B[v],2*N)) for nv in ban[v]: if nv < v: seg_to_small.update(A[nv],idx[nv]) if group[v]==target: seg_to_small.update(A[v],idx[v]) for v in Vs[::-1]: for nv in ban[v]: if v < nv: seg_from_large.update(B[nv],-1) memo_from[v] = max(memo_from[v],seg_from_large.query(0,A[v])) for nv in ban[v]: if v < nv: seg_from_large.update(B[nv],idx[nv]) if group[v]==target: seg_from_large.update(B[v],idx[v]) for v in Vs: for nv in ban[v]: if nv < v: seg_from_small.update(A[nv],-1) memo_from[v] = max(memo_from[v],seg_from_small.query(0,B[v])) for nv in ban[v]: if nv < v: seg_from_small.update(A[nv],idx[nv]) if group[v]==target: seg_from_small.update(A[v],idx[v]) for v in Vs: if memo_to[v]!=N: nv = clique[target][memo_to[v]] res.add((v,nv)) memo_to[v] = N if memo_from[v]!=-1: pv = clique[target][memo_from[v]] res.add((pv,v)) memo_from[v] = -1 ban[v] = [] if group[v]==target: seg_to_large.update(B[v],N) seg_to_small.update(A[v],N) seg_from_large.update(B[v],-1) seg_from_small.update(A[v],-1) print(len(res)) for u,v in res: print(u+1,v+1) import sys input = lambda :sys.stdin.buffer.readline() mi = lambda :map(int,input().split()) li = lambda :list(mi()) N,M = mi() A = li() B = li() E = [tuple(mi()) for i in range(M)] solve(N,A,B,M,E)