This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
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... |