結果

問題 No.2242 Cities and Teleporters
ユーザー prism17prism17
提出日時 2023-03-28 21:18:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 3,000 ms
コード長 1,798 bytes
コンパイル時間 2,792 ms
コンパイル使用メモリ 174,452 KB
実行使用メモリ 23,740 KB
最終ジャッジ日時 2023-10-20 14:24:50
合計ジャッジ時間 13,014 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,692 KB
testcase_01 AC 2 ms
5,692 KB
testcase_02 AC 2 ms
5,692 KB
testcase_03 AC 3 ms
5,692 KB
testcase_04 AC 2 ms
5,692 KB
testcase_05 AC 223 ms
23,740 KB
testcase_06 AC 192 ms
23,712 KB
testcase_07 AC 224 ms
23,740 KB
testcase_08 AC 285 ms
23,740 KB
testcase_09 AC 248 ms
23,740 KB
testcase_10 AC 155 ms
23,740 KB
testcase_11 AC 172 ms
23,740 KB
testcase_12 AC 159 ms
23,740 KB
testcase_13 AC 179 ms
23,740 KB
testcase_14 AC 183 ms
23,740 KB
testcase_15 AC 181 ms
23,740 KB
testcase_16 AC 216 ms
23,740 KB
testcase_17 AC 226 ms
23,740 KB
testcase_18 AC 150 ms
23,716 KB
testcase_19 AC 158 ms
23,476 KB
testcase_20 AC 171 ms
23,476 KB
testcase_21 AC 161 ms
23,476 KB
testcase_22 AC 158 ms
23,476 KB
testcase_23 AC 151 ms
23,740 KB
testcase_24 AC 152 ms
23,740 KB
testcase_25 AC 152 ms
23,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Problem: No.2242 Cities and Teleporters
// Contest: yukicoder
// URL: https://yukicoder.me/problems/no/2242
// Memory Limit: 512 MB
// Time Limit: 3000 ms

#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define dbg(x) cout << #x << " = " << (x) << "\n";
#define popcount(x) __builtin_popcountll((x))
#define all(v) ((v).begin()), ((v).end())
#define pb emplace_back
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<int, int> pll;

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

const int N = 2e5 + 7;
int n;
int H[N], T[N], pos[N], dp[N][21];

void solve() {
  cin >> n;
  vector<pll> v;
  for (int i = 1; i <= n; i++) {
    cin >> H[i];
    v.pb(H[i], i);
  }
  for (int i = 1; i <= n; i++) {
    cin >> T[i];
  }
  sort(all(v));
  int ma = 0;
  for (int i = 0; i < n; i++) {
    pos[v[i].y] = i;
    ma = max(T[v[i].y], ma);
    dp[i][0] =
        lower_bound(v.begin(), v.end(), make_pair(ma + 1, 0)) - v.begin() - 1;
    dp[i][0] = max(i, dp[i][0]);
  }
  for (int j = 1; j <= 20; j++) {
    for (int i = 0; i < n; i++) {
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    if (T[x] >= H[y]) {
      cout << "1\n";
    } else {
      int to = lower_bound(all(v), make_pair(T[x] + 1, 0)) - v.begin() - 1;
      if (to == -1) {
        cout << "-1\n";
        continue;
      }
      if (dp[to][20] < pos[y]) {
        cout << "-1\n";
        continue;
      }
      int ans = 2;
      for (int j = 20; j >= 0; j--) {
        if (dp[to][j] < pos[y]) ans += 1 << j, to = dp[to][j];
      }
      cout << ans << "\n";
    }
  }
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
0