結果

問題 No.1698 Face to Face
ユーザー eSeFeSeF
提出日時 2021-09-18 16:32:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,120 ms / 5,000 ms
コード長 4,390 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 143,752 KB
最終ジャッジ日時 2024-07-19 10:12:40
合計ジャッジ時間 88,585 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
69,100 KB
testcase_01 AC 66 ms
69,864 KB
testcase_02 AC 69 ms
69,028 KB
testcase_03 AC 745 ms
133,216 KB
testcase_04 AC 1,087 ms
133,056 KB
testcase_05 AC 2,975 ms
139,348 KB
testcase_06 AC 2,896 ms
140,504 KB
testcase_07 AC 2,868 ms
143,752 KB
testcase_08 AC 2,884 ms
141,824 KB
testcase_09 AC 2,905 ms
142,992 KB
testcase_10 AC 1,112 ms
133,616 KB
testcase_11 AC 3,327 ms
137,800 KB
testcase_12 AC 3,809 ms
143,556 KB
testcase_13 AC 65 ms
68,604 KB
testcase_14 AC 65 ms
68,164 KB
testcase_15 AC 3,648 ms
135,884 KB
testcase_16 AC 3,992 ms
140,252 KB
testcase_17 AC 3,947 ms
139,800 KB
testcase_18 AC 3,452 ms
132,056 KB
testcase_19 AC 3,468 ms
133,396 KB
testcase_20 AC 3,464 ms
132,732 KB
testcase_21 AC 2,134 ms
109,952 KB
testcase_22 AC 1,967 ms
108,520 KB
testcase_23 AC 906 ms
90,556 KB
testcase_24 AC 2,363 ms
114,792 KB
testcase_25 AC 3,954 ms
141,148 KB
testcase_26 AC 673 ms
86,828 KB
testcase_27 AC 1,434 ms
100,360 KB
testcase_28 AC 1,553 ms
100,844 KB
testcase_29 AC 1,372 ms
98,368 KB
testcase_30 AC 2,564 ms
116,360 KB
testcase_31 AC 2,380 ms
113,584 KB
testcase_32 AC 2,829 ms
119,640 KB
testcase_33 AC 2,928 ms
120,788 KB
testcase_34 AC 4,120 ms
136,236 KB
testcase_35 AC 67 ms
68,544 KB
testcase_36 AC 68 ms
69,848 KB
testcase_37 AC 67 ms
68,636 KB
testcase_38 AC 67 ms
69,568 KB
testcase_39 AC 67 ms
68,416 KB
testcase_40 AC 67 ms
68,696 KB
testcase_41 AC 67 ms
70,344 KB
testcase_42 AC 65 ms
68,476 KB
testcase_43 AC 68 ms
69,492 KB
testcase_44 AC 65 ms
68,336 KB
testcase_45 AC 2,238 ms
138,604 KB
testcase_46 AC 2,373 ms
139,400 KB
testcase_47 AC 2,322 ms
138,412 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 reset(self):
        for i in range(len(self.p)):
            self.p[i] = -1
            self.L[i] = i
            self.R[i] = i
    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
            self.L[y] = min(self.L[y],self.L[x])
            self.R[y] = max(self.R[y],self.R[x])
        else:
            p[x] += p[y]
            p[y] = x
            self.L[x] = min(self.L[y],self.L[x])
            self.R[x] = max(self.R[y],self.R[x])
 
    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):
        self.score -= self.sub_score(self.root(v))
        self.num_pair[self.root(v)]+=1
        self.score += self.sub_score(self.root(v))
    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)])

import sys
from typing import Union
input = sys.stdin.readline

def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    z = list(map(int,input().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

    mid = [[]for i in range(n)]
    ufa = UnionFind_AB(n)
    ufb = UnionFind_AB(n)
    for q in range(17):
        for i in range(n):
            if r[i]-l[i]>1:
                mid[(l[i]+r[i])//2].append(i)
        
        if q > 0:
            ufa.reset()
            ufb.reset()

        for k in range(n):
            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 range(n):
        mid[r[i]].append(i)

    less_k = [0]*n
    ufi = UnionFind_IDX(n)
    ans = []
    for k in range(n):
        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 x in mid[k]:
            ufi.add_pair(pos_left[x])
        ans.append(ufi.score)

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

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