結果

問題 No.1914 Directed by Two Sequences
ユーザー chineristACchineristAC
提出日時 2021-10-31 19:02:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,794 ms / 3,000 ms
コード長 5,915 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 193,436 KB
最終ジャッジ日時 2024-05-09 12:21:42
合計ジャッジ時間 63,385 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
55,296 KB
testcase_01 AC 44 ms
54,912 KB
testcase_02 AC 602 ms
97,972 KB
testcase_03 AC 550 ms
98,400 KB
testcase_04 AC 571 ms
98,916 KB
testcase_05 AC 554 ms
97,864 KB
testcase_06 AC 566 ms
97,856 KB
testcase_07 AC 669 ms
95,200 KB
testcase_08 AC 658 ms
96,680 KB
testcase_09 AC 672 ms
96,920 KB
testcase_10 AC 1,558 ms
175,244 KB
testcase_11 AC 1,492 ms
173,512 KB
testcase_12 AC 1,502 ms
168,872 KB
testcase_13 AC 1,475 ms
163,372 KB
testcase_14 AC 1,577 ms
165,360 KB
testcase_15 AC 1,539 ms
162,584 KB
testcase_16 AC 1,584 ms
180,356 KB
testcase_17 AC 1,565 ms
180,184 KB
testcase_18 AC 1,554 ms
179,104 KB
testcase_19 AC 960 ms
134,584 KB
testcase_20 AC 998 ms
134,324 KB
testcase_21 AC 1,029 ms
141,076 KB
testcase_22 AC 1,584 ms
176,524 KB
testcase_23 AC 1,602 ms
173,148 KB
testcase_24 AC 1,777 ms
172,452 KB
testcase_25 AC 1,510 ms
169,632 KB
testcase_26 AC 1,423 ms
165,332 KB
testcase_27 AC 1,724 ms
168,928 KB
testcase_28 AC 1,794 ms
167,284 KB
testcase_29 AC 1,681 ms
167,936 KB
testcase_30 AC 1,604 ms
163,516 KB
testcase_31 AC 1,595 ms
172,360 KB
testcase_32 AC 1,704 ms
163,352 KB
testcase_33 AC 636 ms
113,312 KB
testcase_34 AC 1,686 ms
193,436 KB
testcase_35 AC 897 ms
144,912 KB
testcase_36 AC 1,320 ms
166,660 KB
testcase_37 AC 1,351 ms
167,640 KB
testcase_38 AC 1,462 ms
179,988 KB
testcase_39 AC 1,628 ms
187,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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):
    from collections import deque
    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 merge(a,b):
        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):
        deq = deque([[v] for v in clique[g]])
        while 1 < len(deq):
            a = deq.popleft()
            b = deq.popleft()
            deq.append(merge(a,b))
        clique[g] = deq[0]

        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 = []

    

    for i in range(n):
        target = ci[i]
        Vs += clique[target]
        Vs.sort()
        val = sorted([A[v] for v in Vs]+[B[v] for v in Vs])
        comp = {e:i for i,e in enumerate(val)}
        for nv in clique[target]:
            for v in edge[nv]:
                if idx_on_ci[group[v]] <= i:
                    ban[v].append(nv)
        
        seg_to_large = SegmentTree([N]*len(comp),min,N)
        seg_to_small = SegmentTree([N]*len(comp),min,N)
        seg_from_large = SegmentTree([-1]*len(comp),max,-1)
        seg_from_small = SegmentTree([-1]*len(comp),max,-1)
        k = len(comp)

        for v in Vs[::-1]:
            for nv in ban[v]:
                if v < nv:
                    seg_to_large.update(comp[B[nv]],N)
            memo_to[v] = min(memo_to[v],seg_to_large.query(comp[A[v]],k))
            for nv in ban[v]:
                if v < nv:
                    seg_to_large.update(comp[B[nv]],idx[nv])
            if group[v]==target:
                seg_to_large.update(comp[B[v]],idx[v])
        
        for v in Vs:
            for nv in ban[v]:
                if nv < v:
                    seg_to_small.update(comp[A[nv]],N)
            memo_to[v] = min(memo_to[v],seg_to_small.query(comp[B[v]],k))
            for nv in ban[v]:
                if nv < v:
                    seg_to_small.update(comp[A[nv]],idx[nv])
            if group[v]==target:
                seg_to_small.update(comp[A[v]],idx[v])
        
        for v in Vs[::-1]:
            for nv in ban[v]:
                if v < nv:
                    seg_from_large.update(comp[B[nv]],-1)
            memo_from[v] = max(memo_from[v],seg_from_large.query(0,comp[A[v]]))
            for nv in ban[v]:
                if v < nv:
                    seg_from_large.update(comp[B[nv]],idx[nv])
            if group[v]==target:
                seg_from_large.update(comp[B[v]],idx[v])
        
        for v in Vs:
            for nv in ban[v]:
                if nv < v:
                    seg_from_small.update(comp[A[nv]],-1)
            memo_from[v] = max(memo_from[v],seg_from_small.query(0,comp[B[v]]))
            for nv in ban[v]:
                if nv < v:
                    seg_from_small.update(comp[A[nv]],idx[nv])
            if group[v]==target:
                seg_from_small.update(comp[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] = []
            
    
    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)
0