結果
問題 |
No.2242 Cities and Teleporters
|
ユーザー |
|
提出日時 | 2023-03-11 00:28:09 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,633 bytes |
コンパイル時間 | 2,577 ms |
コンパイル使用メモリ | 210,200 KB |
最終ジャッジ日時 | 2025-02-11 09:27:16 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 WA * 10 |
ソースコード
#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>> cvv; { int nw=0; while(nw<n){ vector<int> cnd2; while(nw!=cnd.at(nw+1)){ cnd2.push_back(nw); nw=cnd.at(nw+1); } cnd2.push_back(nw); cvv.push_back(cnd2); for(;nw<n;nw++) if(nw<cnd.at(nw+1)) break; } } vector<int> rcv; rep(i,cvv.size()){ rcv.push_back(cvv.at(i).at(0)); } 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<nb){ int nna=upper_bound(rcv.begin(),rcv.end(),na)-rcv.begin(); int nnb=upper_bound(rcv.begin(),rcv.end(),nb)-rcv.begin(); if(nna!=nnb){ ans=-1; }else{ int v=nna-1; if(cvv.at(v).back()<nb) ans=-1; else{ ans+=upper_bound(cvv.at(v).begin(),cvv.at(v).end(),nb)-upper_bound(cvv.at(v).begin(),cvv.at(v).end(),na); } } } cout<<ans<<'\n'; } }