結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー SSRSSSRS
提出日時 2021-11-26 23:11:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,673 ms / 3,000 ms
コード長 3,503 bytes
コンパイル時間 2,186 ms
コンパイル使用メモリ 186,508 KB
実行使用メモリ 25,272 KB
最終ジャッジ日時 2023-09-12 05:54:29
合計ジャッジ時間 43,889 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 40 ms
4,376 KB
testcase_02 AC 39 ms
4,380 KB
testcase_03 AC 39 ms
4,376 KB
testcase_04 AC 39 ms
4,380 KB
testcase_05 AC 39 ms
4,380 KB
testcase_06 AC 2,011 ms
8,980 KB
testcase_07 AC 2,146 ms
13,268 KB
testcase_08 AC 1,836 ms
11,488 KB
testcase_09 AC 1,893 ms
8,952 KB
testcase_10 AC 1,985 ms
13,488 KB
testcase_11 AC 2,316 ms
13,892 KB
testcase_12 AC 2,353 ms
13,748 KB
testcase_13 AC 2,315 ms
13,828 KB
testcase_14 AC 2,309 ms
13,804 KB
testcase_15 AC 2,310 ms
13,732 KB
testcase_16 AC 2,637 ms
25,152 KB
testcase_17 AC 2,638 ms
25,200 KB
testcase_18 AC 2,650 ms
25,148 KB
testcase_19 AC 2,673 ms
25,216 KB
testcase_20 AC 2,667 ms
25,272 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 35 ms
4,380 KB
testcase_25 AC 2,609 ms
25,264 KB
testcase_26 AC 2,612 ms
25,184 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct sparse_table{
	vector<vector<T>> ST;
	sparse_table(vector<T> &A){
		int N = A.size();
		int LOG = 32 - __builtin_clz(N);
		ST = vector<vector<T>>(LOG, vector<T>(N));
		for (int i = 0; i < N; i++){
			ST[0][i] = A[i];
		}
		for (int i = 0; i < LOG - 1; i++){
			for (int j = 0; j < N - (1 << i); j++){
				ST[i + 1][j] = min(ST[i][j], ST[i][j + (1 << i)]);
			}
		}
	}
	T range_min(int L, int R){
		int d = 31 - __builtin_clz(R - L);
		return min(ST[d][L], ST[d][R - (1 << d)]);
	}
};
template <typename T>
struct lazy_segment_tree{
	int N;
	vector<T> ST;
	vector<T> lazy;
	lazy_segment_tree(vector<T> A){
		int n = A.size();
		N = 1;
		while (N < n){
			N *= 2;
		}
		ST = vector<T>(N * 2 - 1, 0);
		lazy = vector<T>(N * 2 - 1, 0);
		for (int i = 0; i < n; i++){
			ST[N - 1 + i] = A[i];
		}
		for (int i = N - 2; i >= 0; i--){
			ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]);
		}
	}
	void eval(int i){
    if (i < N - 1){
      lazy[i * 2 + 1] = max(lazy[i * 2 + 1], lazy[i]);
      lazy[i * 2 + 2] = max(lazy[i * 2 + 2], lazy[i]);
    }
    ST[i] = max(ST[i], lazy[i]);
	}
	void range_chmax(int L, int R, T x, int i, int l, int r){
		eval(i);
		if (R <= l || r <= L){
			return;
		} else if (L <= l && r <= R){
			lazy[i] = max(lazy[i], x);
			eval(i);
		} else {
			int m = (l + r) / 2;
			range_chmax(L, R, x, i * 2 + 1, l, m);
			range_chmax(L, R, x, i * 2 + 2, m, r);
			ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]);
		}
	}
	void range_chmax(int L, int R, T x){
		return range_chmax(L, R, x, 0, 0, N);
	}
	T range_max(int L, int R, int i, int l, int r){
		eval(i);
		if (R <= l || r <= L){
			return 0;
		} else if (L <= l && r <= R){
			return ST[i];
		} else {
			int m = (l + r) / 2;
			return max(range_max(L, R, i * 2 + 1, l, m), range_max(L, R, i * 2 + 2, m, r));
		}
	}
	T range_max(int L, int R){
		return range_max(L, R, 0, 0, N);
	}
};
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N;
    cin >> N;
    vector<int> A(N);
    for (int j = 0; j < N; j++){
      cin >> A[j];
    }
    vector<int> B(N);
    for (int j = 0; j < N; j++){
      cin >> B[j];
    }
    bool ok = true;
    for (int j = 0; j < N; j++){
      if (B[j] < A[j]){
        ok = false;
      }
    }
    if (!ok){
      cout << "No" << "\n";
    } else {
      vector<pair<int, int>> P(N);
      for (int j = 0; j < N; j++){
        P[j] = make_pair(A[j], j);
      }
      sort(P.begin(), P.end());
      lazy_segment_tree<int> ST1(A);
      sparse_table<int> ST2(B);
      for (int j = 0; j < N; j++){
        int x = P[j].first;
        int p = P[j].second;
        int tv1 = p, fv1 = -1;
        while (tv1 - fv1 > 1){
          int mid = (tv1 + fv1) / 2;
          if (ST1.range_max(mid, p + 1) <= x && ST2.range_min(mid, p + 1) >= x){
            tv1 = mid;
          } else {
            fv1 = mid;
          }
        }
        int tv2 = p + 1, fv2 = N + 1;
        while (fv2 - tv2 > 1){
          int mid = (tv2 + fv2) / 2;
          if (ST1.range_max(p, mid) <= x && ST2.range_min(p, mid) >= x){
            tv2 = mid;
          } else {
            fv2 = mid;
          }
        }
        ST1.range_chmax(tv1, tv2, x);
      }
      for (int j = 0; j < N; j++){
        if (ST1.range_max(j, j + 1) != B[j]){
          ok = false;
        }
      }
      if (ok){
        cout << "Yes" << "\n";
      } else {
        cout << "No" << "\n";
      }
    }
  }
}
0