結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー gazellegazelle
提出日時 2021-11-26 23:12:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 3,000 ms
コード長 3,238 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 216,512 KB
実行使用メモリ 13,796 KB
最終ジャッジ日時 2023-09-12 05:55:55
合計ジャッジ時間 7,575 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 8 ms
4,380 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 9 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 183 ms
5,804 KB
testcase_07 AC 192 ms
8,336 KB
testcase_08 AC 178 ms
8,004 KB
testcase_09 AC 180 ms
5,748 KB
testcase_10 AC 184 ms
8,600 KB
testcase_11 AC 199 ms
8,264 KB
testcase_12 AC 199 ms
8,348 KB
testcase_13 AC 199 ms
8,344 KB
testcase_14 AC 199 ms
8,244 KB
testcase_15 AC 200 ms
8,404 KB
testcase_16 AC 217 ms
13,796 KB
testcase_17 AC 214 ms
13,676 KB
testcase_18 AC 214 ms
13,676 KB
testcase_19 AC 215 ms
13,792 KB
testcase_20 AC 215 ms
13,744 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 57 ms
4,380 KB
testcase_25 AC 168 ms
13,680 KB
testcase_26 AC 194 ms
13,792 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i, n, m) for(long long i = (n); i < (long long)(m); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr ld eps = 1e-6;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
    REP(i, v.size()) {
        if(i)
            os << " ";
        os << v[i];
    }
    return os;
}

template <typename T> struct segment_tree {
  private:
    int n;
    const function<T(T, T)> op;
    const T ie;
    vector<T> seq;

  public:
    segment_tree(int _n, function<T(T, T)> op, const T ie) : op(op), ie(ie) {
        n = 1;
        while(n < _n)
            n *= 2;
        seq.resize(2 * n - 1);
        for(int i = 0; i < 2 * n - 1; i++)
            seq[i] = ie;
    }

    void update(int k, const T e) {
        k += n - 1;
        seq[k] = e;
        while(k > 0) {
            k = (k - 1) / 2;
            seq[k] = op(seq[k * 2 + 1], seq[k * 2 + 2]);
        }
    }

    T get(int k) {
        k += n - 1;
        return seq[k];
    }

    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if(r == -1)
            r = n;
        if(r <= a || b <= l)
            return ie;
        if(a <= l && r <= b)
            return seq[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return op(vl, vr);
    }
};
// segment_tree<ll> rsq(n, [] (ll a, ll b) { return a + b; }, 0)
// segment_tree<ll> rMq(n, [] (ll a, ll b) { return max(a, b); }, -1e18)
// segment_tree<ll> rmq(n, [] (ll a, ll b) { return min(a, b); }, 1e18)

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int t;
    cin >> t;

    REP(testcase, t) {
        int n;
        cin >> n;
        vector<int> a(n), b(n);
        REP(i, n) cin >> a[i];
        REP(i, n) cin >> b[i];
        vector<bool> ok(n, false);
        segment_tree<ll> rmq(
            n + 1, [](ll a, ll b) { return min(a, b); }, 1e18);
        segment_tree<ll> rMq(
            n + 1, [](ll a, ll b) { return max(a, b); }, -1e18);
        REP(i, n) rmq.update(i, b[i]);
        REP(i, n) rMq.update(i, a[i]);
        vector<int> last_i(n + 1, -1);

        REP(i, n) {
            last_i[a[i]] = i;
            if(last_i[b[i]] != -1 && rmq.query(last_i[b[i]], i + 1) >= b[i] &&
               rMq.query(last_i[b[i]], i + 1) <= b[i])
                ok[i] = true;
        }

        REP(i, n + 1) last_i[i] = -1;

        for(int i = n - 1; i >= 0; i--) {
            last_i[a[i]] = i;
            if(last_i[b[i]] != -1 && rmq.query(i, last_i[b[i]] + 1) >= b[i] &&
               rMq.query(i, last_i[b[i]] + 1) <= b[i])
                ok[i] = true;
        }

        REP(i, n) if(a[i] > b[i]) ok[i] = false;

        if(count(ALL(ok), false) == 0)
            cout << "Yes"
                 << "\n";
        else
            cout << "No"
                 << "\n";
    }
    return 0;
}
0