結果

問題 No.2242 Cities and Teleporters
ユーザー KudeKude
提出日時 2023-03-10 22:25:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 576 ms / 3,000 ms
コード長 1,976 bytes
コンパイル時間 2,802 ms
コンパイル使用メモリ 233,172 KB
実行使用メモリ 21,136 KB
最終ジャッジ日時 2023-10-18 08:07:25
合計ジャッジ時間 13,807 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 432 ms
21,136 KB
testcase_06 AC 337 ms
21,136 KB
testcase_07 AC 416 ms
21,136 KB
testcase_08 AC 576 ms
21,136 KB
testcase_09 AC 559 ms
21,136 KB
testcase_10 AC 195 ms
21,136 KB
testcase_11 AC 274 ms
21,136 KB
testcase_12 AC 277 ms
21,136 KB
testcase_13 AC 301 ms
21,136 KB
testcase_14 AC 367 ms
21,136 KB
testcase_15 AC 307 ms
21,136 KB
testcase_16 AC 318 ms
21,136 KB
testcase_17 AC 453 ms
21,136 KB
testcase_18 AC 316 ms
20,872 KB
testcase_19 AC 396 ms
20,872 KB
testcase_20 AC 288 ms
20,344 KB
testcase_21 AC 277 ms
20,608 KB
testcase_22 AC 375 ms
20,344 KB
testcase_23 AC 398 ms
21,136 KB
testcase_24 AC 394 ms
21,136 KB
testcase_25 AC 393 ms
21,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VI h(n), t(n);
  rep(i, n) cin >> h[i];
  rep(i, n) cin >> t[i];
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return h[i] < h[j];
  });
  VI rank(n);
  rep(i, n) rank[ord[i]] = i;
  VI to(n, -1);
  VI to_d[18];
  rep(u, n) {
    auto it = upper_bound(all(ord), t[u], [&](int x, int i) {
      return x < h[i];
    });
    if (it != ord.begin()) to[u] = *prev(it);
  }
  to_d[0].resize(n);
  {
    int now = ord[0];
    rep(i, n) {
      int u = ord[i];
      if (rank[u] > rank[now]) now = u;
      if (to[u] != -1 && rank[to[u]] > rank[now]) now = to[u];
      to_d[0][u] = now;
    }
  }
  rep(i, 17) {
    to_d[i+1].resize(n, -1);
    rep(v, n) to_d[i+1][v] = to_d[i][to_d[i][v]];
  }
  int q;
  cin >> q;
  while(q--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (to[a] == -1) {
      cout << -1 << '\n';
      continue;
    }
    int ans = 1;
    a = to[a];
    rrep(k, 18) {
      int v = to_d[k][a];
      if (rank[v] < rank[b]) ans += 1 << k, a = v;
    }
    if (rank[a] < rank[b]) ans++, a = to_d[0][a];
    if (rank[a] < rank[b]) ans = -1;
    cout << ans << '\n';
  }
}
0