結果

問題 No.2242 Cities and Teleporters
ユーザー roarisroaris
提出日時 2023-03-10 22:53:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,396 ms / 3,000 ms
コード長 1,512 bytes
コンパイル時間 3,098 ms
コンパイル使用メモリ 213,304 KB
実行使用メモリ 40,656 KB
最終ジャッジ日時 2023-10-18 08:32:58
合計ジャッジ時間 17,322 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 819 ms
40,656 KB
testcase_06 AC 796 ms
40,656 KB
testcase_07 AC 818 ms
40,656 KB
testcase_08 AC 1,396 ms
40,656 KB
testcase_09 AC 717 ms
40,656 KB
testcase_10 AC 271 ms
40,656 KB
testcase_11 AC 302 ms
40,656 KB
testcase_12 AC 293 ms
40,656 KB
testcase_13 AC 411 ms
40,656 KB
testcase_14 AC 722 ms
40,656 KB
testcase_15 AC 427 ms
40,656 KB
testcase_16 AC 545 ms
40,656 KB
testcase_17 AC 1,082 ms
40,656 KB
testcase_18 AC 361 ms
40,368 KB
testcase_19 AC 426 ms
40,164 KB
testcase_20 AC 380 ms
39,068 KB
testcase_21 AC 318 ms
39,340 KB
testcase_22 AC 372 ms
39,068 KB
testcase_23 AC 374 ms
40,656 KB
testcase_24 AC 351 ms
40,656 KB
testcase_25 AC 467 ms
40,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
typedef long long ll;

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    int N; cin >> N;
    vector<int> H, T;
    rep(i, N) {
        int Hi; cin >> Hi;
        H.push_back(Hi);
    }
    rep(i, N) {
        int Ti; cin >> Ti;
        T.push_back(Ti);
    }
    vector<vector<int>> HTI;
    rep(i, N) HTI.push_back({H[i], T[i], i});
    sort(HTI.begin(), HTI.end());
    int idx[N];
    rep(i, N) {
        H[i] = HTI[i][0];
        T[i] = HTI[i][1];
        idx[HTI[i][2]] = i;
    }
    
    int to[N+1];
    to[0] = 0;
    rep(v, N) to[v+1] = upper_bound(H.begin(), H.end(), T[v])-H.begin();
    
    int dp[30][N+1];
    dp[0][0] = 0;
    for (int v=1; v<=N; v++) dp[0][v] = max(dp[0][v-1], to[v]);
    for (int i=1; i<30; i++) rep(v, N+1) dp[i][v] = dp[i-1][dp[i-1][v]];
    
    int Q; cin >> Q;
    while (Q--) {
        int A, B; cin >> A >> B;
        A = idx[A-1]+1;
        B = idx[B-1]+1;
        int v = to[A];
        int ans;
        
        if (B<=v) ans = 1;
        else {
            int ok = N+10;
            int ng = -1;
            while (abs(ok-ng)>1) {
                int mid = (ok+ng)/2;
                int now = v;
                rep(i, 20) if ((mid>>i)&1) now = dp[i][now];
                if (B<=now) ok = mid;
                else ng = mid;
            }
            if (ok>N) ans = -1;
            else ans = ok+1;
        }
        
        printf("%d\n", ans);
    }
}
0