#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define x first
#define y second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 3e3 + 7, Inf = 1e9 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
struct Segment_tree_iterative_max
{
ll n;
vector<pair<ll,ll>> st;
pair<ll,ll> merge(pair<ll,ll> a, pair<ll,ll> b)
{
if(a.first==b.first)
{
a.second=min(a.second,b.second);
return a;
}
a=max(a,b);
return a;
}
void update(ll a, pair<ll,ll> b)
{
a+=n;
st[a].x=b.x;
st[a].y=b.y;
for(a/=2;a>0;a/=2)
st[a]=merge(st[a*2],st[a*2+1]);
}
pair<ll,ll> query(ll a, ll b){
pair<ll,ll> s = {0,1e9};
a+=n;b+=n;
while(a<=b){
if(a%2==1) s=merge(s,st[a++]);
if(b%2==0) s=merge(s,st[b--]);
a/=2;b/=2;
}
return s;
}
pair<ll,ll> query(ll a) { return query(a, a); }
Segment_tree_iterative_max(ll N){
n = N * 2;
st.assign(n*2,{0,1e9});
}
}maximo(2e5+5);
struct Segment_tree_iterative_min{
ll n;
vector<pair<ll,ll>> st;
pair<ll,ll> merge(pair<ll,ll> a, pair<ll,ll> b)
{
if(a.first==b.first)
{
a.second=max(a.second,b.second);
return a;
}
a=min(a,b);
return a;
}
void update(ll a, pair<ll,ll> b)
{
a+=n;
st[a].x=b.x;
st[a].y=b.y;
for(a/=2;a>0;a/=2)
st[a]=merge(st[a*2],st[a*2+1]);
}
pair<ll,ll> query(ll a, ll b){
pair<ll,ll> s = {1e9,0};
a+=n;b+=n;
while(a<=b){
if(a%2==1) s=merge(s,st[a++]);
if(b%2==0) s=merge(s,st[b--]);
a/=2;b/=2;
}
return s;
}
pair<ll,ll> query(ll a) { return query(a, a); }
Segment_tree_iterative_min(ll N){
n = N * 2;
st.assign(n*2,{1e9,0});
}
}minimo(2e5+5);
map <int, map <int, int>> mp; int n;
int dp(int l, int r)
{
if(l == 1 && r == n) return 0;
if(mp[l][r] != 0) return mp[l][r];
mp[l][r] = Inf;
pair<int, int> a = maximo.query(l, r); a.sd = min(l, a.sd);
pair<int, int> b = minimo.query(l, r); b.sd = max(r, b.sd);
return mp[l][r] = min(dp(a.sd, a.fs), dp(b.fs, b.sd)) + 1;
}
void solve()
{
cin >> n; vector <pair<int, int>> v(n);
for(int i = 0; i < n; i++)
{
cin >> v[i].fs >> v[i].sd;
maximo.update(i+1, { v[i].sd, v[i].fs });
minimo.update(i+1, { v[i].fs, v[i].sd });
}
int q; cin >> q;
while(q--)
{
int x, ans; cin >> x; x--;
ans = dp(v[x].fs, v[x].sd);
cout << (ans >= Inf ? -1 : ans + 1) << endl;
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
50520 KB |
Output is correct |
2 |
Correct |
8 ms |
50568 KB |
Output is correct |
3 |
Correct |
9 ms |
50524 KB |
Output is correct |
4 |
Correct |
118 ms |
53852 KB |
Output is correct |
5 |
Correct |
138 ms |
59548 KB |
Output is correct |
6 |
Correct |
329 ms |
132164 KB |
Output is correct |
7 |
Correct |
135 ms |
53604 KB |
Output is correct |
8 |
Correct |
106 ms |
52828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50480 KB |
Output is correct |
2 |
Correct |
8 ms |
50524 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50536 KB |
Output is correct |
5 |
Correct |
7 ms |
50776 KB |
Output is correct |
6 |
Correct |
8 ms |
50528 KB |
Output is correct |
7 |
Correct |
8 ms |
50320 KB |
Output is correct |
8 |
Correct |
8 ms |
50524 KB |
Output is correct |
9 |
Correct |
7 ms |
50536 KB |
Output is correct |
10 |
Correct |
8 ms |
50408 KB |
Output is correct |
11 |
Correct |
8 ms |
50520 KB |
Output is correct |
12 |
Correct |
8 ms |
50620 KB |
Output is correct |
13 |
Correct |
8 ms |
50776 KB |
Output is correct |
14 |
Correct |
8 ms |
50524 KB |
Output is correct |
15 |
Correct |
11 ms |
51032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50480 KB |
Output is correct |
2 |
Correct |
8 ms |
50524 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50536 KB |
Output is correct |
5 |
Correct |
7 ms |
50776 KB |
Output is correct |
6 |
Correct |
8 ms |
50528 KB |
Output is correct |
7 |
Correct |
8 ms |
50320 KB |
Output is correct |
8 |
Correct |
8 ms |
50524 KB |
Output is correct |
9 |
Correct |
7 ms |
50536 KB |
Output is correct |
10 |
Correct |
8 ms |
50408 KB |
Output is correct |
11 |
Correct |
8 ms |
50520 KB |
Output is correct |
12 |
Correct |
8 ms |
50620 KB |
Output is correct |
13 |
Correct |
8 ms |
50776 KB |
Output is correct |
14 |
Correct |
8 ms |
50524 KB |
Output is correct |
15 |
Correct |
11 ms |
51032 KB |
Output is correct |
16 |
Correct |
8 ms |
50524 KB |
Output is correct |
17 |
Incorrect |
19 ms |
51896 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50480 KB |
Output is correct |
2 |
Correct |
8 ms |
50524 KB |
Output is correct |
3 |
Correct |
8 ms |
50524 KB |
Output is correct |
4 |
Correct |
8 ms |
50536 KB |
Output is correct |
5 |
Correct |
7 ms |
50776 KB |
Output is correct |
6 |
Correct |
8 ms |
50528 KB |
Output is correct |
7 |
Correct |
8 ms |
50320 KB |
Output is correct |
8 |
Correct |
8 ms |
50524 KB |
Output is correct |
9 |
Correct |
7 ms |
50536 KB |
Output is correct |
10 |
Correct |
8 ms |
50408 KB |
Output is correct |
11 |
Correct |
8 ms |
50520 KB |
Output is correct |
12 |
Correct |
8 ms |
50620 KB |
Output is correct |
13 |
Correct |
8 ms |
50776 KB |
Output is correct |
14 |
Correct |
8 ms |
50524 KB |
Output is correct |
15 |
Correct |
11 ms |
51032 KB |
Output is correct |
16 |
Correct |
8 ms |
50524 KB |
Output is correct |
17 |
Incorrect |
19 ms |
51896 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
50520 KB |
Output is correct |
2 |
Correct |
8 ms |
50568 KB |
Output is correct |
3 |
Correct |
9 ms |
50524 KB |
Output is correct |
4 |
Correct |
118 ms |
53852 KB |
Output is correct |
5 |
Correct |
138 ms |
59548 KB |
Output is correct |
6 |
Correct |
329 ms |
132164 KB |
Output is correct |
7 |
Correct |
135 ms |
53604 KB |
Output is correct |
8 |
Correct |
106 ms |
52828 KB |
Output is correct |
9 |
Correct |
8 ms |
50480 KB |
Output is correct |
10 |
Correct |
8 ms |
50524 KB |
Output is correct |
11 |
Correct |
8 ms |
50524 KB |
Output is correct |
12 |
Correct |
8 ms |
50536 KB |
Output is correct |
13 |
Correct |
7 ms |
50776 KB |
Output is correct |
14 |
Correct |
8 ms |
50528 KB |
Output is correct |
15 |
Correct |
8 ms |
50320 KB |
Output is correct |
16 |
Correct |
8 ms |
50524 KB |
Output is correct |
17 |
Correct |
7 ms |
50536 KB |
Output is correct |
18 |
Correct |
8 ms |
50408 KB |
Output is correct |
19 |
Correct |
8 ms |
50520 KB |
Output is correct |
20 |
Correct |
8 ms |
50620 KB |
Output is correct |
21 |
Correct |
8 ms |
50776 KB |
Output is correct |
22 |
Correct |
8 ms |
50524 KB |
Output is correct |
23 |
Correct |
11 ms |
51032 KB |
Output is correct |
24 |
Correct |
8 ms |
50524 KB |
Output is correct |
25 |
Incorrect |
19 ms |
51896 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |