#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cout.setf(ios::fixed);cout.precision(0);
typedef long long ll;
#define x first
#define y second
const ll MOD=1e9+7,INF=1e18;
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);
ll n;
map<pair<ll,ll>,ll> mapa;
ll sol(ll i, ll j)
{
if(i==1&&j==n)
return 0;
if(mapa[{i,j}]!=0) return mapa[{i,j}];
ll a1=1e9,a2=1e9;
//cout<<i<<j<<maximo.query(i,j).first<<maximo.query(i,j).second<<"\n";
if(minimo.query(i,j).first!=i)
a1=1+sol(minimo.query(i,j).first,max(j,minimo.query(i,j).second));
if(maximo.query(i,j).first!=j)
a2=1+sol(min(i,maximo.query(i,j).second),maximo.query(i,j).first);
return mapa[{i,j}]=min(a1,a2);
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
ll t1,t2;
cin>>t1>>t2;
minimo.update(i,{t1,t2});
maximo.update(i,{t2,t1});
}
ll q;
cin>>q;
while(q--)
{
ll i;
cin>>i;
if(sol(i,i)>n+1)
cout<<"-1\n";
else
cout<<sol(i,i)<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25432 KB |
Output is correct |
2 |
Correct |
6 ms |
25436 KB |
Output is correct |
3 |
Correct |
4 ms |
25436 KB |
Output is correct |
4 |
Correct |
137 ms |
28044 KB |
Output is correct |
5 |
Correct |
148 ms |
30292 KB |
Output is correct |
6 |
Correct |
258 ms |
59216 KB |
Output is correct |
7 |
Correct |
138 ms |
27476 KB |
Output is correct |
8 |
Correct |
112 ms |
26964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25432 KB |
Output is correct |
2 |
Correct |
5 ms |
25432 KB |
Output is correct |
3 |
Correct |
5 ms |
25436 KB |
Output is correct |
4 |
Correct |
5 ms |
25436 KB |
Output is correct |
5 |
Correct |
5 ms |
25436 KB |
Output is correct |
6 |
Correct |
5 ms |
25328 KB |
Output is correct |
7 |
Correct |
5 ms |
25436 KB |
Output is correct |
8 |
Correct |
5 ms |
25340 KB |
Output is correct |
9 |
Correct |
5 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25436 KB |
Output is correct |
11 |
Correct |
5 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25436 KB |
Output is correct |
13 |
Correct |
5 ms |
25436 KB |
Output is correct |
14 |
Correct |
5 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25432 KB |
Output is correct |
2 |
Correct |
5 ms |
25432 KB |
Output is correct |
3 |
Correct |
5 ms |
25436 KB |
Output is correct |
4 |
Correct |
5 ms |
25436 KB |
Output is correct |
5 |
Correct |
5 ms |
25436 KB |
Output is correct |
6 |
Correct |
5 ms |
25328 KB |
Output is correct |
7 |
Correct |
5 ms |
25436 KB |
Output is correct |
8 |
Correct |
5 ms |
25340 KB |
Output is correct |
9 |
Correct |
5 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25436 KB |
Output is correct |
11 |
Correct |
5 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25436 KB |
Output is correct |
13 |
Correct |
5 ms |
25436 KB |
Output is correct |
14 |
Correct |
5 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25692 KB |
Output is correct |
16 |
Correct |
6 ms |
25432 KB |
Output is correct |
17 |
Incorrect |
21 ms |
26716 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25432 KB |
Output is correct |
2 |
Correct |
5 ms |
25432 KB |
Output is correct |
3 |
Correct |
5 ms |
25436 KB |
Output is correct |
4 |
Correct |
5 ms |
25436 KB |
Output is correct |
5 |
Correct |
5 ms |
25436 KB |
Output is correct |
6 |
Correct |
5 ms |
25328 KB |
Output is correct |
7 |
Correct |
5 ms |
25436 KB |
Output is correct |
8 |
Correct |
5 ms |
25340 KB |
Output is correct |
9 |
Correct |
5 ms |
25436 KB |
Output is correct |
10 |
Correct |
5 ms |
25436 KB |
Output is correct |
11 |
Correct |
5 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25436 KB |
Output is correct |
13 |
Correct |
5 ms |
25436 KB |
Output is correct |
14 |
Correct |
5 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25692 KB |
Output is correct |
16 |
Correct |
6 ms |
25432 KB |
Output is correct |
17 |
Incorrect |
21 ms |
26716 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25432 KB |
Output is correct |
2 |
Correct |
6 ms |
25436 KB |
Output is correct |
3 |
Correct |
4 ms |
25436 KB |
Output is correct |
4 |
Correct |
137 ms |
28044 KB |
Output is correct |
5 |
Correct |
148 ms |
30292 KB |
Output is correct |
6 |
Correct |
258 ms |
59216 KB |
Output is correct |
7 |
Correct |
138 ms |
27476 KB |
Output is correct |
8 |
Correct |
112 ms |
26964 KB |
Output is correct |
9 |
Correct |
5 ms |
25432 KB |
Output is correct |
10 |
Correct |
5 ms |
25432 KB |
Output is correct |
11 |
Correct |
5 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25436 KB |
Output is correct |
13 |
Correct |
5 ms |
25436 KB |
Output is correct |
14 |
Correct |
5 ms |
25328 KB |
Output is correct |
15 |
Correct |
5 ms |
25436 KB |
Output is correct |
16 |
Correct |
5 ms |
25340 KB |
Output is correct |
17 |
Correct |
5 ms |
25436 KB |
Output is correct |
18 |
Correct |
5 ms |
25436 KB |
Output is correct |
19 |
Correct |
5 ms |
25436 KB |
Output is correct |
20 |
Correct |
5 ms |
25436 KB |
Output is correct |
21 |
Correct |
5 ms |
25436 KB |
Output is correct |
22 |
Correct |
5 ms |
25436 KB |
Output is correct |
23 |
Correct |
8 ms |
25692 KB |
Output is correct |
24 |
Correct |
6 ms |
25432 KB |
Output is correct |
25 |
Incorrect |
21 ms |
26716 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |