#include "bits/stdc++.h"
using namespace std;
const int INF = 1e9;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,q; cin >> n >> q;
vector<tuple<int,int,int>> ev;
if(n <= 1000 && q <= 1000){
for(int i = 0; i < n; ++i){
int x,y; cin >> x >> y;
ev.emplace_back(y,x,i+1);
}
sort(ev.begin(),ev.end());
vector<int> nw_order(n+1);
vector<int> left(n),right(n);
for(int i = 0; i < n; ++i) {
left[i] = get<1>(ev[i]);
right[i] = get<0>(ev[i]);
nw_order[get<2>(ev[i])] = i;
}
vector<vector<int>> g(n);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(i == j) continue;
if(left[j] <= right[i] && right[i] <= right[j]) {
g[i].emplace_back(j);
}
}
}
auto get = [&](int a, int b){
if(a == b) return 0;
vector<int> dis(n,INF); dis[a] = 0;
queue<int> q; q.push(a);
while(!q.empty()){
int mn = q.front(); q.pop();
for(int z: g[mn]){
if(dis[z] == INF){
dis[z] = dis[mn]+1;
q.push(z);
}
}
}
if(dis[b] == INF) return -1;
else return dis[b];
};
while(q--){
int a,b; cin >> a >> b;
a = nw_order[a];
b = nw_order[b];
int dis = get(a,b);
if(dis == -1) cout << "impossible\n";
else cout << dis << '\n';
}
} else {
for(int i = 0; i < n; ++i){
int x,y; cin >> x >> y;
ev.emplace_back(x,y,i+1);
}
sort(ev.begin(),ev.end());
vector<int> nw_order(n+1);
vector<int> left(n),right(n);
for(int i = 0; i < n; ++i) {
left[i] = get<0>(ev[i]);
right[i] = get<1>(ev[i]);
nw_order[get<2>(ev[i])] = i;
}
vector<vector<int>> nxt(20,vector<int>(n+1));
nxt[0][n] = n;
int r = 0;
for(int i = 0; i < n; ++i){
while(r+1 < n && left[r+1] <= right[i]) r++;
if(i != r) nxt[0][i] = r;
else nxt[0][i] = n;
}
for(int i = 1; i < 20; ++i) {
for(int j = 0; j <= n; ++j){
nxt[i][j] = nxt[i-1][nxt[i-1][j]];
}
}
auto get = [&](int x, int y){
if(x == y) return 0;
if(x > y) return -1;
if(nxt[0][x] == n) return -1;
if(nxt[0][x] > y) return 1;
int cur = 0;
for(int i = 19; i >= 0; --i){
if(x == y) return cur;
if(nxt[i][x] <= y) {
cur |= (1<<i);
x = nxt[i][x];
}
}
if(x == y) return cur;
if(nxt[0][x] == n) return -1;
return cur+1;
};
while(q--){
int a,b; cin >> a >> b;
a = nw_order[a];
b = nw_order[b];
int ans = get(a,b);
if(ans == -1) cout << "impossible\n";
else cout << ans << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
71 ms |
11468 KB |
Output is correct |
3 |
Correct |
71 ms |
11576 KB |
Output is correct |
4 |
Correct |
72 ms |
11584 KB |
Output is correct |
5 |
Incorrect |
66 ms |
11580 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
388 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
22 ms |
1992 KB |
Output is correct |
8 |
Correct |
23 ms |
2900 KB |
Output is correct |
9 |
Correct |
151 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
388 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
22 ms |
1992 KB |
Output is correct |
8 |
Correct |
23 ms |
2900 KB |
Output is correct |
9 |
Correct |
151 ms |
4428 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
8 ms |
1108 KB |
Output is correct |
16 |
Correct |
40 ms |
1876 KB |
Output is correct |
17 |
Correct |
22 ms |
3024 KB |
Output is correct |
18 |
Correct |
122 ms |
4392 KB |
Output is correct |
19 |
Correct |
33 ms |
1308 KB |
Output is correct |
20 |
Correct |
38 ms |
1688 KB |
Output is correct |
21 |
Incorrect |
38 ms |
1888 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
388 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
1108 KB |
Output is correct |
7 |
Correct |
22 ms |
1992 KB |
Output is correct |
8 |
Correct |
23 ms |
2900 KB |
Output is correct |
9 |
Correct |
151 ms |
4428 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
404 KB |
Output is correct |
13 |
Correct |
4 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
9 ms |
1184 KB |
Output is correct |
16 |
Correct |
19 ms |
1892 KB |
Output is correct |
17 |
Correct |
23 ms |
2900 KB |
Output is correct |
18 |
Correct |
110 ms |
4388 KB |
Output is correct |
19 |
Correct |
37 ms |
10872 KB |
Output is correct |
20 |
Incorrect |
44 ms |
11280 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
11404 KB |
Output is correct |
2 |
Correct |
71 ms |
11452 KB |
Output is correct |
3 |
Correct |
94 ms |
11456 KB |
Output is correct |
4 |
Correct |
71 ms |
11916 KB |
Output is correct |
5 |
Correct |
104 ms |
11828 KB |
Output is correct |
6 |
Correct |
102 ms |
11560 KB |
Output is correct |
7 |
Correct |
118 ms |
11576 KB |
Output is correct |
8 |
Correct |
71 ms |
11684 KB |
Output is correct |
9 |
Correct |
35 ms |
10828 KB |
Output is correct |
10 |
Correct |
82 ms |
11164 KB |
Output is correct |
11 |
Correct |
70 ms |
10956 KB |
Output is correct |
12 |
Correct |
68 ms |
11164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
71 ms |
11468 KB |
Output is correct |
3 |
Correct |
71 ms |
11576 KB |
Output is correct |
4 |
Correct |
72 ms |
11584 KB |
Output is correct |
5 |
Incorrect |
66 ms |
11580 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |