結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 emthrmemthrm
提出日時 2023-03-11 02:58:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,792 bytes
コンパイル時間 4,367 ms
コンパイル使用メモリ 270,856 KB
実行使用メモリ 22,836 KB
最終ジャッジ日時 2023-10-18 09:38:16
合計ジャッジ時間 18,973 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 710 ms
22,820 KB
testcase_06 AC 647 ms
22,820 KB
testcase_07 AC 665 ms
22,820 KB
testcase_08 AC 1,097 ms
22,820 KB
testcase_09 AC 651 ms
22,820 KB
testcase_10 AC 340 ms
22,820 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 470 ms
22,448 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 428 ms
21,832 KB
testcase_23 AC 466 ms
22,820 KB
testcase_24 AC 432 ms
22,820 KB
testcase_25 AC 446 ms
22,820 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));
  REP(i, n) {
    if (const auto it = ranges::upper_bound(h, t[i]); it != h.begin()) {
      dp[0][i] = distance(h.begin(), prev(it));
    }
  }
  REP(bit, B - 1) {
    vector<int> rmax = dp[bit];
    FOR(i, 1, n) chmax(rmax[i], rmax[i - 1]);
    REP(i, n) {
      if (dp[bit][i] != -1) chmax(dp[bit + 1][i], rmax[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;
    }
    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) << '\n';
  }
  return 0;
}
0