結果

問題 No.2242 Cities and Teleporters
ユーザー rabimearabimea
提出日時 2023-03-10 21:50:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,206 ms / 3,000 ms
コード長 1,202 bytes
コンパイル時間 4,383 ms
コンパイル使用メモリ 207,724 KB
実行使用メモリ 56,736 KB
最終ジャッジ日時 2023-10-18 07:36:21
合計ジャッジ時間 28,028 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
36,940 KB
testcase_01 AC 20 ms
36,936 KB
testcase_02 AC 20 ms
36,936 KB
testcase_03 AC 20 ms
36,936 KB
testcase_04 AC 20 ms
36,940 KB
testcase_05 AC 973 ms
47,364 KB
testcase_06 AC 694 ms
47,364 KB
testcase_07 AC 982 ms
47,364 KB
testcase_08 AC 1,040 ms
47,364 KB
testcase_09 AC 913 ms
47,364 KB
testcase_10 AC 978 ms
56,736 KB
testcase_11 AC 1,084 ms
56,736 KB
testcase_12 AC 1,169 ms
56,736 KB
testcase_13 AC 1,096 ms
56,732 KB
testcase_14 AC 1,163 ms
56,736 KB
testcase_15 AC 1,123 ms
56,732 KB
testcase_16 AC 1,053 ms
56,732 KB
testcase_17 AC 1,206 ms
56,736 KB
testcase_18 AC 1,079 ms
56,496 KB
testcase_19 AC 1,135 ms
56,380 KB
testcase_20 AC 1,050 ms
55,828 KB
testcase_21 AC 1,041 ms
55,888 KB
testcase_22 AC 1,095 ms
55,868 KB
testcase_23 AC 1,206 ms
56,736 KB
testcase_24 AC 1,180 ms
56,732 KB
testcase_25 AC 1,186 ms
56,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 11;
int h[N], t[N];

const int M = N * 2;
int b[M];
void update(int p, int c) {
	for(int i = p; i < M; i += i & (-i))
		b[i] = max(b[i], c);
}
int query(int p) {
	int r = 0;
	for(int i = p; i; i -= i & (-i))
		r = max(r, b[i]);
	return r;
}

int st[20][M];

int main() {
	ios::sync_with_stdio(0);
	
	int n; cin >> n;
	
	map <int, int> mp;
	for(int i = 1; i <= n; i ++) {
		cin >> h[i];
		mp[h[i]] = 1;
	}
	for(int i = 1; i <= n; i ++) {
		cin >> t[i];
		mp[t[i]] = 1;
	}
	
	int cnt = 0;
	for(auto &p : mp)
		p.second = ++ cnt;
	
	for(int i = 1; i <= n; i ++) {
		h[i] = mp[h[i]];
		t[i] = mp[t[i]];
		
		update(h[i], t[i]);
	}
	
	for(int i = 1; i < M; i ++) {
		st[0][i] = max(i, query(i));
	}
	
	for(int k = 1; k < 20; k ++)
		for(int i = 1; i < M; i ++) {
			st[k][i] = st[k - 1][st[k - 1][i]];	
		}
	
	int q; cin >> q;
	while(q --) {
		int a, b; cin >> a >> b;
		int ans = 0;
		int x = t[a];
		for(int k = 19; k >= 0; k --)
			if(st[k][x] < h[b]) {
				ans += (1 << k);
				x = st[k][x];
			}
		if(x < h[b]) {
			ans ++;
			x = st[0][x];
		}
		
		ans ++;
		
		if(x < h[b])
			cout << -1 << '\n';
		else
			cout << ans << '\n';
	}
}
0