結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー se1ka2se1ka2
提出日時 2021-11-26 23:44:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,873 ms / 3,000 ms
コード長 4,219 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 92,136 KB
実行使用メモリ 26,144 KB
最終ジャッジ日時 2023-09-12 06:03:02
合計ジャッジ時間 10,103 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,596 KB
testcase_01 AC 15 ms
10,544 KB
testcase_02 AC 15 ms
9,976 KB
testcase_03 AC 14 ms
9,756 KB
testcase_04 AC 14 ms
9,908 KB
testcase_05 AC 15 ms
10,508 KB
testcase_06 AC 250 ms
14,500 KB
testcase_07 AC 270 ms
17,964 KB
testcase_08 AC 242 ms
16,324 KB
testcase_09 AC 246 ms
14,876 KB
testcase_10 AC 253 ms
18,732 KB
testcase_11 AC 296 ms
19,200 KB
testcase_12 AC 257 ms
18,016 KB
testcase_13 AC 239 ms
17,900 KB
testcase_14 AC 251 ms
18,168 KB
testcase_15 AC 275 ms
18,820 KB
testcase_16 AC 324 ms
26,088 KB
testcase_17 AC 322 ms
26,144 KB
testcase_18 AC 322 ms
26,140 KB
testcase_19 AC 267 ms
23,160 KB
testcase_20 AC 312 ms
25,652 KB
testcase_21 AC 6 ms
9,820 KB
testcase_22 AC 7 ms
9,676 KB
testcase_23 AC 46 ms
9,696 KB
testcase_24 AC 2,873 ms
9,680 KB
testcase_25 AC 232 ms
25,760 KB
testcase_26 AC 181 ms
21,072 KB
testcase_27 AC 5 ms
9,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef pair<int, int> P;

template <typename T>
struct SegmentTree
{
    using F = function<T(T, T)>;
    
    const F f;
    const T e;
    
    int n;
    vector<T> seg;
    
    SegmentTree(int nn, const F f, const T e) : f(f), e(e){
        n = 1;
        while(n <= nn) n <<= 1;
        seg.assign(n * 2, e);
    }
    
    void set(int i, T x){
        seg[i + n] = x;
    }
    
    void build(){
        for(int k = n - 1; k > 0; k--){
            seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
        }
    }
    
    void update(int i, T x){
        int k = i + n;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
        }
    }
    
    T query(int l, int r){
        l += n, r += n;
        T L = e, R = e;
        for(; l != r; l >>= 1, r >>= 1){
            if(l % 2) L = f(L, seg[l++]);
            if(r % 2) R = f(seg[--r], R);
        }
        return f(L, R);
    }
    
    T operator[](const int i)const {
        return seg[i + n];
    }
    
    template<typename C>
    int right_bound_sub(int k, const C &check, T x){
        while(k < n){
            if(check(f(x, seg[k * 2]))){
                k = k * 2;
            }
            else{
                x = f(x, seg[k * 2]);
                k = k * 2 + 1;
            }
        }
        return k - n;
    }
    
    template <typename C>
    int right_bound(int i, const C &check){
        T x = e;
        for(int l = i + n, r = n * 2; l != r; l >>= 1, r >>= 1){
            if(l % 2){
                if(check(f(x, seg[l]))){
                    return right_bound_sub(l, check, x);
                }
                x = f(x, seg[l]);
                l++;
            }
        }
        return -1;
    }
    
    template <typename C>
    int left_bound_sub(int k, const C &check, T x){
        while(k < n){
            if(check(f(seg[k * 2 + 1], x))){
                k = k * 2 + 1;
            }
            else{
                x = f(seg[k * 2 + 1], x);
                k = k * 2;
            }
        }
        return k - n;
    }
    
    template <typename C>
    int left_bound(int i, const C &check){
        T x = e;
        for(int l = n, r = i + n; l != r; l >>= 1, r >>= 1){
            if(r % 2){
                if(check(f(seg[--r], x))){
                    return left_bound_sub(r, check, x);
                }
                x = f(x, seg[r]);
            }
        }
        return -1;
    }
};

vector<int> v[200005];

int main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int a[200005], b[200005];
        P p[200005];
        for(int i = 0; i < n; i++){
            cin >> a[i];
            a[i]--;
            v[a[i]].push_back(i);
        }
        for(int i = 0; i < n; i++){
            cin >> b[i];
            b[i]--;
            p[i] = P(b[i], i);
        }
        sort(p, p + n);
        SegmentTree<int> seg(n, [](int a, int b){return max(a, b);}, 0);
        for(int i = 0; i < n; i++) seg.set(i, a[i]);
        seg.build();
        bool f = true;
        int last = 0;
        set<int> st, st0;
        for(int u = 0; u < n; u++){
            f = false;
            int i = p[u].second;
            if(b[i] > last){
                for(int x : st0) st.insert(x);
                st0.clear();
                last = b[i];
            }
            int j = lower_bound(v[b[i]].begin(), v[b[i]].end(), i) - v[b[i]].begin();
            if(j < (int)v[b[i]].size()){
                int r = v[b[i]][j];
                auto itr = st.lower_bound(i);
                if(itr != st.end()) r = min(r, *itr);
                if(seg.query(i, r + 1) == b[i]) f = true;
            }
            if(j > 0){
                int l = v[b[i]][j - 1];
                auto itr = st.lower_bound(i);
                if(itr != st.begin()) l = max(l, *--itr);
                if(seg.query(l, i + 1) == b[i]) f = true;
            }
            if(!f) break;
            st0.insert(i);
        }
        if(f) cout << "Yes" << endl;
        else cout << "No" << endl;
        for(int i = 0; i < n; i++) v[i].clear();
    }
}
0