結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー 👑 emthrmemthrm
提出日時 2021-11-26 23:33:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 3,000 ms
コード長 3,377 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 222,660 KB
実行使用メモリ 29,508 KB
最終ジャッジ日時 2023-09-12 06:01:02
合計ジャッジ時間 5,951 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 5 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,384 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 96 ms
11,260 KB
testcase_07 AC 98 ms
13,320 KB
testcase_08 AC 92 ms
11,424 KB
testcase_09 AC 80 ms
10,256 KB
testcase_10 AC 104 ms
14,084 KB
testcase_11 AC 122 ms
14,808 KB
testcase_12 AC 92 ms
13,880 KB
testcase_13 AC 115 ms
14,632 KB
testcase_14 AC 92 ms
14,256 KB
testcase_15 AC 104 ms
14,828 KB
testcase_16 AC 145 ms
26,124 KB
testcase_17 AC 146 ms
25,932 KB
testcase_18 AC 145 ms
26,008 KB
testcase_19 AC 95 ms
22,092 KB
testcase_20 AC 98 ms
22,108 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 32 ms
4,384 KB
testcase_25 AC 129 ms
29,508 KB
testcase_26 AC 101 ms
29,488 KB
testcase_27 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

void solve() {
  int n; cin >> n;
  n += 2;
  vector<int> a(n, INF), b(n, INF);
  FOR(i, 1, n - 1) cin >> a[i];
  FOR(i, 1, n - 1) cin >> b[i];
  REP(i, n) {
    if (a[i] > b[i]) {
      cout << "No\n";
      return;
    }
  }
  map<int, vector<pair<int, int>>> seg;
  for (int i = 1; i < n - 1;) {
    int j = i + 1;
    while (j < n - 1 && b[i] == b[j]) ++j;
    seg[-b[i]].emplace_back(i, j - 1);
    i = j;
  }
  vector<set<int>> que(n);
  for (const auto [num_, v] : seg) {
    const int num = -num_;
    for (const auto [l, r] : v) {
      if (b[l - 1] > num && que[l].count(num) == 1) {
        if (b[r + 1] < num) {
          int mx = -INF;
          for (int i = r; i >= l; --i) {
            if (chmax(mx, a[i]) && mx < num) que[r + 1].emplace(mx);
          }
          for (auto it = que[l].begin(); it != que[l].end() && *it <= mx; it = que[l].erase(it)) {}
          while (!que[l].empty() && *que[l].rbegin() >= num) {
            que[l].erase(prev(que[l].end()));
          }
          if (que[l].size() > que[r + 1].size()) swap(que[l], que[r + 1]);
          copy(ALL(que[l]), inserter(que[r + 1], que[r + 1].end()));
        }
      } else if (b[r + 1] > num && que[r].count(num) == 1) {
        if (b[l - 1] < num) {
          int mx = -INF;
          for (int i = l; i <= r; ++i) {
            if (chmax(mx, a[i]) && mx < num) que[l - 1].emplace(mx);
          }
          for (auto it = que[r].begin(); it != que[r].end() && *it <= mx; it = que[r].erase(it)) {}
          while (!que[r].empty() && *que[r].rbegin() >= num) {
            que[r].erase(prev(que[r].end()));
          }
          if (que[r].size() > que[l - 1].size()) swap(que[r], que[l - 1]);
          copy(ALL(que[r]), inserter(que[l - 1], que[l - 1].end()));
        }
      } else if (count(a.begin() + l, a.begin() + (r + 1), num) > 0) {
        FOR(i, l, r + 1) {
          if (a[i] == num) {
            int mx = -INF;
            FOR(j, l, i) {
              if (chmax(mx, a[j]) && mx < num) que[l - 1].emplace(mx);
            }
            break;
          }
        }
        for (int i = r; i >= l; --i) {
          if (a[i] == num) {
            int mx = -INF;
            for (int j = r; j > i; --j) {
              if (chmax(mx, a[j]) && mx < num) que[r + 1].emplace(mx);
            }
            break;
          }
        }
      } else {
        cout << "No\n";
        return;
      }
    }
  }
  cout << "Yes\n";
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}
0