#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, idx;
}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;
seg[i].idx = i;
}
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 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 ++];
for (int u : adj[v])
{
if (used[u] == - 1)
{
used[u] = used[v] + 1;
my_queue[++ en] = u;
}
}
}
}
void build_graph()
{
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
{
if (seg[j].s <= seg[i].e && seg[i].e <= seg[j].e)
adj[i].push_back(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);
}
bool cmp(interval seg1, interval seg2)
{
return seg1.s < seg2.s;
}
const int maxlog = 21;
int dp[maxlog][maxn], rev[maxn];
void solve_no_intersect()
{
sort(seg + 1, seg + n + 1, cmp);
int pt = n;
for (int i = n; i > 0; i --)
{
while(seg[pt].s > seg[i].e)
pt --;
dp[0][i] = pt;
}
for (int j = 1; j < maxlog; j ++)
for (int i = 1; i <= n; i ++)
{
dp[j][i] = dp[j - 1][dp[j - 1][i]];
}
for (int i = 1; i <= n; i ++)
rev[seg[i].idx] = i;
for (int i = 1; i <= q; i ++)
{
int S = task[i].first, E = task[i].second;
S = rev[S];
E = rev[E];
if (S > E)
{
cout << "impossible" << endl;
continue;
}
if (S == E)
{
cout << 0 << endl;
continue;
}
int jump = 0;
for (int bit = maxlog - 1; bit >= 0; bit --)
{
if (dp[bit][S] < E)
{
jump |= (1 << bit);
S = dp[bit][S];
}
}
if (dp[0][S] < E)
cout << "impossible" << endl;
else
cout << jump + 1 << endl;
}
}
void solve()
{
read();
if (n > 1000 || q > 100)
{
solve_no_intersect();
}
else
{
build_graph();
precalc();
answer_queries();
}
}
int main()
{
speed();
solve();
return 0;
}
/**
3 2
1 2
2 3
3 4
1 3
2 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
183 ms |
16412 KB |
Output is correct |
3 |
Correct |
158 ms |
16308 KB |
Output is correct |
4 |
Correct |
154 ms |
16468 KB |
Output is correct |
5 |
Incorrect |
148 ms |
16976 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
13 ms |
25096 KB |
Output is correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
14 ms |
24920 KB |
Output is correct |
6 |
Correct |
48 ms |
25680 KB |
Output is correct |
7 |
Correct |
141 ms |
26668 KB |
Output is correct |
8 |
Correct |
221 ms |
27732 KB |
Output is correct |
9 |
Correct |
939 ms |
29068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
13 ms |
25096 KB |
Output is correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
14 ms |
24920 KB |
Output is correct |
6 |
Correct |
48 ms |
25680 KB |
Output is correct |
7 |
Correct |
141 ms |
26668 KB |
Output is correct |
8 |
Correct |
221 ms |
27732 KB |
Output is correct |
9 |
Correct |
939 ms |
29068 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
12 ms |
24924 KB |
Output is correct |
13 |
Correct |
10 ms |
25100 KB |
Output is correct |
14 |
Correct |
12 ms |
24920 KB |
Output is correct |
15 |
Correct |
48 ms |
25628 KB |
Output is correct |
16 |
Correct |
145 ms |
26672 KB |
Output is correct |
17 |
Correct |
173 ms |
27732 KB |
Output is correct |
18 |
Correct |
1063 ms |
29332 KB |
Output is correct |
19 |
Correct |
126 ms |
13080 KB |
Output is correct |
20 |
Correct |
124 ms |
14168 KB |
Output is correct |
21 |
Incorrect |
132 ms |
14428 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
13 ms |
25096 KB |
Output is correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
14 ms |
24920 KB |
Output is correct |
6 |
Correct |
48 ms |
25680 KB |
Output is correct |
7 |
Correct |
141 ms |
26668 KB |
Output is correct |
8 |
Correct |
221 ms |
27732 KB |
Output is correct |
9 |
Correct |
939 ms |
29068 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
13 ms |
24924 KB |
Output is correct |
13 |
Correct |
10 ms |
24924 KB |
Output is correct |
14 |
Correct |
13 ms |
25096 KB |
Output is correct |
15 |
Correct |
47 ms |
25876 KB |
Output is correct |
16 |
Correct |
145 ms |
26968 KB |
Output is correct |
17 |
Correct |
169 ms |
27736 KB |
Output is correct |
18 |
Correct |
971 ms |
29328 KB |
Output is correct |
19 |
Correct |
27 ms |
15708 KB |
Output is correct |
20 |
Correct |
23 ms |
15704 KB |
Output is correct |
21 |
Incorrect |
27 ms |
15708 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
16308 KB |
Output is correct |
2 |
Correct |
149 ms |
16468 KB |
Output is correct |
3 |
Correct |
172 ms |
16304 KB |
Output is correct |
4 |
Correct |
155 ms |
16980 KB |
Output is correct |
5 |
Correct |
152 ms |
16724 KB |
Output is correct |
6 |
Correct |
155 ms |
16436 KB |
Output is correct |
7 |
Correct |
159 ms |
16420 KB |
Output is correct |
8 |
Correct |
160 ms |
16976 KB |
Output is correct |
9 |
Correct |
29 ms |
15760 KB |
Output is correct |
10 |
Correct |
148 ms |
16208 KB |
Output is correct |
11 |
Correct |
153 ms |
16296 KB |
Output is correct |
12 |
Correct |
153 ms |
16364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
183 ms |
16412 KB |
Output is correct |
3 |
Correct |
158 ms |
16308 KB |
Output is correct |
4 |
Correct |
154 ms |
16468 KB |
Output is correct |
5 |
Incorrect |
148 ms |
16976 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |