結果

問題 No.2242 Cities and Teleporters
ユーザー olpheolphe
提出日時 2023-03-10 22:32:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 796 ms / 3,000 ms
コード長 2,453 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 123,484 KB
実行使用メモリ 40,076 KB
最終ジャッジ日時 2023-10-18 08:15:10
合計ジャッジ時間 15,973 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 629 ms
40,076 KB
testcase_06 AC 604 ms
40,076 KB
testcase_07 AC 619 ms
40,076 KB
testcase_08 AC 796 ms
40,076 KB
testcase_09 AC 653 ms
40,076 KB
testcase_10 AC 409 ms
40,076 KB
testcase_11 AC 535 ms
40,076 KB
testcase_12 AC 532 ms
40,076 KB
testcase_13 AC 551 ms
40,076 KB
testcase_14 AC 594 ms
40,076 KB
testcase_15 AC 550 ms
40,076 KB
testcase_16 AC 629 ms
40,076 KB
testcase_17 AC 764 ms
40,076 KB
testcase_18 AC 463 ms
39,508 KB
testcase_19 AC 454 ms
39,404 KB
testcase_20 AC 543 ms
38,352 KB
testcase_21 AC 532 ms
38,456 KB
testcase_22 AC 448 ms
38,356 KB
testcase_23 AC 472 ms
40,076 KB
testcase_24 AC 469 ms
40,076 KB
testcase_25 AC 466 ms
40,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct Node {
	int height;
	int to;
	int idx;
	bool operator<(const Node& n)const {
		return height < n.height;
	}
};

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

	cin >> N;
	vector<Node>node(N);
	for (int i = 0; i < N; i++) {
		cin >> node[i].height;
		node[i].idx = i;
	}
	for (int i = 0; i < N; i++) {
		cin >> node[i].to;
	}
	sort(node.begin(), node.end());
	vector<int>conv(N);
	for (int i = 0; i < N; i++) {
		conv[node[i].idx] = i;
	}
	vector<int>mxh(N);
	vector<int>mxmx(N);
	mxh[0] = node[0].height;
	mxmx[0] = node[0].to;
	for (int i = 1; i < N; i++) {
		mxh[i] = max(mxh[i - 1], node[i].height);
		mxmx[i] = max(mxmx[i - 1], node[i].to);
	}
	vector<vector<int>>to(20, vector<int>(N, -1));
	vector<vector<int>>mxto(20, vector<int>(N, -1));
	for (int i = 0; i < N; i++) {
		to[0][i] = upper_bound(mxh.begin(), mxh.end(), node[i].to) - mxh.begin();
		to[0][i]--;
		mxto[0][i] = to[0][i];
		if(i)mxto[0][i] = max(mxto[0][i-1], to[0][i]);
	//	cout << 0 << " " << i << " " << mxto[0][i] << endl;
	}
	for (int i = 1; i < 20; i++) {
		int mx = -1;
		for (int j = 0; j < N; j++) {
			if (mxto[i - 1][j] >= 0) {
				to[i][j] = mxto[i - 1][mxto[i-1][j]];
			}
			mx = max(mx, to[i][j]);
			mxto[i][j] = mx;
		//	cout << i << " " << j << " " << mxto[i][j] << endl;
		}
	}
	cin >> K;
	while (K--) {
		cin >> L >> R;
		L--, R--;
		L = conv[L];
		R = conv[R];
		int ans = 1;
		H = node[L].to;
		if (H >= node[R].height) {
			cout << 1 << endl;
			continue;
		}
		auto it = upper_bound(mxh.begin(), mxh.end(), H);
		if (it == mxh.begin()) {
			cout << -1 << endl;
			continue;
		}
		//if(L==)
		L = it - mxh.begin() - 1;
		if (mxto.back()[L] < R) {
			cout << -1 << endl;
			continue;
		}
		for (int i = 19; i >= 0; i--) {
			if (mxto[i][L] < R) {
				L = mxto[i][L];
				ans += 1 << i;
			}
		//	cout << i << " " << L << endl;
		}
		cout << ans + 1 << endl;
	}
}
0