結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 emthrmemthrm
提出日時 2023-03-11 03:25:32
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,167 ms / 3,000 ms
コード長 3,066 bytes
コンパイル時間 4,119 ms
コンパイル使用メモリ 271,204 KB
実行使用メモリ 22,836 KB
最終ジャッジ日時 2023-10-18 09:39:49
合計ジャッジ時間 18,855 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 750 ms
22,836 KB
testcase_06 AC 700 ms
22,836 KB
testcase_07 AC 748 ms
22,836 KB
testcase_08 AC 1,167 ms
22,836 KB
testcase_09 AC 748 ms
22,836 KB
testcase_10 AC 364 ms
22,836 KB
testcase_11 AC 355 ms
22,836 KB
testcase_12 AC 354 ms
22,836 KB
testcase_13 AC 443 ms
22,836 KB
testcase_14 AC 681 ms
22,836 KB
testcase_15 AC 441 ms
22,836 KB
testcase_16 AC 593 ms
22,836 KB
testcase_17 AC 1,018 ms
22,836 KB
testcase_18 AC 429 ms
22,496 KB
testcase_19 AC 482 ms
22,464 KB
testcase_20 AC 419 ms
21,864 KB
testcase_21 AC 357 ms
22,176 KB
testcase_22 AC 455 ms
21,848 KB
testcase_23 AC 438 ms
22,836 KB
testcase_24 AC 435 ms
22,836 KB
testcase_25 AC 438 ms
22,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  constexpr int B = 18;
  int n; cin >> n;
  vector<int> h(n), t(n);
  REP(i, n) cin >> h[i];
  REP(i, n) cin >> t[i];
  vector<int> v(n * 2);
  ranges::copy(h, v.begin());
  ranges::copy(t, next(v.begin(), n));
  ranges::sort(v);
  v.erase(unique(ALL(v)), v.end());
  REP(i, n) h[i] = distance(v.begin(), ranges::lower_bound(v, h[i]));
  REP(i, n) t[i] = distance(v.begin(), ranges::lower_bound(v, t[i]));
  // REP(i, n) cout << h[i] << " \n"[i + 1 == n];
  // REP(i, n) cout << t[i] << " \n"[i + 1 == n];
  int q; cin >> q;
  vector<int> a(q), b(q); REP(i, q) cin >> a[i] >> b[i], --a[i], --b[i];
  {
    vector<int> ord(n);
    iota(ALL(ord), 0);
    ranges::sort(ord, {}, [&](const int i) -> int { return h[i]; });
    vector<int> tmp(n);
    REP(i, n) tmp[i] = h[ord[i]];
    h.swap(tmp);
    REP(i, n) tmp[i] = t[ord[i]];
    t.swap(tmp);
    REP(i, n) tmp[ord[i]] = i;
    REP(i, q) a[i] = tmp[a[i]];
    REP(i, q) b[i] = tmp[b[i]];
  }
  vector dp(B, vector(n, -1));
  iota(ALL(dp[0]), 0);
  REP(i, n) {
    if (i > 0) chmax(dp[0][i], dp[0][i - 1]);
    if (const auto it = ranges::upper_bound(h, t[i]); it != h.begin()) {
      chmax(dp[0][i], distance(h.begin(), prev(it)));
    }
  }
  REP(bit, B - 1) {
    iota(ALL(dp[bit + 1]), 0);
    REP(i, n) {
      if (i > 0) chmax(dp[bit + 1][i], dp[bit + 1][i - 1]);
      chmax(dp[bit + 1][i], dp[bit][dp[bit][i]]);
    }
  }
  // REP(i, n) cout << dp[0][i] << " \n"[i + 1 == n];
  // REP(i, n) cout << dp[1][i] << " \n"[i + 1 == n];
  // REP(i, q) cout << a[i] << ' ' << b[i] << '\n';
  REP(i, q) {
    if (h[b[i]] <= t[a[i]]) {
      cout << 1 << '\n';
      continue;
    }
    if (t[a[i]] < h.front()) {
      cout << "-1\n";
      continue;
    }
    if (const auto it = ranges::upper_bound(h, t[a[i]]); it != h.begin()) {
      a[i] = distance(h.begin(), prev(it));
    }
    int lb = 0, ub = n;
    while (ub - lb > 1) {
      const int mid = midpoint(lb, ub);
      int town = a[i];
      for (int j = mid, bit = 0; j > 0 && town != -1; j /= 2, ++bit) {
        if (j % 2 == 1) town = dp[bit][town];
      }
      (b[i] <= town ? ub : lb) = mid;
    }
    cout << (ub == n ? -1 : ub + 1) << '\n';
  }
  return 0;
}
0