#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("fast-math")
#pragma GCC optimize ("unroll-loops")
using namespace std;
typedef pair<int,int> pii;
int n,L[200005],R[200005],torgt[200005],tolft[200005],sufL[200005],prefR[200005],sol[200005];
pii d1[200005],dn[200005];
unordered_set<int> arb[4*200005];
vector<int> nodes;
void build(int nod,int st,int dr)
{
arb[nod].clear();
if(st==dr)
return;
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
}
void update(int nod,int st,int dr,int a,int b, int val)
{
if(st>=a&&dr<=b)
{
if(val>0)
arb[nod].insert(val);
else
arb[nod].erase(-val);
return;
}
int mij=(st+dr)/2;
if(a<=mij)
update(nod*2,st,mij,a,b,val);
if(b>mij)
update(nod*2+1,mij+1,dr,a,b,val);
}
void query(int nod,int st,int dr,int poz)
{
for(int j:arb[nod])
nodes.push_back(j);
if(st==dr)
return;
int mij=(st+dr)/2;
if(poz<=mij)
query(nod*2,st,mij,poz);
else
query(nod*2+1,mij+1,dr,poz);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>L[i]>>R[i];
for(int i=1;i<=n;i++)
prefR[i]=max(prefR[i-1],R[i]);
sufL[n+1]=1e9;
for(int i=n;i>=1;i--)
sufL[i]=min(sufL[i+1],L[i]);
torgt[n]=0;
for(int i=n-1;i>=1;i--)
{
if(prefR[i]<=i)
torgt[i]=-1;
else
{
int j=prefR[i];
if(torgt[j]==-1)
torgt[i]=-1;
else
torgt[i]=1+torgt[j];
}
}
tolft[1]=0;
for(int i=2;i<=n;i++)
{
if(sufL[i]>=i)
tolft[i]=-1;
else
{
int j=sufL[i];
if(tolft[j]==-1)
tolft[i]=-1;
else
tolft[i]=1+tolft[j];
}
}
for(int i=1;i<=n;i++)
{
d1[i]={-1,0};
dn[i]={-1,0};
}
d1[1]={0,1};
build(1,1,n);
for(int i=2;i<=n;i++)
update(1,1,n,L[i],R[i],i);
set<pair<pii,int>> coada;
coada.insert({{0,1},1});
while(!coada.empty())
{
int val=(*coada.begin()).first.first;
auto it=prev(coada.lower_bound({{val+1,0},0}));
int nod=(*it).second;
coada.erase(it);
nodes.clear();
query(1,1,n,nod);
for(int j:nodes)
{
d1[j].first=d1[nod].first+1;
d1[j].second=max(d1[nod].second,R[j]);
update(1,1,n,L[j],R[j],-j);
coada.insert({d1[j],j});
}
}
dn[n]={0,n};
build(1,1,n);
for(int i=1;i<n;i++)
update(1,1,n,L[i],R[i],i);
coada.insert({{0,n},n});
while(!coada.empty())
{
auto it=coada.begin();
int nod=(*it).second;
coada.erase(it);
nodes.clear();
query(1,1,n,nod);
for(int j:nodes)
{
dn[j].first=dn[nod].first+1;
dn[j].second=min(dn[nod].second,L[j]);
update(1,1,n,L[j],R[j],-j);
coada.insert({dn[j],j});
}
}
for(int i=1;i<=n;i++)
{
sol[i]=1e9;
if(d1[i].first!=-1)
{
int poz=d1[i].second;
if(torgt[poz]!=-1)
sol[i]=min(sol[i],d1[i].first+torgt[poz]);
}
if(dn[i].first!=-1)
{
int poz=dn[i].second;
if(tolft[poz]!=-1)
sol[i]=min(sol[i],dn[i].first+tolft[poz]);
}
if(sol[i]>n)
sol[i]=-1;
}
int q;
cin>>q;
while(q--)
{
int x;
cin>>x;
cout<<sol[x]<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
49752 KB |
Output is correct |
2 |
Correct |
11 ms |
49752 KB |
Output is correct |
3 |
Correct |
12 ms |
49756 KB |
Output is correct |
4 |
Execution timed out |
2064 ms |
189180 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
49756 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
13 ms |
49756 KB |
Output is correct |
4 |
Correct |
14 ms |
49756 KB |
Output is correct |
5 |
Correct |
11 ms |
49756 KB |
Output is correct |
6 |
Correct |
12 ms |
49752 KB |
Output is correct |
7 |
Correct |
14 ms |
49756 KB |
Output is correct |
8 |
Correct |
14 ms |
49752 KB |
Output is correct |
9 |
Correct |
11 ms |
49828 KB |
Output is correct |
10 |
Correct |
14 ms |
49756 KB |
Output is correct |
11 |
Correct |
12 ms |
49756 KB |
Output is correct |
12 |
Correct |
12 ms |
49836 KB |
Output is correct |
13 |
Correct |
12 ms |
50012 KB |
Output is correct |
14 |
Correct |
12 ms |
50012 KB |
Output is correct |
15 |
Correct |
12 ms |
49972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
49756 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
13 ms |
49756 KB |
Output is correct |
4 |
Correct |
14 ms |
49756 KB |
Output is correct |
5 |
Correct |
11 ms |
49756 KB |
Output is correct |
6 |
Correct |
12 ms |
49752 KB |
Output is correct |
7 |
Correct |
14 ms |
49756 KB |
Output is correct |
8 |
Correct |
14 ms |
49752 KB |
Output is correct |
9 |
Correct |
11 ms |
49828 KB |
Output is correct |
10 |
Correct |
14 ms |
49756 KB |
Output is correct |
11 |
Correct |
12 ms |
49756 KB |
Output is correct |
12 |
Correct |
12 ms |
49836 KB |
Output is correct |
13 |
Correct |
12 ms |
50012 KB |
Output is correct |
14 |
Correct |
12 ms |
50012 KB |
Output is correct |
15 |
Correct |
12 ms |
49972 KB |
Output is correct |
16 |
Correct |
20 ms |
50856 KB |
Output is correct |
17 |
Correct |
18 ms |
50520 KB |
Output is correct |
18 |
Correct |
18 ms |
51036 KB |
Output is correct |
19 |
Correct |
18 ms |
51088 KB |
Output is correct |
20 |
Correct |
15 ms |
50268 KB |
Output is correct |
21 |
Correct |
15 ms |
50544 KB |
Output is correct |
22 |
Correct |
12 ms |
50012 KB |
Output is correct |
23 |
Correct |
21 ms |
50780 KB |
Output is correct |
24 |
Correct |
17 ms |
50780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
49756 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
13 ms |
49756 KB |
Output is correct |
4 |
Correct |
14 ms |
49756 KB |
Output is correct |
5 |
Correct |
11 ms |
49756 KB |
Output is correct |
6 |
Correct |
12 ms |
49752 KB |
Output is correct |
7 |
Correct |
14 ms |
49756 KB |
Output is correct |
8 |
Correct |
14 ms |
49752 KB |
Output is correct |
9 |
Correct |
11 ms |
49828 KB |
Output is correct |
10 |
Correct |
14 ms |
49756 KB |
Output is correct |
11 |
Correct |
12 ms |
49756 KB |
Output is correct |
12 |
Correct |
12 ms |
49836 KB |
Output is correct |
13 |
Correct |
12 ms |
50012 KB |
Output is correct |
14 |
Correct |
12 ms |
50012 KB |
Output is correct |
15 |
Correct |
12 ms |
49972 KB |
Output is correct |
16 |
Correct |
20 ms |
50856 KB |
Output is correct |
17 |
Correct |
18 ms |
50520 KB |
Output is correct |
18 |
Correct |
18 ms |
51036 KB |
Output is correct |
19 |
Correct |
18 ms |
51088 KB |
Output is correct |
20 |
Correct |
15 ms |
50268 KB |
Output is correct |
21 |
Correct |
15 ms |
50544 KB |
Output is correct |
22 |
Correct |
12 ms |
50012 KB |
Output is correct |
23 |
Correct |
21 ms |
50780 KB |
Output is correct |
24 |
Correct |
17 ms |
50780 KB |
Output is correct |
25 |
Correct |
11 ms |
49752 KB |
Output is correct |
26 |
Correct |
12 ms |
49756 KB |
Output is correct |
27 |
Correct |
20 ms |
51036 KB |
Output is correct |
28 |
Correct |
16 ms |
50604 KB |
Output is correct |
29 |
Correct |
14 ms |
50520 KB |
Output is correct |
30 |
Correct |
15 ms |
50524 KB |
Output is correct |
31 |
Correct |
20 ms |
50524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
49752 KB |
Output is correct |
2 |
Correct |
11 ms |
49752 KB |
Output is correct |
3 |
Correct |
12 ms |
49756 KB |
Output is correct |
4 |
Execution timed out |
2064 ms |
189180 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |