#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct interval
{
int s, e, idx;
}seg[maxn];
struct query
{
int start, finish, idx;
}task[maxn];
bool cmp_finish(query q1, query q2)
{
return q1.finish < q2.finish;
}
int n, q;
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].start >> task[i].finish;
task[i].idx = i;
}
}
bool cmp_end(interval seg1, interval seg2)
{
return seg1.e < seg2.e;
}
int lf[maxn], rev[maxn], rf[maxn], ans[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()
{
for (int i = 1; i <= q; i ++)
{
task[i].start = rev[task[i].start];
task[i].finish = rev[task[i].finish];
}
sort(task + 1, task + q + 1, cmp_finish);
/**cout << "----------------------" << endl;
for (int i = 1; i <= n; i ++)
cout << seg[i].s << " :: " << seg[i].e << endl;*/
vector < int > viable;
for (int i = 1; i <= n; i ++)
{
}
int pt = 1;
for (int i = 1; i <= q; i ++)
{
int st = task[i].start, en = task[i].finish;
while(pt <= en)
{
while(!viable.empty() && lf[viable.back()] >= lf[pt])
viable.pop_back();
viable.push_back(pt);
pt ++;
}
if (st == en)
{
ans[task[i].idx] = 0;
continue;
}
if (seg[st].e == seg[en].e && st != en)
{
ans[task[i].idx] = 1;
continue;
}
if (st > en)
{
ans[task[i].idx] = -1;
continue;
}
if (st >= lf[en])
{
ans[task[i].idx] = 1;
continue;
}
int to = fix[seg[en].e];
for (int i = lf[en]; i <= en; i ++)
to = min(to, lf[i]);
int steps = 1;
bool done = false;
int cur = viable.size() - 1;
while(to > st)
{
while(cur >= 0 && viable[cur] >= to)
cur --;
cur ++;
if (lf[viable[cur]] >= to)
{
done = true;
break;
}
steps ++;
to = lf[viable[cur]];
}
if (done == true)
{
ans[task[i].idx] = -1;
}
else
{
ans[task[i].idx] = steps + 1;
}
}
for (int i = 1; i <= q; i ++)
{
if (ans[i] != - 1)
cout << ans[i] << endl;
else
cout << "impossible" << 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 |
1589 ms |
9396 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
2904 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 |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
835 ms |
3716 KB |
Output is correct |
20 |
Correct |
1355 ms |
3968 KB |
Output is correct |
21 |
Correct |
387 ms |
5744 KB |
Output is correct |
22 |
Correct |
163 ms |
4948 KB |
Output is correct |
23 |
Correct |
186 ms |
4952 KB |
Output is correct |
24 |
Correct |
162 ms |
4960 KB |
Output is correct |
25 |
Correct |
545 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
2 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 |
112 ms |
8780 KB |
Output is correct |
20 |
Correct |
75 ms |
8912 KB |
Output is correct |
21 |
Correct |
88 ms |
5776 KB |
Output is correct |
22 |
Correct |
108 ms |
8956 KB |
Output is correct |
23 |
Correct |
89 ms |
8276 KB |
Output is correct |
24 |
Correct |
98 ms |
8912 KB |
Output is correct |
25 |
Correct |
45 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1566 ms |
9312 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 |
1589 ms |
9396 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |