結果

問題 No.1698 Face to Face
ユーザー eSeFeSeF
提出日時 2021-09-18 16:21:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,715 ms / 5,000 ms
コード長 3,862 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 220,476 KB
最終ジャッジ日時 2024-07-19 10:01:05
合計ジャッジ時間 103,885 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,248 KB
testcase_01 AC 42 ms
53,248 KB
testcase_02 AC 42 ms
53,248 KB
testcase_03 AC 851 ms
182,144 KB
testcase_04 AC 1,266 ms
198,728 KB
testcase_05 AC 3,473 ms
215,304 KB
testcase_06 AC 3,429 ms
216,072 KB
testcase_07 AC 3,347 ms
220,476 KB
testcase_08 AC 3,279 ms
218,052 KB
testcase_09 AC 3,349 ms
217,732 KB
testcase_10 AC 1,236 ms
199,124 KB
testcase_11 AC 3,816 ms
213,632 KB
testcase_12 AC 4,091 ms
217,092 KB
testcase_13 AC 42 ms
53,376 KB
testcase_14 AC 42 ms
53,120 KB
testcase_15 AC 4,715 ms
219,240 KB
testcase_16 AC 4,425 ms
214,568 KB
testcase_17 AC 4,490 ms
213,620 KB
testcase_18 AC 3,975 ms
199,420 KB
testcase_19 AC 4,252 ms
204,464 KB
testcase_20 AC 3,900 ms
198,576 KB
testcase_21 AC 2,747 ms
149,940 KB
testcase_22 AC 2,322 ms
134,448 KB
testcase_23 AC 1,116 ms
98,084 KB
testcase_24 AC 2,918 ms
156,964 KB
testcase_25 AC 4,159 ms
206,528 KB
testcase_26 AC 778 ms
90,872 KB
testcase_27 AC 1,897 ms
121,316 KB
testcase_28 AC 1,852 ms
121,412 KB
testcase_29 AC 1,591 ms
114,172 KB
testcase_30 AC 2,926 ms
164,456 KB
testcase_31 AC 2,646 ms
148,544 KB
testcase_32 AC 3,699 ms
185,544 KB
testcase_33 AC 3,639 ms
186,720 KB
testcase_34 AC 4,601 ms
213,980 KB
testcase_35 AC 42 ms
53,120 KB
testcase_36 AC 44 ms
53,504 KB
testcase_37 AC 42 ms
53,376 KB
testcase_38 AC 42 ms
53,376 KB
testcase_39 AC 42 ms
53,376 KB
testcase_40 AC 42 ms
53,248 KB
testcase_41 AC 42 ms
53,760 KB
testcase_42 AC 42 ms
53,504 KB
testcase_43 AC 41 ms
53,248 KB
testcase_44 AC 42 ms
53,376 KB
testcase_45 AC 2,870 ms
213,544 KB
testcase_46 AC 2,902 ms
213,636 KB
testcase_47 AC 2,793 ms
211,116 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
            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
input = sys.stdin.readline
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)]

for _ in range(17):
    for i in range(n):
        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 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)))
0