#include<bits/stdc++.h>
#define debug true
#ifdef DEVAL
#define debug false
#endif
using namespace std;
const int maxn = 1e5 + 10;
struct interval
{
int s, e;
}seg[maxn];
int n, q;
pair < int, int > task[maxn];
void read()
{
cin >> n >> q;
for (int i = 1; i <= n; i ++)
{
cin >> seg[i].s >> seg[i].e;
}
for (int i = 1; i <= q; i ++)
{
cin >> task[i].first >> task[i].second;
}
}
vector < int > adj[maxn];
int used[maxn], dist[5010][5010];
int nxt[5010][5010];
int my_queue[maxn];
void bfs(int start)
{
for (int i = 1; i <= n; i ++)
used[i] = -1;
used[start] = 0;
int st = 1, en = 0;
my_queue[++ en] = start;
while(st <= en)
{
int v = my_queue[st ++];
int u = nxt[v][0];
while(u <= n)
{
if (used[u] == - 1)
{
used[u] = used[v] + 1;
my_queue[++ en] = u;
}
assert(nxt[v][u] > u);
u = nxt[v][u];
}
}
}
void build_graph()
{
for (int i = 1; i <= n; i ++)
{
for (int j = 0; j <= n; j ++)
nxt[i][j] = n + 1;
int last = 0;
for (int j = 1; j <= n; j ++)
{
if (seg[j].s <= seg[i].e && seg[i].e <= seg[j].e)
nxt[i][last] = j, last = j;
}
}
}
void answer_queries()
{
for (int i = 1; i <= q; i ++)
{
if (dist[task[i].first][task[i].second] == -1)
cout << "impossible" << endl;
else
cout << dist[task[i].first][task[i].second] << endl;
}
}
void precalc()
{
for (int v = 1; v <= n; v ++)
{
bfs(v);
for (int i = 1; i <= n; i ++)
dist[v][i] = used[i];
}
}
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void solve()
{
read();
build_graph();
precalc();
answer_queries();
}
int main()
{
speed();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Execution timed out |
1564 ms |
103060 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
14 ms |
47964 KB |
Output is correct |
4 |
Correct |
13 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
48040 KB |
Output is correct |
6 |
Correct |
112 ms |
48032 KB |
Output is correct |
7 |
Correct |
321 ms |
48008 KB |
Output is correct |
8 |
Correct |
392 ms |
48216 KB |
Output is correct |
9 |
Execution timed out |
1530 ms |
41876 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
14 ms |
47964 KB |
Output is correct |
4 |
Correct |
13 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
48040 KB |
Output is correct |
6 |
Correct |
112 ms |
48032 KB |
Output is correct |
7 |
Correct |
321 ms |
48008 KB |
Output is correct |
8 |
Correct |
392 ms |
48216 KB |
Output is correct |
9 |
Execution timed out |
1530 ms |
41876 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
14 ms |
47964 KB |
Output is correct |
4 |
Correct |
13 ms |
47964 KB |
Output is correct |
5 |
Correct |
22 ms |
48040 KB |
Output is correct |
6 |
Correct |
112 ms |
48032 KB |
Output is correct |
7 |
Correct |
321 ms |
48008 KB |
Output is correct |
8 |
Correct |
392 ms |
48216 KB |
Output is correct |
9 |
Execution timed out |
1530 ms |
41876 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1601 ms |
105096 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Execution timed out |
1564 ms |
103060 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |