結果

問題 No.2242 Cities and Teleporters
ユーザー cho435cho435
提出日時 2023-03-11 01:41:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 666 ms / 3,000 ms
コード長 1,287 bytes
コンパイル時間 4,261 ms
コンパイル使用メモリ 214,172 KB
実行使用メモリ 40,912 KB
最終ジャッジ日時 2023-10-18 09:32:45
合計ジャッジ時間 16,516 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 476 ms
40,912 KB
testcase_06 AC 438 ms
40,912 KB
testcase_07 AC 490 ms
40,912 KB
testcase_08 AC 662 ms
40,912 KB
testcase_09 AC 481 ms
40,912 KB
testcase_10 AC 231 ms
40,912 KB
testcase_11 AC 395 ms
40,912 KB
testcase_12 AC 379 ms
40,912 KB
testcase_13 AC 398 ms
40,912 KB
testcase_14 AC 444 ms
40,912 KB
testcase_15 AC 391 ms
40,912 KB
testcase_16 AC 459 ms
40,912 KB
testcase_17 AC 613 ms
40,912 KB
testcase_18 AC 427 ms
40,384 KB
testcase_19 AC 632 ms
40,120 KB
testcase_20 AC 382 ms
39,064 KB
testcase_21 AC 387 ms
39,328 KB
testcase_22 AC 636 ms
39,064 KB
testcase_23 AC 666 ms
40,912 KB
testcase_24 AC 664 ms
40,912 KB
testcase_25 AC 652 ms
40,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin>>n;
  vector<vector<int>> ht(n,vector<int>(3));
  rep(i,n) cin>>ht.at(i).at(0);
  rep(i,n) cin>>ht.at(i).at(1);
  rep(i,n) ht.at(i).at(2)=i;
  sort(ht.begin(),ht.end());
  vector<int> p(n);
  rep(i,n) p.at(ht.at(i).at(2))=i;
  vector<int> cnd(n+1,-1);
  vector<int> cph(n);
  rep(i,n) cph.at(i)=ht.at(i).at(0);
  rep(i,n){
    auto it=upper_bound(cph.begin(),cph.end(),ht.at(i).at(1));
    cnd.at(i+1)=max(cnd.at(i),int(it-cph.begin()-1));
  }
  vector<vector<int>> ddv(30,vector<int>(n,0));
  rep(i,n) ddv.at(0).at(i)=max(cnd.at(i+1),i);
  rep(i,29){
    rep(j,n){
      ddv.at(i+1).at(j)=ddv.at(i).at(ddv.at(i).at(j));
    }
  }
  int q;
  cin>>q;
  rep(Qi,q){
    int a,b;
    cin>>a>>b;
    a--; b--;
    int na=p.at(a),nb=p.at(b);
    int ans=1;
    na=upper_bound(cph.begin(),cph.end(),ht.at(na).at(1))-cph.begin()-1;
    if(na==-1) ans=-1;
    else if(na<nb){
      for(int i=29;i>=0;i--){
        if(ddv.at(i).at(na)<nb){
          na=ddv.at(i).at(na);
          ans+=1<<i;
        }
      }
      na=ddv.at(0).at(na);
      ans++;
      if(na<nb) ans=-1;
    }
    cout<<ans<<'\n';
  }
}
0