結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー chineristACchineristAC
提出日時 2021-10-24 03:54:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 873 ms / 3,000 ms
コード長 4,075 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,124 KB
実行使用メモリ 145,156 KB
最終ジャッジ日時 2024-06-29 18:54:23
合計ジャッジ時間 15,214 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
56,848 KB
testcase_01 AC 166 ms
79,712 KB
testcase_02 AC 160 ms
79,520 KB
testcase_03 AC 165 ms
79,408 KB
testcase_04 AC 157 ms
79,108 KB
testcase_05 AC 154 ms
79,192 KB
testcase_06 AC 775 ms
137,944 KB
testcase_07 AC 854 ms
143,956 KB
testcase_08 AC 818 ms
125,268 KB
testcase_09 AC 812 ms
131,072 KB
testcase_10 AC 829 ms
127,592 KB
testcase_11 AC 873 ms
135,680 KB
testcase_12 AC 850 ms
139,384 KB
testcase_13 AC 631 ms
142,728 KB
testcase_14 AC 723 ms
145,156 KB
testcase_15 AC 864 ms
139,844 KB
testcase_16 AC 674 ms
139,616 KB
testcase_17 AC 719 ms
139,760 KB
testcase_18 AC 756 ms
140,148 KB
testcase_19 AC 621 ms
139,760 KB
testcase_20 AC 707 ms
139,628 KB
testcase_21 AC 39 ms
55,908 KB
testcase_22 AC 38 ms
56,296 KB
testcase_23 AC 58 ms
72,356 KB
testcase_24 AC 313 ms
81,348 KB
testcase_25 AC 440 ms
132,132 KB
testcase_26 AC 326 ms
131,752 KB
testcase_27 AC 43 ms
56,772 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] = self.segfunc(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 bisect_left(self,l,r,x):
        l += self.num
        r += self.num
        segment = []
        segment_r = []
        while l<r:
            if l&1:
                segment.append(l)
                l += 1
            if r&1:
                segment_r.append(r-1)
            l >>= 1
            r >>= 1
        segment += segment_r[::-1]
        tmp = self.ide_ele
        for pos in segment:
            check = self.segfunc(tmp,self.tree[pos])
            if x <= check :
                break
            else:
                tmp = check
        else:
            return -1
        
        while pos < self.num:
            check = self.segfunc(tmp,self.tree[2*pos])
            if x <= check:
                pos = 2 * pos
            else:
                tmp = check
                pos = 2 * pos + 1
        return pos - self.num
    
    def bisect_right(self,l,r,x):
        l += self.num
        r += self.num
        segment = []
        segment_r = []
        while l<r:
            if l&1:
                segment.append(l)
                l += 1
            if r&1:
                segment_r.append(r-1)
            l >>= 1
            r >>= 1
        segment += segment_r[::-1]
        tmp = self.ide_ele
        for pos in segment[::-1]:
            check = self.segfunc(tmp,self.tree[pos])
            if check >= x:
                break
            else:
                tmp = check
        else:
            return -1
        
        while pos < self.num:
            check = self.segfunc(tmp,self.tree[2*pos])
            if check >= x:
                pos = 2 * pos
            else:
                tmp = check
                pos = 2 * pos + 1
        return pos - self.num
    


import sys,random
from collections import deque
import bisect

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(N,A,B):
    if any(A[i] > B[i] for i in range(N)):
        return "No"
    
    val_to_idx = [[] for i in range(N+1)]
    check = [[] for i in range(N+1)]
    for i in range(N):
        val_to_idx[A[i]].append(i)
        check[B[i]].append(i)
    
    seg_A = SegmentTree(A,max,0)
    seg_B = SegmentTree(B,min,N+1)
    for k in range(1,N+1):
        t = val_to_idx[k]
        for idx in check[k]:
            flag = (A[idx]==k)
            i = bisect.bisect_right(t,idx)
            if i!=0:
                l = t[i-1]
                if seg_A.query(l,idx+1) <= k <= seg_B.query(l,idx+1):
                    flag = True
            if i!=len(t):
                r = t[i]
                if seg_A.query(idx,r+1) <= k <= seg_B.query(idx,r+1):
                    flag = True
            
            if not flag:
                return "No"
    
    return "Yes"

for _ in range(int(input())):
    N = int(input())
    A = li()
    B = li()
    print(solve(N,A,B))
            
            

0