結果

問題 No.2242 Cities and Teleporters
ユーザー kwm_tkwm_t
提出日時 2023-03-11 18:21:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 3,000 ms
コード長 2,151 bytes
コンパイル時間 7,469 ms
コンパイル使用メモリ 264,400 KB
実行使用メモリ 25,876 KB
最終ジャッジ日時 2023-10-18 10:11:18
合計ジャッジ時間 15,984 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 331 ms
25,876 KB
testcase_06 AC 313 ms
25,876 KB
testcase_07 AC 344 ms
25,876 KB
testcase_08 AC 487 ms
25,876 KB
testcase_09 AC 287 ms
25,876 KB
testcase_10 AC 167 ms
25,876 KB
testcase_11 AC 238 ms
25,876 KB
testcase_12 AC 239 ms
25,876 KB
testcase_13 AC 244 ms
25,876 KB
testcase_14 AC 303 ms
25,876 KB
testcase_15 AC 247 ms
25,876 KB
testcase_16 AC 287 ms
25,876 KB
testcase_17 AC 385 ms
25,876 KB
testcase_18 AC 239 ms
25,552 KB
testcase_19 AC 391 ms
25,444 KB
testcase_20 AC 235 ms
24,904 KB
testcase_21 AC 235 ms
25,012 KB
testcase_22 AC 388 ms
24,904 KB
testcase_23 AC 360 ms
25,876 KB
testcase_24 AC 244 ms
25,876 KB
testcase_25 AC 209 ms
25,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
#define debug(v){{rep(_,v.size())cout << v[_] <<" ";}cout<< endl;}
template<typename A, typename B> inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; }
int op(int a, int b) { return max(a, b); }
int e() { return -1; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<int>h(n), t(n);
	rep(i, n)cin >> h[i];
	rep(i, n)cin >> t[i];
	vector<int> idx(n);
	iota(all(idx), 0);
	sort(all(idx), [&](int i, int j) {return h[i] < h[j]; });
	vector<int>inv(n);
	rep(i, n)inv[idx[i]] = i;
	vector<int> hs(n), ts(n);
	rep(i, n) hs[i] = h[idx[i]], ts[i] = t[idx[i]];
	vector<int> rs(n);
	rep(i, n) rs[i] = upper_bound(all(hs), ts[i]) - hs.begin();
	const int sz = 20;
	vector<vector<int>> dp(sz, vector<int>(n));
	vector<int> rui(n + 1, -1), arg(n + 1, -1);
	rep(i, n) {
		rui[i + 1] = rui[i];
		arg[i + 1] = arg[i];
		if (chmax(rui[i + 1], rs[i])) arg[i + 1] = i;
	}
	rep(i, n) dp[0][i] = arg[rs[i]];
	rep(i, sz - 1) {
		rep(j, n) {
			dp[i + 1][j] = (dp[i][j] == -1 ? -1 : dp[i][dp[i][j]]);
		}
	}
	int q; cin >> q;
	while (q--) {
		int a, b; cin >> a >> b;
		a--, b--;
		a = inv[a], b = inv[b];
		if (b < rs[a]) {
			cout << 1 << endl;
			continue;
		}
		int ans = 0;
		rrep(i, sz) {
			if (-1 == dp[i][a])continue;
			if (b >= rs[dp[i][a]]) {
				ans += 1 << i;
				a = dp[i][a];
			}
		}
		a = dp[0][a];
		cout << (a == -1 ? -1 : b < rs[a] ? ans + 2 : -1) << endl;
	}
	return 0;
}
0