#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;
priority_queue<pair<int,int>> pq;
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;
}
pq.push({-rez[id[i]],id[i]});
}
while(!pq.empty())
{
int nod = pq.top().second;
int cdist = -pq.top().first;
pq.pop();
if(rez[nod]!=cdist)
continue;
for(auto [adj,c]:conrev[nod])
{
if(rez[adj]>rez[nod]+c)
{
rez[adj] = rez[nod]+c;
pq.push({-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);
//for(int i=1;i<=n;i++) cout<<i<<" "<<dist1[id[i]]<<" "<<distn[id[i]]<<" dist1 & distn\n";
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 |
10 ms |
25176 KB |
Output is correct |
2 |
Correct |
10 ms |
25320 KB |
Output is correct |
3 |
Correct |
9 ms |
25180 KB |
Output is correct |
4 |
Correct |
565 ms |
115184 KB |
Output is correct |
5 |
Correct |
286 ms |
74184 KB |
Output is correct |
6 |
Correct |
173 ms |
61388 KB |
Output is correct |
7 |
Correct |
124 ms |
61068 KB |
Output is correct |
8 |
Correct |
123 ms |
74664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25176 KB |
Output is correct |
2 |
Correct |
11 ms |
25180 KB |
Output is correct |
3 |
Correct |
10 ms |
25180 KB |
Output is correct |
4 |
Correct |
9 ms |
25180 KB |
Output is correct |
5 |
Correct |
9 ms |
25180 KB |
Output is correct |
6 |
Correct |
10 ms |
25228 KB |
Output is correct |
7 |
Correct |
10 ms |
25176 KB |
Output is correct |
8 |
Correct |
10 ms |
25296 KB |
Output is correct |
9 |
Correct |
10 ms |
25180 KB |
Output is correct |
10 |
Correct |
10 ms |
25180 KB |
Output is correct |
11 |
Correct |
11 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25432 KB |
Output is correct |
13 |
Correct |
10 ms |
25480 KB |
Output is correct |
14 |
Correct |
11 ms |
25432 KB |
Output is correct |
15 |
Correct |
10 ms |
25432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25176 KB |
Output is correct |
2 |
Correct |
11 ms |
25180 KB |
Output is correct |
3 |
Correct |
10 ms |
25180 KB |
Output is correct |
4 |
Correct |
9 ms |
25180 KB |
Output is correct |
5 |
Correct |
9 ms |
25180 KB |
Output is correct |
6 |
Correct |
10 ms |
25228 KB |
Output is correct |
7 |
Correct |
10 ms |
25176 KB |
Output is correct |
8 |
Correct |
10 ms |
25296 KB |
Output is correct |
9 |
Correct |
10 ms |
25180 KB |
Output is correct |
10 |
Correct |
10 ms |
25180 KB |
Output is correct |
11 |
Correct |
11 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25432 KB |
Output is correct |
13 |
Correct |
10 ms |
25480 KB |
Output is correct |
14 |
Correct |
11 ms |
25432 KB |
Output is correct |
15 |
Correct |
10 ms |
25432 KB |
Output is correct |
16 |
Correct |
13 ms |
26204 KB |
Output is correct |
17 |
Correct |
14 ms |
25944 KB |
Output is correct |
18 |
Correct |
14 ms |
26456 KB |
Output is correct |
19 |
Correct |
13 ms |
26204 KB |
Output is correct |
20 |
Correct |
12 ms |
25692 KB |
Output is correct |
21 |
Correct |
12 ms |
25948 KB |
Output is correct |
22 |
Correct |
11 ms |
25692 KB |
Output is correct |
23 |
Correct |
13 ms |
25876 KB |
Output is correct |
24 |
Correct |
15 ms |
26204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25176 KB |
Output is correct |
2 |
Correct |
11 ms |
25180 KB |
Output is correct |
3 |
Correct |
10 ms |
25180 KB |
Output is correct |
4 |
Correct |
9 ms |
25180 KB |
Output is correct |
5 |
Correct |
9 ms |
25180 KB |
Output is correct |
6 |
Correct |
10 ms |
25228 KB |
Output is correct |
7 |
Correct |
10 ms |
25176 KB |
Output is correct |
8 |
Correct |
10 ms |
25296 KB |
Output is correct |
9 |
Correct |
10 ms |
25180 KB |
Output is correct |
10 |
Correct |
10 ms |
25180 KB |
Output is correct |
11 |
Correct |
11 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25432 KB |
Output is correct |
13 |
Correct |
10 ms |
25480 KB |
Output is correct |
14 |
Correct |
11 ms |
25432 KB |
Output is correct |
15 |
Correct |
10 ms |
25432 KB |
Output is correct |
16 |
Correct |
13 ms |
26204 KB |
Output is correct |
17 |
Correct |
14 ms |
25944 KB |
Output is correct |
18 |
Correct |
14 ms |
26456 KB |
Output is correct |
19 |
Correct |
13 ms |
26204 KB |
Output is correct |
20 |
Correct |
12 ms |
25692 KB |
Output is correct |
21 |
Correct |
12 ms |
25948 KB |
Output is correct |
22 |
Correct |
11 ms |
25692 KB |
Output is correct |
23 |
Correct |
13 ms |
25876 KB |
Output is correct |
24 |
Correct |
15 ms |
26204 KB |
Output is correct |
25 |
Correct |
9 ms |
25356 KB |
Output is correct |
26 |
Correct |
12 ms |
25436 KB |
Output is correct |
27 |
Correct |
13 ms |
26204 KB |
Output is correct |
28 |
Correct |
13 ms |
26056 KB |
Output is correct |
29 |
Correct |
11 ms |
25692 KB |
Output is correct |
30 |
Correct |
13 ms |
25948 KB |
Output is correct |
31 |
Correct |
12 ms |
25948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25176 KB |
Output is correct |
2 |
Correct |
10 ms |
25320 KB |
Output is correct |
3 |
Correct |
9 ms |
25180 KB |
Output is correct |
4 |
Correct |
565 ms |
115184 KB |
Output is correct |
5 |
Correct |
286 ms |
74184 KB |
Output is correct |
6 |
Correct |
173 ms |
61388 KB |
Output is correct |
7 |
Correct |
124 ms |
61068 KB |
Output is correct |
8 |
Correct |
123 ms |
74664 KB |
Output is correct |
9 |
Correct |
10 ms |
25176 KB |
Output is correct |
10 |
Correct |
11 ms |
25180 KB |
Output is correct |
11 |
Correct |
10 ms |
25180 KB |
Output is correct |
12 |
Correct |
9 ms |
25180 KB |
Output is correct |
13 |
Correct |
9 ms |
25180 KB |
Output is correct |
14 |
Correct |
10 ms |
25228 KB |
Output is correct |
15 |
Correct |
10 ms |
25176 KB |
Output is correct |
16 |
Correct |
10 ms |
25296 KB |
Output is correct |
17 |
Correct |
10 ms |
25180 KB |
Output is correct |
18 |
Correct |
10 ms |
25180 KB |
Output is correct |
19 |
Correct |
11 ms |
25436 KB |
Output is correct |
20 |
Correct |
12 ms |
25432 KB |
Output is correct |
21 |
Correct |
10 ms |
25480 KB |
Output is correct |
22 |
Correct |
11 ms |
25432 KB |
Output is correct |
23 |
Correct |
10 ms |
25432 KB |
Output is correct |
24 |
Correct |
13 ms |
26204 KB |
Output is correct |
25 |
Correct |
14 ms |
25944 KB |
Output is correct |
26 |
Correct |
14 ms |
26456 KB |
Output is correct |
27 |
Correct |
13 ms |
26204 KB |
Output is correct |
28 |
Correct |
12 ms |
25692 KB |
Output is correct |
29 |
Correct |
12 ms |
25948 KB |
Output is correct |
30 |
Correct |
11 ms |
25692 KB |
Output is correct |
31 |
Correct |
13 ms |
25876 KB |
Output is correct |
32 |
Correct |
15 ms |
26204 KB |
Output is correct |
33 |
Correct |
9 ms |
25356 KB |
Output is correct |
34 |
Correct |
12 ms |
25436 KB |
Output is correct |
35 |
Correct |
13 ms |
26204 KB |
Output is correct |
36 |
Correct |
13 ms |
26056 KB |
Output is correct |
37 |
Correct |
11 ms |
25692 KB |
Output is correct |
38 |
Correct |
13 ms |
25948 KB |
Output is correct |
39 |
Correct |
12 ms |
25948 KB |
Output is correct |
40 |
Correct |
594 ms |
120204 KB |
Output is correct |
41 |
Correct |
331 ms |
77484 KB |
Output is correct |
42 |
Correct |
470 ms |
132572 KB |
Output is correct |
43 |
Correct |
448 ms |
132392 KB |
Output is correct |
44 |
Correct |
221 ms |
64612 KB |
Output is correct |
45 |
Correct |
309 ms |
78144 KB |
Output is correct |
46 |
Correct |
100 ms |
45260 KB |
Output is correct |
47 |
Correct |
311 ms |
83760 KB |
Output is correct |
48 |
Correct |
293 ms |
98876 KB |
Output is correct |