#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
using namespace std;
int tt = 1, n;
int a[200005], lf[200005], rg[200005];
int dp[200005];
vector<int> g1[200005], g2[200005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> lf[i] >> rg[i];
up(1, n, i, lf[i], rg[i], 1);
}
cin >> tt;
while(tt--){
int x;
cin >> x;
bool ok = 0;
set<pair<int, int>> st1, st2;
for(int i = 1; i <= n; i++){
dp[i] = 1e9;
g1[i].clear();
g2[i].clear();
}
dp[x] = 0;
int l = x - 1, r = x + 1, pos = x;
st1.insert({dp[x], x});
st2.insert({dp[x], x});
g1[lf[x]].push_back(x);
g2[rg[x]].push_back(x);
int mn1 = l, mx1 = r;
if(lf[x] == x) st1.erase({dp[x], x});
if(rg[x] == x) st2.erase({dp[x], x});
g1[x].clear();
g2[x].clear();
while(l >= 1 || r <= n){
bool f1 = 0, f2 = 0;
if(l > 0 && !st1.empty()){
dp[l] = st1.begin()->first + 1;
f1 = 1;
}
if(r <= n && !st2.empty()){
dp[r] = st2.begin()->first + 1;
f2 = 1;
}
// cout << l << " " << r << "\n";
// cout << f1 << " " << f2 << "\n";
if(!f1 && !f2){
ok = 1;
break;
}
if(f1 == 1 && l > 0 && rg[l] >= r && r <= n) dp[r] = min(dp[r], dp[l] + 1);
if(f2 == 1 && r <= n && lf[r] <= l && l > 0) dp[l] = min(dp[l], dp[r] + 1);
if(f1 == 1){
if(lf[l] != l){
g1[lf[l]].push_back(l);
st1.insert({dp[l], l});
}
if(r <= n && rg[l] >= r){
st2.insert({dp[l], l});
g2[rg[l]].push_back(l);
}
}
if(f2 == 1 && r <= n){
if(rg[r] != r){
g2[rg[r]].push_back(r);
st2.insert({dp[r], r});
}
if(l > 0 && lf[r] <= l){
st1.insert({dp[r], r});
g1[lf[r]].push_back(r);
}
}
if(f1 == 1){
for(int to : g1[l])
st1.erase({dp[to], to});
g1[l].clear();
}
if(f2 == 1){
for(int to : g2[r])
st2.erase({dp[to], to});
g2[r].clear();
}
// cout << dp[l] << " " << dp[r] << "\n" << "\n";
if(f1 == 1) l--;
if(f2 == 1) r++;
}
if(ok){
cout << -1 << "\n";
continue;
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
cout << ans << "\n";
}
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:41:9: error: 'up' was not declared in this scope; did you mean 'dp'?
41 | up(1, n, i, lf[i], rg[i], 1);
| ^~
| dp
passport.cpp:55:35: warning: unused variable 'pos' [-Wunused-variable]
55 | int l = x - 1, r = x + 1, pos = x;
| ^~~
passport.cpp:60:13: warning: unused variable 'mn1' [-Wunused-variable]
60 | int mn1 = l, mx1 = r;
| ^~~
passport.cpp:60:22: warning: unused variable 'mx1' [-Wunused-variable]
60 | int mn1 = l, mx1 = r;
| ^~~