#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
//code
const int maxn = 2e5 + 8;
int l[maxn] , r[maxn] , x[maxn];
int n , q;
namespace subtask4
{
const int maxn = 2505;
vector <iii> adj[maxn][maxn];
int dist[maxn][maxn];
void Build()
{
for(int left = 1; left <= n; left++)
{
int Mi = inf , Mx = -inf;
for(int right = left; right <= n; right++)
{
Mi = min(Mi , l[right]);
Mx = max(Mx , r[right]);
if(left < right)
{
adj[left + 1][right].push_back({0 , {left , right}});
adj[left][right - 1].push_back({0 , {left , right}});
adj[Mi][right].push_back({1 , {left , right}});
adj[left][Mx].push_back({1 , {left , right}});
}
else
{
adj[Mi][Mx].push_back({1 , {left , right}});
}
}
}
}
void solve()
{
Build();
ms(dist , 0x3f);
deque <ii> dq;
dq.push_back({1 , n});
dist[1][n] = 0;
while(!dq.empty())
{
int u = dq.front().fi;
int v = dq.front().se;
dq.pop_front();
for(iii it : adj[u][v])
{
int x = it.se.fi;
int y = it.se.se;
int w = it.fi;
if(minimize(dist[x][y] , dist[u][v] + w))
{
if(w)
{
dq.push_back({x , y});
}
else dq.push_front({x , y});
}
}
}
for(int i = 1; i <= q; i++)
{
if(dist[x[i]][x[i]] == inf)
{
cout << -1 << "\n";
}
else cout << dist[x[i]][x[i]] << "\n";
}
}
}
void solve(void)
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
}
cin >> q;
for(int i = 1; i <= q; i++)
{
cin >> x[i];
}
subtask4 :: solve();
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
}
컴파일 시 표준 에러 (stderr) 메시지
passport.cpp: In function 'int main()':
passport.cpp:162:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
passport.cpp:163:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |