#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int n, q;
cin >> n >> q;
vector<pair<int, int> > inter(n);
for (int i = 0; i < n; i++){
cin >> inter[i].first >> inter[i].second;
}
if (n > 1000){
vector<pair<pair<int, int>, int> > elso(n);
vector<int> hely(n);
for (int i = 0; i < n; i++){
elso[i] = make_pair(inter[i], i);
}
sort(elso.begin(), elso.end());
for (int i = 0; i < n; i++){
hely[elso[i].second] = i;
}
vector<ll> tav(n, -1);
for (ll i = 0; i < n; i++){
if (tav[i] != -1) continue;
queue<ll> q;
q.push(i);
tav[i] = i*10000000;
while(!q.empty()){
ll pos = q.front();
q.pop();
if (pos != n - 1 && elso[pos].first.second <= elso[pos + 1].first.second && elso[pos].first.second >= elso[pos + 1].first.first){
tav[pos + 1] = tav[pos] + 1;
q.push(pos + 1);
}
}
}
for (int i = 0; i < q; i++){
int a, b;
cin >> a >> b;
a--, b--;
a = hely[a];
b = hely[b];
if (tav[b] - tav[a] < 0 || tav[b] - tav[a] > 999999){
cout << "impossible" << endl;
}
else cout << tav[b] - tav[a] << endl;
}
}
vector<vector<int> > graph(n);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i == j) continue;
if (inter[i].second <= inter[j].second && inter[i].second >= inter[j].first){
graph[i].push_back(j);
}
}
}
// for (int i = 0; i < n; i++){
// for (int x : graph[i]) cout << x << " ";
// cout << endl;
// }
for (int i = 0; i < q; i++){
int s, e;
cin >> s >> e;
s--, e--;
queue<int> q;
q.push(s);
vector<int> tav(n, -1);
tav [s] = 0;
while(!q.empty()){
int pos = q.front();
q.pop();
for (int x : graph[pos]){
if (tav[x] == -1){
tav[x] = tav[pos] + 1;
q.push(x);
}
}
}
if (tav[e] == -1) {
cout << "impossible" << endl;
}
else cout << tav[e] << endl;
}
return 0;
}
/*
5 2
1 3
2 4
4 7
7 9
3 7
1 4
3 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1574 ms |
4172 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
352 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
1108 KB |
Output is correct |
7 |
Correct |
26 ms |
1876 KB |
Output is correct |
8 |
Correct |
25 ms |
2928 KB |
Output is correct |
9 |
Correct |
105 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
352 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
1108 KB |
Output is correct |
7 |
Correct |
26 ms |
1876 KB |
Output is correct |
8 |
Correct |
25 ms |
2928 KB |
Output is correct |
9 |
Correct |
105 ms |
4308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
340 KB |
Output is correct |
15 |
Correct |
11 ms |
1108 KB |
Output is correct |
16 |
Correct |
29 ms |
1876 KB |
Output is correct |
17 |
Correct |
25 ms |
2916 KB |
Output is correct |
18 |
Correct |
110 ms |
4272 KB |
Output is correct |
19 |
Runtime error |
397 ms |
61840 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
7 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
352 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
1108 KB |
Output is correct |
7 |
Correct |
26 ms |
1876 KB |
Output is correct |
8 |
Correct |
25 ms |
2928 KB |
Output is correct |
9 |
Correct |
105 ms |
4308 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
1108 KB |
Output is correct |
16 |
Correct |
22 ms |
1840 KB |
Output is correct |
17 |
Correct |
24 ms |
2900 KB |
Output is correct |
18 |
Correct |
108 ms |
4308 KB |
Output is correct |
19 |
Execution timed out |
1590 ms |
3440 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1549 ms |
4168 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1574 ms |
4172 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |