#include <bits/stdc++.h>
using namespace std;
#define int int64_t
typedef pair<int,int> pii;
typedef vector<int> vii;
#define MP make_pair
#define PB push_back
#define f first
#define s second
#define INF 2e18
#define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
struct Interval
{
int id;
pii range;
};
int N, Q;
vector<Interval> intervals;
vector<pii> queries;
vector<int> adjBest;
vector<set<int>> adjAll;
int32_t main()
{
fast_io();
cin >> N >> Q;
intervals = vector<Interval>(N + 1);
adjBest = vector<int>(N + 1);
adjAll = vector<set<int>>(N + 1);
//vector<pii> endPoints;
// { time, 0 for start / 1 for end }
for (int i = 1; i <= N; i++)
{
Interval inter;
cin >> inter.range.f >> inter.range.s;
inter.id = i + 1;
intervals[i] = inter;
//endPoints.push_back({inter.range.f, 0});
//endPoints.push_back({inter.range.s, 1});
}
queries = vector<pii>(Q);
for (int i = 0; i < Q; i++)
{
cin >> queries[i].f >> queries[i].s;
}
//sort(all(endPoints));
//sort(all(intervals), [](Interval a, Interval b)
// { return a.range.s < b.range.s; });
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
if (i == j)
continue;
if (intervals[i].range.s >= intervals[j].range.f &&
intervals[i].range.s <= intervals[j].range.s)
{
adjAll[i].insert(j);
}
}
}
for (int i = 1; i <= N; i++)
{
int bestE = -1;
int bestId = -1;
//cout << "adjAll[" << i << "]: {";
for (int j : adjAll[i])
{
//cout << j << " ";
if (intervals[j].range.s > bestE)
{
bestId = j;
bestE = intervals[j].range.s;
}
}
//cout << "}\n";
adjBest[i] = bestId;
}
for (int q = 0; q < Q; q++)
{
if (queries[q].f == queries[q].s)
{
cout << 0 << '\n';
continue;
}
vector<int> dis(N + 1, INF);
queue<int> qu;
qu.push(queries[q].f);
dis[queries[q].f] = 0;
bool impossible = true;
while (!qu.empty())
{
int fr = qu.front();
qu.pop();
//cout << "at: " << fr << '\n';
if (adjAll[fr].count(queries[q].s))
{
//cout << "can go already\n";
cout << dis[fr] + 1 << '\n';
impossible = false;
break;
}
if (adjBest[fr] != -1 && dis[adjBest[fr]] == INF)
{
dis[adjBest[fr]] = dis[fr] + 1;
qu.push(adjBest[fr]);
}
}
if (impossible)
{
cout << "impossible\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1580 ms |
13352 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
572 KB |
Output is correct |
5 |
Incorrect |
4 ms |
568 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
572 KB |
Output is correct |
5 |
Incorrect |
4 ms |
568 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
572 KB |
Output is correct |
5 |
Incorrect |
4 ms |
568 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1508 ms |
13364 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1580 ms |
13352 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |