//####################
//PassportJOI
//####################
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
struct segtree{
struct inter{
int deb,fin;
bool inclus_dans(inter I){
return I.deb<=deb && fin<=I.fin;
}
bool exclus_de(inter I){
return fin<I.deb || I.fin<deb;
}
inter gauche(){
int mid = (deb+fin)/2;
if(mid==fin)mid=deb;
return {deb,mid};
}
inter droite(){
int mid = (deb+fin)/2;
if(mid==fin)mid=deb;
return {mid+1,fin};
}
};
vector<vi> nodes;
int leaf;
int nb_nums;
segtree(int n){
nb_nums=n;
leaf = (1<<((int)ceil(log2(n))));
nodes.resize(leaf*2);
}
void addVal(int node,inter act,inter request,int val){
if(act.inclus_dans(request)){
nodes[node].push_back(val);
}else if(act.exclus_de(request)){
return ;
}else if(node<leaf){
addVal(node*2,act.gauche(),request,val);
addVal(node*2+1,act.droite(),request,val);
}
}
void getNext(int node,inter act,int u,vector<int> &acc){
if(!(act.deb<=u && u<=act.fin)){
return;
}
for(int &v:nodes[node]){
acc.push_back(v);
}
nodes[node].clear();
nodes[node].shrink_to_fit();
if(node<leaf){
getNext(node*2,act.gauche(),u,acc);
getNext(node*2+1,act.droite(),u,acc);
}
}
};
vector<pair<int,int>> passports;
vector<int> distN;
vector<int> dist1;
int n,q;
void bfs(int source,vector<int> &distances_bfs){
fill(distances_bfs.begin(),distances_bfs.end(),-1);
vi distances[n+1];
distances[0].push_back(source);
bool mem[n];
fill(mem,mem+n,false);
segtree st(n);
for(int i = 0;i<n;i++){
st.addVal(1,{0,st.leaf-1},{passports[i].first,passports[i].second},i);
}
//BFS
for(int i = 0;i<=n;i++){
for(int act:distances[i]){
if(mem[act])continue;
distances_bfs[act]=i;
mem[act]=true;
vi fils;
st.getNext(1,{0,st.leaf-1},act,fils);
for(int &f:fils){
if(!mem[f]){
distances[i+1].push_back(f);
}
}
}
}
}
void bfs2(vector<int> &distances_bfs){
fill(distances_bfs.begin(),distances_bfs.end(),-1);
vi distances[5*n];
bool mem[n];
fill(mem,mem+n,false);
segtree st(n);
for(int i = 0;i<n;i++){
st.addVal(1,{0,st.leaf-1},{passports[i].first,passports[i].second},i);
}
for(int i = 0;i<n;i++){
if(dist1[i]==-1 || distN[i]==-1)continue;
int d = dist1[i]+distN[i]-1;
if(i==0 || i==n-1)d++;
distances[d].push_back(i);
}
//BFS
for(int i = 0;i<=5*n-1;i++){
for(int act:distances[i]){
if(mem[act])continue;
distances_bfs[act]=i;
mem[act]=true;
vi fils;
st.getNext(1,{0,st.leaf-1},act,fils);
for(int &f:fils){
if(!mem[f]){
distances[i+1].push_back(f);
}
}
}
}
}
signed main(){
cin>>n;
for(int i=0;i<n;i++){
int a,b;cin>>a>>b;
a--;
b--;
passports.push_back({a,b});
}
distN.resize(n);
dist1.resize(n);
bfs(n-1,distN);
bfs(0,dist1);
vi ans(n);
bfs2(ans);
int q;cin>>q;
for(int i = 0;i<q;i++){
int query;cin>>query;
query--;
cout<<ans[query]<<'\n';
}
return 0;
};
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1139 ms |
78068 KB |
Output is correct |
5 |
Correct |
526 ms |
53344 KB |
Output is correct |
6 |
Correct |
382 ms |
51360 KB |
Output is correct |
7 |
Correct |
276 ms |
52928 KB |
Output is correct |
8 |
Correct |
200 ms |
47312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
432 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
432 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
1176 KB |
Output is correct |
17 |
Correct |
6 ms |
1176 KB |
Output is correct |
18 |
Correct |
6 ms |
1176 KB |
Output is correct |
19 |
Correct |
6 ms |
1176 KB |
Output is correct |
20 |
Correct |
4 ms |
1016 KB |
Output is correct |
21 |
Correct |
5 ms |
1176 KB |
Output is correct |
22 |
Correct |
3 ms |
920 KB |
Output is correct |
23 |
Correct |
5 ms |
1176 KB |
Output is correct |
24 |
Correct |
4 ms |
920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
432 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
1176 KB |
Output is correct |
17 |
Correct |
6 ms |
1176 KB |
Output is correct |
18 |
Correct |
6 ms |
1176 KB |
Output is correct |
19 |
Correct |
6 ms |
1176 KB |
Output is correct |
20 |
Correct |
4 ms |
1016 KB |
Output is correct |
21 |
Correct |
5 ms |
1176 KB |
Output is correct |
22 |
Correct |
3 ms |
920 KB |
Output is correct |
23 |
Correct |
5 ms |
1176 KB |
Output is correct |
24 |
Correct |
4 ms |
920 KB |
Output is correct |
25 |
Correct |
1 ms |
480 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
10 ms |
1176 KB |
Output is correct |
28 |
Correct |
10 ms |
1176 KB |
Output is correct |
29 |
Correct |
9 ms |
1176 KB |
Output is correct |
30 |
Correct |
8 ms |
1176 KB |
Output is correct |
31 |
Correct |
8 ms |
1176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1139 ms |
78068 KB |
Output is correct |
5 |
Correct |
526 ms |
53344 KB |
Output is correct |
6 |
Correct |
382 ms |
51360 KB |
Output is correct |
7 |
Correct |
276 ms |
52928 KB |
Output is correct |
8 |
Correct |
200 ms |
47312 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
432 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
6 ms |
1176 KB |
Output is correct |
25 |
Correct |
6 ms |
1176 KB |
Output is correct |
26 |
Correct |
6 ms |
1176 KB |
Output is correct |
27 |
Correct |
6 ms |
1176 KB |
Output is correct |
28 |
Correct |
4 ms |
1016 KB |
Output is correct |
29 |
Correct |
5 ms |
1176 KB |
Output is correct |
30 |
Correct |
3 ms |
920 KB |
Output is correct |
31 |
Correct |
5 ms |
1176 KB |
Output is correct |
32 |
Correct |
4 ms |
920 KB |
Output is correct |
33 |
Correct |
1 ms |
480 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
10 ms |
1176 KB |
Output is correct |
36 |
Correct |
10 ms |
1176 KB |
Output is correct |
37 |
Correct |
9 ms |
1176 KB |
Output is correct |
38 |
Correct |
8 ms |
1176 KB |
Output is correct |
39 |
Correct |
8 ms |
1176 KB |
Output is correct |
40 |
Correct |
1268 ms |
84160 KB |
Output is correct |
41 |
Correct |
792 ms |
58644 KB |
Output is correct |
42 |
Correct |
911 ms |
68376 KB |
Output is correct |
43 |
Correct |
941 ms |
74808 KB |
Output is correct |
44 |
Correct |
655 ms |
56484 KB |
Output is correct |
45 |
Correct |
703 ms |
60408 KB |
Output is correct |
46 |
Correct |
266 ms |
25532 KB |
Output is correct |
47 |
Correct |
758 ms |
65700 KB |
Output is correct |
48 |
Correct |
812 ms |
60428 KB |
Output is correct |