#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int Inicio[MAXN], Fim[MAXN];
vector<int> Indices;
vector<int> grafo[MAXN];
int Marc[MAXN], Dist[MAXN];
void bfs(int v, int N)
{
for (int i = 1; i <= N; i++) {Marc[i] = 0; Dist[i] = 1e9 + 10;}
queue<int> q;
Marc[v] = 1; Dist[v] = 0;
q.push(v);
while (!q.empty())
{
int cur = q.front(); q.pop();
for (auto viz : grafo[cur])
{
if (Marc[viz]) continue;
Dist[viz] = Dist[cur] + 1;
Marc[viz] = 1;
q.push(viz);
}
}
}
bool cmp(int a, int b)
{
if (Fim[a] == Fim[b]) return Inicio[a] < Inicio[b];
return Fim[a] > Fim[b];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; i++)
{
cin >> Inicio[i] >> Fim[i];
Indices.push_back(i);
}
sort(Indices.begin(), Indices.end(), cmp);
for (int i = 0; i < Indices.size(); i++)
{
int id = Indices[i];
for (int j = 0; j < Indices.size(); j++)
{
int jd = Indices[j];
if (Inicio[jd] <= Fim[id] && Fim[id] <= Fim[jd]) {grafo[id].push_back(jd);}
}
}
for (int q = 1; q <= Q; q++)
{
int X, Y;
cin >> X >> Y;
bfs(X, N);
if (Dist[Y] == 1e9 + 10) cout << "impossible" << '\n';
else cout << Dist[Y] << '\n';
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < Indices.size(); i++)
| ~~^~~~~~~~~~~~~~~~
events.cpp:52:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j = 0; j < Indices.size(); j++)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1574 ms |
4052 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
5 ms |
2704 KB |
Output is correct |
5 |
Correct |
4 ms |
2748 KB |
Output is correct |
6 |
Correct |
9 ms |
3464 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5384 KB |
Output is correct |
9 |
Correct |
90 ms |
6720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
5 ms |
2704 KB |
Output is correct |
5 |
Correct |
4 ms |
2748 KB |
Output is correct |
6 |
Correct |
9 ms |
3464 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5384 KB |
Output is correct |
9 |
Correct |
90 ms |
6720 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
5 ms |
2644 KB |
Output is correct |
13 |
Correct |
4 ms |
2644 KB |
Output is correct |
14 |
Correct |
5 ms |
2644 KB |
Output is correct |
15 |
Correct |
8 ms |
3412 KB |
Output is correct |
16 |
Correct |
16 ms |
4308 KB |
Output is correct |
17 |
Correct |
18 ms |
5332 KB |
Output is correct |
18 |
Correct |
80 ms |
6724 KB |
Output is correct |
19 |
Execution timed out |
1584 ms |
32716 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
5 ms |
2704 KB |
Output is correct |
5 |
Correct |
4 ms |
2748 KB |
Output is correct |
6 |
Correct |
9 ms |
3464 KB |
Output is correct |
7 |
Correct |
18 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5384 KB |
Output is correct |
9 |
Correct |
90 ms |
6720 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
5 ms |
2644 KB |
Output is correct |
13 |
Correct |
4 ms |
2708 KB |
Output is correct |
14 |
Correct |
4 ms |
2644 KB |
Output is correct |
15 |
Correct |
8 ms |
3412 KB |
Output is correct |
16 |
Correct |
17 ms |
4248 KB |
Output is correct |
17 |
Correct |
17 ms |
5332 KB |
Output is correct |
18 |
Correct |
82 ms |
6612 KB |
Output is correct |
19 |
Execution timed out |
1529 ms |
4796 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1574 ms |
4240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1574 ms |
4052 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |