#include <algorithm>
#include <iostream>
#include <numeric>
const int MAXN = 100000 + 10;
const int MAXLOG = 17;
const int INF = 1e9 + 10;
struct Event
{
int s, e;
inline friend bool operator < (Event a, Event b)
{
return a.s < b.s || (a.s == b.s && a.e < b.e);
}
} ev[MAXN];
int n, q;
int getLog[MAXN];
int rmq[MAXLOG][MAXN];
int lift[MAXLOG][MAXN];
int perm[MAXN], newNum[MAXN];
int cmp(int x, int y)
{
if (ev[x].e >= ev[y].e) return x;
return y;
}
void buildRMQ()
{
std::iota(rmq[0]+1, rmq[0]+1+n, 1);
for (int log = 1 ; (1 << log) <= n ; ++log)
{
for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
{
rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
}
}
for (int i = 1 ; i <= n ; ++i)
{
getLog[i] = getLog[i-1];
if ((1 << getLog[i]+1) < i) ++getLog[i];
}
}
int upperBinSearch(int value)
{
int l = 0, r = n+1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (ev[mid].s <= value) l = mid;
else r = mid;
}
return l;
}
int lowerBinSearch(int idx)
{
int l = 0, r = n, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (mid <= idx || ev[mid].s < ev[idx].s) l = mid;
else r = mid;
}
return r;
}
int findMaximum(int l, int r)
{
int log = getLog[r-l+1];
return cmp(rmq[log][l], rmq[log][r - (1 << log) + 1]);
}
void buildLift()
{
for (int log = 1 ; (1 << log) <= n ; ++log)
{
for (int i = 1 ; i <= n ; ++i)
{
lift[log][i] = lift[log-1][lift[log-1][i]];
}
}
}
int binaryLifting(int from, int value)
{
if (ev[from].s > value) return -1;
int ans = 1 + (ev[from].e < value);
for (int log = MAXLOG-1 ; log >= 0 ; --log)
{
if (lift[log][from] == 0) continue;
if (ev[lift[log][from]].e < value)
{
ans += (1 << log);
from = lift[log][from];
}
}
if (ev[lift[0][from]].e < value) return -1;
else return ans;
}
void solve()
{
std::iota(perm+1, perm+1+n, 1);
std::sort(perm+1, perm+1+n, [&](int x, int y)
{
return ev[x] < ev[y];
});
for (int i = 1 ; i <= n ; ++i)
{
newNum[perm[i]] = i;
}
std::sort(ev+1, ev+1+n);
buildRMQ();
for (int i = 1 ; i <= n ; ++i)
{
int from = lowerBinSearch(i);
int to = upperBinSearch(ev[i].e);
if (i+1 <= from && from <= to)
{
int jump = findMaximum(from, to);
lift[0][i] = jump;
if (i == jump) lift[0][i] = 0;
}
}
buildLift();
for (int i = 1 ; i <= q ; ++i)
{
int st, en;
std::cin >> st >> en;
std::swap(st, en);
if (st == en)
{
std::cout << 0 << '\n';
continue;
}
st = newNum[st];
en = ev[newNum[en]].s;
int res = binaryLifting(st, en);
if (res == -1) std::cout << "impossible\n";
else std::cout << res << '\n';
}
}
void read()
{
std::cin >> n >> q;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> ev[i].s >> ev[i].e;
ev[i].s = INF - ev[i].s;
ev[i].e = INF - ev[i].e;
std::swap(ev[i].s, ev[i].e);
}
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
Compilation message
events.cpp: In function 'void buildRMQ()':
events.cpp:37:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
37 | rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
| ~~~^~
events.cpp:44:28: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
44 | if ((1 << getLog[i]+1) < i) ++getLog[i];
| ~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
89 ms |
18456 KB |
Output is correct |
3 |
Correct |
83 ms |
18620 KB |
Output is correct |
4 |
Correct |
89 ms |
18512 KB |
Output is correct |
5 |
Correct |
98 ms |
18660 KB |
Output is correct |
6 |
Correct |
105 ms |
18648 KB |
Output is correct |
7 |
Correct |
82 ms |
18768 KB |
Output is correct |
8 |
Correct |
74 ms |
18752 KB |
Output is correct |
9 |
Correct |
87 ms |
18556 KB |
Output is correct |
10 |
Correct |
113 ms |
18948 KB |
Output is correct |
11 |
Correct |
99 ms |
18984 KB |
Output is correct |
12 |
Correct |
65 ms |
17672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
480 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
532 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
480 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
32 ms |
2348 KB |
Output is correct |
20 |
Correct |
36 ms |
2420 KB |
Output is correct |
21 |
Correct |
30 ms |
2724 KB |
Output is correct |
22 |
Incorrect |
25 ms |
2796 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
476 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
56 ms |
16588 KB |
Output is correct |
20 |
Correct |
50 ms |
16124 KB |
Output is correct |
21 |
Correct |
62 ms |
16632 KB |
Output is correct |
22 |
Correct |
65 ms |
16568 KB |
Output is correct |
23 |
Correct |
66 ms |
16520 KB |
Output is correct |
24 |
Correct |
56 ms |
16712 KB |
Output is correct |
25 |
Correct |
38 ms |
16240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
18420 KB |
Output is correct |
2 |
Correct |
79 ms |
18476 KB |
Output is correct |
3 |
Correct |
94 ms |
18404 KB |
Output is correct |
4 |
Correct |
79 ms |
18552 KB |
Output is correct |
5 |
Correct |
111 ms |
18820 KB |
Output is correct |
6 |
Correct |
123 ms |
18448 KB |
Output is correct |
7 |
Correct |
114 ms |
18636 KB |
Output is correct |
8 |
Correct |
95 ms |
18880 KB |
Output is correct |
9 |
Correct |
54 ms |
16872 KB |
Output is correct |
10 |
Correct |
87 ms |
18340 KB |
Output is correct |
11 |
Correct |
89 ms |
18040 KB |
Output is correct |
12 |
Correct |
83 ms |
18252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
89 ms |
18456 KB |
Output is correct |
3 |
Correct |
83 ms |
18620 KB |
Output is correct |
4 |
Correct |
89 ms |
18512 KB |
Output is correct |
5 |
Correct |
98 ms |
18660 KB |
Output is correct |
6 |
Correct |
105 ms |
18648 KB |
Output is correct |
7 |
Correct |
82 ms |
18768 KB |
Output is correct |
8 |
Correct |
74 ms |
18752 KB |
Output is correct |
9 |
Correct |
87 ms |
18556 KB |
Output is correct |
10 |
Correct |
113 ms |
18948 KB |
Output is correct |
11 |
Correct |
99 ms |
18984 KB |
Output is correct |
12 |
Correct |
65 ms |
17672 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
528 KB |
Output is correct |
18 |
Correct |
1 ms |
476 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
532 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
484 KB |
Output is correct |
29 |
Correct |
1 ms |
480 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
32 ms |
2348 KB |
Output is correct |
32 |
Correct |
36 ms |
2420 KB |
Output is correct |
33 |
Correct |
30 ms |
2724 KB |
Output is correct |
34 |
Incorrect |
25 ms |
2796 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |