結果

問題 No.2242 Cities and Teleporters
ユーザー たたき@競プロ
提出日時 2023-08-08 20:39:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,001 ms / 3,000 ms
コード長 1,392 bytes
コンパイル時間 6,280 ms
コンパイル使用メモリ 317,280 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2024-11-14 00:38:48
合計ジャッジ時間 26,761 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint=modint998244353; //1000000007;
using ll=long long;
using pp=pair<int,int>;
#define sr string 
#define vc vector
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define pque priority_queue
#define bpc(a) __builtin_popcount(a)
int main(){
  int n;cin>>n;
  vc<pair<int,pp>>hh(n); rep(i,n)cin>>hh[i].fi; rep(i,n)cin>>hh[i].se.fi;
  rep(i,n)hh[i].se.se=i; sort(all(hh));
  vc<int>h(n),t(n); rep(i,n)h[i]=hh[i].fi,t[i]=hh[i].se.fi;
  vc<int>r(n); rep(i,n)r[hh[i].se.se]=i;
  vc<int>rr(n); rep(i,n)rr[i]=hh[i].se.se;
  vc<int>v(n);
  rep(i,n){
    int it=upper_bound(all(h),t[i])-h.begin();
    it--;
    v[i]=it;
  }
  vc vx(20,vc<int>(n)); vx[0]=v;
  rep(i,n-1)vx[0][i+1]=max(vx[0][i+1],vx[0][i]);
  rep(i,19)rep(j,n)if(vx[i][j]!=-1)vx[i+1][j]=vx[i][vx[i][j]];
  int q;cin>>q;
  rep(z,q){
    int a,b;cin>>a>>b; a--;b--;
    a=r[a],b=r[b];
    if(v[a]==-1){
      cout<<-1<<"\n";
      continue;
    }
    if(b<=v[a]){
      cout<<1<<"\n";
      continue;
    }
    a=v[a];
    if(b>vx[19][a]){
      cout<<-1<<"\n";
      continue;
    }
    int res=0;
    for(int i=19;i>=0;i--){
      int na=vx[i][a];
      if(na<b){
        res+=1<<i;
        a=na;
      }
    }
    cout<<res+2<<"\n";
  }
}
      
  
	
0