#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#define S 262144
using namespace std;
vector<pair<int,int>> V[4*S];
bool vis[4*S][3];
int dist[4*S][3];
void BFS(int x, int t){
deque<pair<int,int>> q;
q.push_front({x,0});
while(!q.empty()){
int v = q.front().first;
int d = q.front().second;
q.pop_front();
if(vis[v][t])
continue;
vis[v][t] = 1;
dist[v][t] = d;
for(int i = 0; i < V[v].size(); i++){
int u = V[v][i].first;
int d2 = V[v][i].second;
if(d2 == 0){
q.push_front({u,d});
}else{
q.push_back({u,d+1});
}
}
}
}
void addEdge(int l, int r, int ll, int rr, int w, int v){
if(l > rr || r < ll)
return;
if(l >= ll && r <= rr){
V[w].push_back({v,1});
return;
}
int sr = (l+r)/2;
addEdge(l,sr,ll,rr,w*2,v);
addEdge(sr+1,r,ll,rr,w*2+1,v);
}
int ans[S];
int tree[4*S];
void sett(int w, int x){
w = w+S-1;
while(w != 0){
tree[w] = min(tree[w], x);
w /= 2;
}
}
int getMin(int l, int r, int ll, int rr, int w){
if(l > rr || r < ll)
return 1e9;
if(l >= ll && r <= rr){
return tree[w];
}
int sr = (l+r)/2;
return min(getMin(l,sr,ll,rr,w*2), getMin(sr+1,r,ll,rr,w*2+1));
}
int L[S];
int R[S];
int main(void){
for(int i = 2; i < 2*S; i++){
V[i].push_back({i/2,0});
}
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++){
int l, r;
scanf("%d %d",&l,&r);
L[i] = l;
R[i] = r;
addEdge(1,S,l,r,1,2*S+i);
}
for(int i = 1; i <= n; i++){
V[2*S+i].push_back({i+S-1,0});
}
BFS(2*S+1,1);
BFS(2*S+n,2);
dist[2*S+1][1] = 1;
dist[2*S+n][2] = 1;
for(int i = 1; i < 2*S;i++){
tree[i] = 1e9;
}
vector<pair<int,int>> P;
for(int i = 1; i <= n; i++){
if(!vis[2*S+i][1] || !vis[2*S+i][2]){
ans[i] = 1e9;
}else{
ans[i] = dist[2*S+i][1] + dist[2*S+i][2] - 1;
}
sett(i, ans[i]);
if(ans[i] < 1e8)
P.push_back({ans[i], i});
}
sort(P.begin(), P.end());
for(int i = 0; i < P.size(); i++){
int v = P[i].second;
// printf("%d %d*\n",v,ans[v]);
ans[v] = min(ans[v], 1 + getMin(1,S,L[v], R[v], 1));
sett(v,ans[v]);
// printf("%d %d$\n",v, ans[v]);
}
int q;
scanf("%d", &q);
for(int i = 1; i <= q; i++){
int x;
scanf("%d", &x);
if(ans[x] >= 1e8)
ans[x] = -1;
printf("%d\n",ans[x]);
}
// for(int i = 1; i <= n; i++){
// printf("%d %d\n", dist[2*S+i][1], dist[2*S+i][2]);
// }
return 0;
}
Compilation message
passport.cpp: In function 'void BFS(int, int)':
passport.cpp:25:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 0; i < V[v].size(); i++){
| ~~^~~~~~~~~~~~~
passport.cpp: In function 'int main()':
passport.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 0; i < P.size(); i++){
| ~~^~~~~~~~~~
passport.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
passport.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d %d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~
passport.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
passport.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
54836 KB |
Output is correct |
2 |
Correct |
26 ms |
54620 KB |
Output is correct |
3 |
Correct |
26 ms |
54620 KB |
Output is correct |
4 |
Correct |
605 ms |
115044 KB |
Output is correct |
5 |
Correct |
237 ms |
78708 KB |
Output is correct |
6 |
Correct |
153 ms |
73672 KB |
Output is correct |
7 |
Correct |
166 ms |
97832 KB |
Output is correct |
8 |
Correct |
125 ms |
90620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
54612 KB |
Output is correct |
2 |
Correct |
27 ms |
54608 KB |
Output is correct |
3 |
Correct |
27 ms |
54868 KB |
Output is correct |
4 |
Correct |
27 ms |
54612 KB |
Output is correct |
5 |
Correct |
26 ms |
54620 KB |
Output is correct |
6 |
Correct |
27 ms |
54620 KB |
Output is correct |
7 |
Correct |
27 ms |
54620 KB |
Output is correct |
8 |
Correct |
26 ms |
54608 KB |
Output is correct |
9 |
Correct |
30 ms |
54616 KB |
Output is correct |
10 |
Correct |
26 ms |
54804 KB |
Output is correct |
11 |
Correct |
26 ms |
54716 KB |
Output is correct |
12 |
Correct |
30 ms |
54876 KB |
Output is correct |
13 |
Correct |
28 ms |
54692 KB |
Output is correct |
14 |
Correct |
27 ms |
54864 KB |
Output is correct |
15 |
Correct |
28 ms |
54876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
54612 KB |
Output is correct |
2 |
Correct |
27 ms |
54608 KB |
Output is correct |
3 |
Correct |
27 ms |
54868 KB |
Output is correct |
4 |
Correct |
27 ms |
54612 KB |
Output is correct |
5 |
Correct |
26 ms |
54620 KB |
Output is correct |
6 |
Correct |
27 ms |
54620 KB |
Output is correct |
7 |
Correct |
27 ms |
54620 KB |
Output is correct |
8 |
Correct |
26 ms |
54608 KB |
Output is correct |
9 |
Correct |
30 ms |
54616 KB |
Output is correct |
10 |
Correct |
26 ms |
54804 KB |
Output is correct |
11 |
Correct |
26 ms |
54716 KB |
Output is correct |
12 |
Correct |
30 ms |
54876 KB |
Output is correct |
13 |
Correct |
28 ms |
54692 KB |
Output is correct |
14 |
Correct |
27 ms |
54864 KB |
Output is correct |
15 |
Correct |
28 ms |
54876 KB |
Output is correct |
16 |
Correct |
29 ms |
55220 KB |
Output is correct |
17 |
Correct |
29 ms |
54864 KB |
Output is correct |
18 |
Correct |
32 ms |
55124 KB |
Output is correct |
19 |
Correct |
29 ms |
55292 KB |
Output is correct |
20 |
Correct |
28 ms |
54876 KB |
Output is correct |
21 |
Correct |
28 ms |
54864 KB |
Output is correct |
22 |
Correct |
37 ms |
55120 KB |
Output is correct |
23 |
Correct |
28 ms |
55324 KB |
Output is correct |
24 |
Correct |
29 ms |
55132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
54612 KB |
Output is correct |
2 |
Correct |
27 ms |
54608 KB |
Output is correct |
3 |
Correct |
27 ms |
54868 KB |
Output is correct |
4 |
Correct |
27 ms |
54612 KB |
Output is correct |
5 |
Correct |
26 ms |
54620 KB |
Output is correct |
6 |
Correct |
27 ms |
54620 KB |
Output is correct |
7 |
Correct |
27 ms |
54620 KB |
Output is correct |
8 |
Correct |
26 ms |
54608 KB |
Output is correct |
9 |
Correct |
30 ms |
54616 KB |
Output is correct |
10 |
Correct |
26 ms |
54804 KB |
Output is correct |
11 |
Correct |
26 ms |
54716 KB |
Output is correct |
12 |
Correct |
30 ms |
54876 KB |
Output is correct |
13 |
Correct |
28 ms |
54692 KB |
Output is correct |
14 |
Correct |
27 ms |
54864 KB |
Output is correct |
15 |
Correct |
28 ms |
54876 KB |
Output is correct |
16 |
Correct |
29 ms |
55220 KB |
Output is correct |
17 |
Correct |
29 ms |
54864 KB |
Output is correct |
18 |
Correct |
32 ms |
55124 KB |
Output is correct |
19 |
Correct |
29 ms |
55292 KB |
Output is correct |
20 |
Correct |
28 ms |
54876 KB |
Output is correct |
21 |
Correct |
28 ms |
54864 KB |
Output is correct |
22 |
Correct |
37 ms |
55120 KB |
Output is correct |
23 |
Correct |
28 ms |
55324 KB |
Output is correct |
24 |
Correct |
29 ms |
55132 KB |
Output is correct |
25 |
Correct |
26 ms |
54620 KB |
Output is correct |
26 |
Correct |
33 ms |
54784 KB |
Output is correct |
27 |
Correct |
30 ms |
55004 KB |
Output is correct |
28 |
Correct |
29 ms |
54876 KB |
Output is correct |
29 |
Correct |
29 ms |
54872 KB |
Output is correct |
30 |
Correct |
29 ms |
55076 KB |
Output is correct |
31 |
Correct |
29 ms |
55032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
54836 KB |
Output is correct |
2 |
Correct |
26 ms |
54620 KB |
Output is correct |
3 |
Correct |
26 ms |
54620 KB |
Output is correct |
4 |
Correct |
605 ms |
115044 KB |
Output is correct |
5 |
Correct |
237 ms |
78708 KB |
Output is correct |
6 |
Correct |
153 ms |
73672 KB |
Output is correct |
7 |
Correct |
166 ms |
97832 KB |
Output is correct |
8 |
Correct |
125 ms |
90620 KB |
Output is correct |
9 |
Correct |
30 ms |
54612 KB |
Output is correct |
10 |
Correct |
27 ms |
54608 KB |
Output is correct |
11 |
Correct |
27 ms |
54868 KB |
Output is correct |
12 |
Correct |
27 ms |
54612 KB |
Output is correct |
13 |
Correct |
26 ms |
54620 KB |
Output is correct |
14 |
Correct |
27 ms |
54620 KB |
Output is correct |
15 |
Correct |
27 ms |
54620 KB |
Output is correct |
16 |
Correct |
26 ms |
54608 KB |
Output is correct |
17 |
Correct |
30 ms |
54616 KB |
Output is correct |
18 |
Correct |
26 ms |
54804 KB |
Output is correct |
19 |
Correct |
26 ms |
54716 KB |
Output is correct |
20 |
Correct |
30 ms |
54876 KB |
Output is correct |
21 |
Correct |
28 ms |
54692 KB |
Output is correct |
22 |
Correct |
27 ms |
54864 KB |
Output is correct |
23 |
Correct |
28 ms |
54876 KB |
Output is correct |
24 |
Correct |
29 ms |
55220 KB |
Output is correct |
25 |
Correct |
29 ms |
54864 KB |
Output is correct |
26 |
Correct |
32 ms |
55124 KB |
Output is correct |
27 |
Correct |
29 ms |
55292 KB |
Output is correct |
28 |
Correct |
28 ms |
54876 KB |
Output is correct |
29 |
Correct |
28 ms |
54864 KB |
Output is correct |
30 |
Correct |
37 ms |
55120 KB |
Output is correct |
31 |
Correct |
28 ms |
55324 KB |
Output is correct |
32 |
Correct |
29 ms |
55132 KB |
Output is correct |
33 |
Correct |
26 ms |
54620 KB |
Output is correct |
34 |
Correct |
33 ms |
54784 KB |
Output is correct |
35 |
Correct |
30 ms |
55004 KB |
Output is correct |
36 |
Correct |
29 ms |
54876 KB |
Output is correct |
37 |
Correct |
29 ms |
54872 KB |
Output is correct |
38 |
Correct |
29 ms |
55076 KB |
Output is correct |
39 |
Correct |
29 ms |
55032 KB |
Output is correct |
40 |
Correct |
646 ms |
116752 KB |
Output is correct |
41 |
Correct |
273 ms |
80832 KB |
Output is correct |
42 |
Correct |
403 ms |
106536 KB |
Output is correct |
43 |
Correct |
382 ms |
109100 KB |
Output is correct |
44 |
Correct |
187 ms |
76048 KB |
Output is correct |
45 |
Correct |
225 ms |
80928 KB |
Output is correct |
46 |
Correct |
116 ms |
65180 KB |
Output is correct |
47 |
Correct |
274 ms |
95060 KB |
Output is correct |
48 |
Correct |
293 ms |
89724 KB |
Output is correct |