#include<bits/stdc++.h>
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 input()
{
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;
}
}
bool cmp_end(interval seg1, interval seg2)
{
return seg1.e < seg2.e;
}
int lf[maxn], rev[maxn], rf[maxn];
map < int, int > fix;
void build_graph()
{
sort(seg + 1, seg + n + 1, cmp_end);
for (int i = 1; i <= n; i ++)
{
rev[seg[i].idx] = i;
}
for (int i = 1; i <= n; i ++)
{
int l = 1;
int r = i;
while(l <= r)
{
int m = (l + r) / 2;
if (seg[m].e < seg[i].s)
l = m + 1;
else
r = m - 1;
}
lf[i] = l;
//while(seg[lf[i]].e < seg[i].s)
// lf[i] ++;
if (fix[seg[i].e] == 0)
fix[seg[i].e] = lf[i];
else
fix[seg[i].e] = min(fix[seg[i].e], lf[i]);
}
}
void answer_queries()
{
/**cout << "----------------------" << endl;
for (int i = 1; i <= n; i ++)
cout << seg[i].s << " :: " << seg[i].e << endl;*/
for (int i = 1; i <= q; i ++)
{
int st = rev[task[i].first], en = rev[task[i].second];
if (seg[st].e == seg[en].e && st != en)
{
cout << 1 << endl;
continue;
}
if (st > en)
{
cout << "impossible" << endl;
continue;
}
int steps = 0, last_left = en + 1;
int bound_left = en;
bool done = false;
while(!(st >= bound_left))
{
///cout << last_left << " " << bound_left << endl;
steps ++;
int new_left = bound_left;
for (int i = bound_left; i < last_left; i ++)
{
new_left = min(new_left, lf[i]);
}
///cout << "here " << lf[en] << " " << fix[seg[en].e] << " " << seg[en].s << endl;
if (steps == 2)
new_left = min(new_left, fix[seg[en].e]);
///cout << new_bound << endl;
if (new_left == bound_left && steps > 1)
{
done = true;
break;
}
last_left = bound_left;
bound_left = new_left;
}
if (done)
cout << "impossible" << endl;
else
cout << steps << endl;
}
}
void solve()
{
input();
build_graph();
answer_queries();
}
int main()
{
///speed();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Execution timed out |
1582 ms |
7936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
920 ms |
3044 KB |
Output is correct |
20 |
Execution timed out |
1563 ms |
3156 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
115 ms |
7972 KB |
Output is correct |
20 |
Correct |
76 ms |
9044 KB |
Output is correct |
21 |
Correct |
83 ms |
7288 KB |
Output is correct |
22 |
Correct |
87 ms |
9780 KB |
Output is correct |
23 |
Correct |
88 ms |
9960 KB |
Output is correct |
24 |
Correct |
95 ms |
9908 KB |
Output is correct |
25 |
Correct |
51 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1552 ms |
8276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Execution timed out |
1582 ms |
7936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |