結果

問題 No.1698 Face to Face
ユーザー eSeFeSeF
提出日時 2021-09-18 16:59:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,882 ms / 5,000 ms
コード長 4,344 bytes
コンパイル時間 500 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 223,036 KB
最終ジャッジ日時 2023-09-26 15:57:59
合計ジャッジ時間 10,384 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
72,184 KB
testcase_01 AC 92 ms
72,144 KB
testcase_02 AC 89 ms
71,936 KB
testcase_03 AC 776 ms
188,588 KB
testcase_04 AC 1,089 ms
201,852 KB
testcase_05 AC 3,255 ms
221,848 KB
testcase_06 AC 3,193 ms
221,448 KB
testcase_07 AC 2,981 ms
223,036 KB
testcase_08 AC 2,840 ms
219,836 KB
testcase_09 AC 2,872 ms
221,656 KB
testcase_10 AC 1,078 ms
205,268 KB
testcase_11 AC 3,270 ms
218,640 KB
testcase_12 AC 3,635 ms
218,100 KB
testcase_13 AC 95 ms
71,916 KB
testcase_14 AC 93 ms
72,204 KB
testcase_15 AC 3,573 ms
213,580 KB
testcase_16 AC 3,815 ms
217,800 KB
testcase_17 AC 3,700 ms
219,932 KB
testcase_18 AC 3,220 ms
198,224 KB
testcase_19 AC 3,318 ms
198,704 KB
testcase_20 AC 3,322 ms
192,424 KB
testcase_21 AC 2,063 ms
145,136 KB
testcase_22 AC 1,881 ms
134,504 KB
testcase_23 AC 881 ms
98,304 KB
testcase_24 AC 2,312 ms
152,796 KB
testcase_25 AC 3,882 ms
212,172 KB
testcase_26 AC 671 ms
92,112 KB
testcase_27 AC 1,461 ms
118,432 KB
testcase_28 AC 1,522 ms
123,336 KB
testcase_29 AC 1,327 ms
116,284 KB
testcase_30 AC 2,397 ms
161,620 KB
testcase_31 AC 2,306 ms
149,916 KB
testcase_32 AC 2,700 ms
175,824 KB
testcase_33 AC 2,779 ms
179,676 KB
testcase_34 AC 3,545 ms
208,564 KB
testcase_35 AC 91 ms
72,152 KB
testcase_36 AC 92 ms
72,104 KB
testcase_37 AC 90 ms
72,212 KB
testcase_38 AC 195 ms
72,272 KB
testcase_39 AC 92 ms
72,308 KB
testcase_40 AC 93 ms
72,204 KB
testcase_41 AC 91 ms
71,944 KB
testcase_42 AC 93 ms
72,312 KB
testcase_43 AC 92 ms
72,340 KB
testcase_44 AC 91 ms
72,096 KB
testcase_45 AC 2,191 ms
211,288 KB
testcase_46 AC 2,396 ms
215,864 KB
testcase_47 AC 2,347 ms
214,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind_AB:
    def __init__(self, N):
        self.p = [-1] * N
        self.L = [i for i in range(N)]
        self.R = [i for i in range(N)]
    def root(self, x):
        while self.p[x] >= 0:
            x = self.p[x]
        return x
 
    def same(self, x, y):
        return self.root(x) == self.root(y)
 
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        p = self.p
        if p[x] > p[y]:
            p[y] += p[x]
            p[x] = y
            if self.L[y] > self.L[x]:
                self.L[y] = self.L[x]
            if self.R[y] < self.R[x]:
                self.R[y] = self.R[x]
        else:
            p[x] += p[y]
            p[y] = x
            if self.L[x] > self.L[y]:
                self.L[x] = self.L[y]
            if self.R[x] < self.R[y]:
                self.R[x] = self.R[y]
 
    def size(self, x):
        return -self.p[self.root(x)]
    def left(self, v):
        return self.L[self.root(v)]
    def right(self,v):
        return self.R[self.root(v)]

class UnionFind_IDX:
    def __init__(self, N):
        self.p = [-1] * N
        self.num_pair = [0] * N
        self.score = 0
 
    def root(self, x):
        while self.p[x] >= 0:
            x = self.p[x]
        return x
 
    def same(self, x, y):
        return self.root(x) == self.root(y)
 
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        self.score -= self.sub_score(x) + self.sub_score(y)
        p = self.p
        if p[x] > p[y]:
            p[y] += p[x]
            p[x] = y
            self.num_pair[y] += self.num_pair[x]
            self.score += self.sub_score(y)
        else:
            p[x] += p[y]
            p[y] = x
            self.num_pair[x] += self.num_pair[y]
            self.score += self.sub_score(x)

    def add_pair(self,v):
        rv = self.root(v)
        self.score -= self.sub_score(rv)
        self.num_pair[rv]+=1
        self.score += self.sub_score(rv)
    def size(self, x):
        return -self.p[self.root(x)]
    def sub_score(self,v):
        return min(-self.p[self.root(v)],self.num_pair[self.root(v)])

from sys import stdin
from collections import deque

def main():
    from builtins import input, int, map
    readline = stdin.readline
    n = int(input())
    a = list(map(int,readline().split()))
    b = list(map(int,readline().split()))
    z = list(map(int,readline().split()))

    ida = [-1]*n
    idb = [-1]*n
    for i in range(n):
        a[i]-=1
        b[i]-=1
        z[i]-=1
        ida[a[i]]=i
        idb[b[i]]=i

    pos_left = [-1]*n
    l = [-1]*n
    r = [n]*n
    rn = range(n)
    mid = [[] for i in rn]
    for _ in [0]*(17):
        for i in rn:
            if r[i]-l[i]>1:
                mid[(l[i]+r[i])//2].append(i)

        ufa = UnionFind_AB(n)
        ufb = UnionFind_AB(n)
        for k in rn:
            ia = ida[k]
            ib = idb[k]
            if 0<ia and a[ia-1]<=k:
                ufa.unite(ia-1,ia)
            if ia+1<n and a[ia+1]<=k:
                ufa.unite(ia,ia+1)
            if 0<ib and b[ib-1]<=k:
                ufb.unite(ib-1,ib)
            if ib+1<n and b[ib+1]<=k:
                ufb.unite(ib,ib+1)
        
            for j in mid[k]:
                la, ra = ufa.left(ida[j]),ufa.right(ida[j])
                lb, rb = ufb.left(idb[z[j]]),ufb.right(idb[z[j]])

                if ra < lb or rb < la:
                    l[j] = k
                else:
                    r[j] = k
                    pos_left[j] = max(la,lb)
            mid[k].clear()

    for i in rn:
        mid[r[i]].append(i)

    less_k = [0]*n
    ufi = UnionFind_IDX(n)
    ans = []
    for k in rn:
        ia, ib = ida[k],idb[k]
        less_k[ia]+=1
        less_k[ib]+=1
        if less_k[ia]==2:
            if 0<ia and less_k[ia-1]==2:
                ufi.unite(ia-1,ia)
            if ia+1<n and less_k[ia+1]==2:
                ufi.unite(ia,ia+1)
        if less_k[ib]==2:
            if 0<ib and less_k[ib-1]==2:
                ufi.unite(ib-1,ib)
            if ib+1<n and less_k[ib+1]==2:
                ufi.unite(ib,ib+1)
        for j in mid[k]:
            ufi.add_pair(pos_left[j])
        ans.append(ufi.score)

    print("\n".join(map(str,ans)))

if __name__=='__main__':
    main()
0