#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
int base = 1<<17;
int inf = 1e9;
struct TreeMax {
vector<pair<int, int>> Tree;
TreeMax() : Tree(2*base, {-inf, -inf}) {}
void upd(int x, pair<int,int> val) {
x+=base;
Tree[x] = {val.second, -val.first};
x/=2;
while(x>0) {
Tree[x] = max(Tree[x+x], Tree[x+x+1]);
x/=2;
}
}
pair<int, int> query(int a, int b) {
if(a==b) return {-Tree[a+base].second, Tree[a+base].first};
a+=base-1;
b+=base+1;
pair<int,int> ans = {-inf,-inf};
while(a/2 != b/2) {
if(a%2==0) ans = max(ans, Tree[a+1]);
if(b%2==1) ans = max(ans, Tree[b-1]);
a/=2; b/=2;
}
return {-ans.second, ans.first};
}
};
struct TreeMin {
vector<pair<int, int>> Tree;
TreeMin() : Tree(2*base, {inf, inf}) {}
void upd(int x, pair<int,int> val) {
x+=base;
Tree[x] = {val.first, -val.second};
x/=2;
while(x>0) {
Tree[x] = min(Tree[x+x], Tree[x+x+1]);
x/=2;
}
}
pair<int, int> query(int a, int b) {
if(a==b) return {Tree[a+base].first, -Tree[a+base].second};
a+=base-1;
b+=base+1;
pair<int,int> ans = {inf,inf};
while(a/2 != b/2) {
if(a%2==0) ans = min(ans, Tree[a+1]);
if(b%2==1) ans = min(ans, Tree[b-1]);
a/=2; b/=2;
}
return {ans.first, -ans.second};
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> l(n), r(n);
for(int i=0; i<n; ++i) cin >> l[i] >> r[i];
TreeMin left;
TreeMax right;
for(int i=0; i<n; ++i) {
l[i]--; r[i]--;
left.upd(i, {l[i], r[i]});
right.upd(i, {l[i], r[i]});
}
vector<vector<vector<pair<int, int>>>> g(n+1, vector<vector<pair<int, int>>>(n+1));
for(int i=0; i<n; ++i) {
for(int j=i; j<n; ++j) {
pair<int, int> L = left.query(i,j);
L.first = min(L.first, i);
L.second = max(L.second, j);
pair<int, int> R = right.query(i,j);
R.first = min(R.first, i);
R.second = max(R.second, j);
g[L.first][L.second].push_back({i,j});
g[R.first][R.second].push_back({i,j});
// cout << "(" << i << " " << j << ") -> (" << L.first << " " << L.second << ")\n";
// cout << "(" << i << " " << j << ") -> (" << R.first << " " << R.second << ")\n";
}
}
vector<vector<int>> dist(n+1, vector<int>(n+1, inf));
dist[0][n-1] = 0;
queue<pair<int, int>> Q;
Q.push({0,n-1});
while(Q.size()) {
auto[a,b] = Q.front(); Q.pop();
for(auto[c,d] : g[a][b]) {
if(dist[c][d]==inf) {
dist[c][d] = dist[a][b] + 1;
Q.push({c,d});
}
}
}
int q; cin >> q;
while(q--) {
int x; cin >> x; --x;
if(dist[x][x] == inf) cout << "-1\n";
else cout << dist[x][x] << "\n";
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |