結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー SSRSSSRS
提出日時 2021-11-26 23:10:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,452 bytes
コンパイル時間 2,482 ms
コンパイル使用メモリ 186,900 KB
実行使用メモリ 25,444 KB
最終ジャッジ日時 2023-09-12 05:55:30
合計ジャッジ時間 48,934 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 44 ms
4,380 KB
testcase_02 AC 43 ms
4,376 KB
testcase_03 AC 43 ms
4,380 KB
testcase_04 AC 43 ms
4,380 KB
testcase_05 AC 44 ms
4,380 KB
testcase_06 AC 2,177 ms
8,892 KB
testcase_07 AC 2,338 ms
13,224 KB
testcase_08 AC 1,979 ms
11,508 KB
testcase_09 AC 2,055 ms
9,172 KB
testcase_10 AC 2,164 ms
13,596 KB
testcase_11 AC 2,537 ms
13,984 KB
testcase_12 AC 2,548 ms
14,036 KB
testcase_13 AC 2,560 ms
14,092 KB
testcase_14 AC 2,573 ms
14,032 KB
testcase_15 AC 2,554 ms
14,108 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 2,993 ms
25,444 KB
testcase_20 TLE -
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 132 ms
4,376 KB
testcase_25 AC 2,852 ms
25,176 KB
testcase_26 AC 2,847 ms
25,284 KB
testcase_27 AC 1 ms
4,380 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(){
  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" << endl;
    } 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" << endl;
      } else {
        cout << "No" << endl;
      }
    }
  }
}
0