#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n,cntn;
vector<pair<int,int>> con[530005];
vector<pair<int,int>> conrev[530005];
int id[200005];
void build(int nod, int st, int dr)
{
cntn = max(cntn, nod);
if(st==dr)
{
id[st]=nod;
return;
}
con[nod].push_back({nod*2,0});
con[nod].push_back({nod*2+1,0});
conrev[nod*2].push_back({nod,0});
conrev[nod*2+1].push_back({nod,0});
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
}
void baga(int nod, int st, int dr, int le, int ri, int from)
{
if(le>ri)
return;
if(le==st && dr==ri)
{
con[from].push_back({nod,1});
conrev[nod].push_back({from,1});
return;
}
int mij=(st+dr)/2;
baga(nod*2,st,mij,le,min(mij,ri),from);
baga(nod*2+1,mij+1,dr,max(mij+1,le),ri,from);
}
void bfs(int src, int dist[])
{
for(int i=1;i<=cntn;i++)
dist[i]=INF;
deque<int> dq;
dist[src]=0;
dq.push_back(src);
while(!dq.empty())
{
int nod = dq.front();
dq.pop_front();
for(auto [adj,c]:conrev[nod])
{
if(dist[adj] > dist[nod]+c)
{
dist[adj] = dist[nod]+c;
if(c==1) dq.push_back(adj);
else dq.push_front(adj);
}
}
}
}
int dist1[530000],distn[530000],rez[530000];
void dijkstra()
{
for(int i=1;i<=cntn;i++)
rez[i]=INF;
deque<pair<int,int>> dq;
for(int i=1;i<=n;i++)
{
if(i==1) rez[id[i]] = distn[id[i]];
else if(i==n) rez[id[i]] = dist1[id[i]];
else if(dist1[id[i]]==1 && distn[id[i]]==1) rez[id[i]] = 1;
else if(dist1[id[i]]==1) rez[id[i]] = distn[id[i]];
else if(distn[id[i]]==1) rez[id[i]] = dist1[id[i]];
else rez[id[i]] = dist1[id[i]] + distn[id[i]] - 1;
dq.push_back({rez[id[i]],id[i]});
}
sort(dq.begin(),dq.end());
while(!dq.empty())
{
int nod = dq.front().second;
int cdist = dq.front().first;
dq.pop_front();
if(rez[nod]!=cdist)
continue;
for(auto [adj,c]:conrev[nod])
{
if(rez[adj]>rez[nod]+c)
{
rez[adj] = rez[nod]+c;
if(c==0) dq.push_front({rez[adj],adj});
else dq.push_back({rez[adj],adj});
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
build(1,1,n);
int le,ri;
for(int i=1;i<=n;i++)
{
cin>>le>>ri;
baga(1,1,n,le,ri,id[i]);
}
bfs(id[1],dist1);
bfs(id[n],distn);
dijkstra();
int q,a;
cin>>q;
while(q--)
{
cin>>a;
if(rez[id[a]]<=n+5) cout<<rez[id[a]]<<"\n";
else cout<<-1<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32188 KB |
Output is correct |
3 |
Correct |
4 ms |
31984 KB |
Output is correct |
4 |
Correct |
466 ms |
115192 KB |
Output is correct |
5 |
Correct |
249 ms |
73812 KB |
Output is correct |
6 |
Correct |
127 ms |
61068 KB |
Output is correct |
7 |
Correct |
92 ms |
59848 KB |
Output is correct |
8 |
Correct |
104 ms |
72880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32088 KB |
Output is correct |
5 |
Correct |
4 ms |
32184 KB |
Output is correct |
6 |
Correct |
6 ms |
32088 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32092 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
5 ms |
32092 KB |
Output is correct |
13 |
Correct |
5 ms |
32092 KB |
Output is correct |
14 |
Correct |
6 ms |
32092 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32088 KB |
Output is correct |
5 |
Correct |
4 ms |
32184 KB |
Output is correct |
6 |
Correct |
6 ms |
32088 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32092 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
5 ms |
32092 KB |
Output is correct |
13 |
Correct |
5 ms |
32092 KB |
Output is correct |
14 |
Correct |
6 ms |
32092 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
7 ms |
32860 KB |
Output is correct |
17 |
Correct |
7 ms |
32708 KB |
Output is correct |
18 |
Correct |
9 ms |
32900 KB |
Output is correct |
19 |
Correct |
7 ms |
33112 KB |
Output is correct |
20 |
Correct |
6 ms |
32344 KB |
Output is correct |
21 |
Correct |
14 ms |
32604 KB |
Output is correct |
22 |
Correct |
6 ms |
32348 KB |
Output is correct |
23 |
Correct |
7 ms |
32652 KB |
Output is correct |
24 |
Correct |
7 ms |
32860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32088 KB |
Output is correct |
5 |
Correct |
4 ms |
32184 KB |
Output is correct |
6 |
Correct |
6 ms |
32088 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32092 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
5 ms |
32092 KB |
Output is correct |
13 |
Correct |
5 ms |
32092 KB |
Output is correct |
14 |
Correct |
6 ms |
32092 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
7 ms |
32860 KB |
Output is correct |
17 |
Correct |
7 ms |
32708 KB |
Output is correct |
18 |
Correct |
9 ms |
32900 KB |
Output is correct |
19 |
Correct |
7 ms |
33112 KB |
Output is correct |
20 |
Correct |
6 ms |
32344 KB |
Output is correct |
21 |
Correct |
14 ms |
32604 KB |
Output is correct |
22 |
Correct |
6 ms |
32348 KB |
Output is correct |
23 |
Correct |
7 ms |
32652 KB |
Output is correct |
24 |
Correct |
7 ms |
32860 KB |
Output is correct |
25 |
Correct |
6 ms |
32092 KB |
Output is correct |
26 |
Correct |
6 ms |
32092 KB |
Output is correct |
27 |
Correct |
8 ms |
32860 KB |
Output is correct |
28 |
Correct |
7 ms |
32524 KB |
Output is correct |
29 |
Correct |
7 ms |
32348 KB |
Output is correct |
30 |
Correct |
14 ms |
32604 KB |
Output is correct |
31 |
Correct |
7 ms |
32600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32188 KB |
Output is correct |
3 |
Correct |
4 ms |
31984 KB |
Output is correct |
4 |
Correct |
466 ms |
115192 KB |
Output is correct |
5 |
Correct |
249 ms |
73812 KB |
Output is correct |
6 |
Correct |
127 ms |
61068 KB |
Output is correct |
7 |
Correct |
92 ms |
59848 KB |
Output is correct |
8 |
Correct |
104 ms |
72880 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32092 KB |
Output is correct |
11 |
Correct |
5 ms |
32092 KB |
Output is correct |
12 |
Correct |
5 ms |
32088 KB |
Output is correct |
13 |
Correct |
4 ms |
32184 KB |
Output is correct |
14 |
Correct |
6 ms |
32088 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
4 ms |
32092 KB |
Output is correct |
17 |
Correct |
5 ms |
32092 KB |
Output is correct |
18 |
Correct |
5 ms |
32092 KB |
Output is correct |
19 |
Correct |
4 ms |
32092 KB |
Output is correct |
20 |
Correct |
5 ms |
32092 KB |
Output is correct |
21 |
Correct |
5 ms |
32092 KB |
Output is correct |
22 |
Correct |
6 ms |
32092 KB |
Output is correct |
23 |
Correct |
5 ms |
32092 KB |
Output is correct |
24 |
Correct |
7 ms |
32860 KB |
Output is correct |
25 |
Correct |
7 ms |
32708 KB |
Output is correct |
26 |
Correct |
9 ms |
32900 KB |
Output is correct |
27 |
Correct |
7 ms |
33112 KB |
Output is correct |
28 |
Correct |
6 ms |
32344 KB |
Output is correct |
29 |
Correct |
14 ms |
32604 KB |
Output is correct |
30 |
Correct |
6 ms |
32348 KB |
Output is correct |
31 |
Correct |
7 ms |
32652 KB |
Output is correct |
32 |
Correct |
7 ms |
32860 KB |
Output is correct |
33 |
Correct |
6 ms |
32092 KB |
Output is correct |
34 |
Correct |
6 ms |
32092 KB |
Output is correct |
35 |
Correct |
8 ms |
32860 KB |
Output is correct |
36 |
Correct |
7 ms |
32524 KB |
Output is correct |
37 |
Correct |
7 ms |
32348 KB |
Output is correct |
38 |
Correct |
14 ms |
32604 KB |
Output is correct |
39 |
Correct |
7 ms |
32600 KB |
Output is correct |
40 |
Correct |
538 ms |
116188 KB |
Output is correct |
41 |
Correct |
259 ms |
75272 KB |
Output is correct |
42 |
Execution timed out |
2056 ms |
129360 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |