#include <bits/stdc++.h>
#define int long long int
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;
const int INF = 1e18;
const int LOGN = 20;
int arr[N];
// pair<int, int> segment_tree[4 * N];
int up[N][LOGN];
int par[N];
// void build(int id, int l, int r)
// {
// if (l == r)
// {
// segment_tree[id] = {arr[l - 1], l - 1};
// return;
// }
// int mid = (l + r) / 2;
// build(2 * id, l, mid);
// build(2 * id + 1, mid + 1, r);
// segment_tree[id] = min(segment_tree[2 * id], segment_tree[2 * id + 1]);
// return;
// }
// pair<int, int> range(int id, int l, int r, int L, int R)
// {
// if (R < l || L > r)
// {
// return {INF, INF};
// }
// if (L <= l && r <= R)
// {
// return segment_tree[id];
// }
// int mid = (l + r) / 2;
// pair<int, int> x = range(2 * id, l, mid, L, R);
// pair<int, int> y = range(2 * id + 1, mid + 1, r, L, R);
// return min(x, y);
// }
struct Seg
{
int siz;
vector<pair<int, int>> v;
void init(int n)
{
siz = 1;
while (siz < n)
siz *= 2;
v.assign(siz * 2, make_pair(INF, -1));
}
pair<int, int> merge(pair<int, int> x, pair<int, int> y)
{
pair<int, int> res;
res.first = min(x.first, y.first);
if (x.first == y.first)
res.second = min(x.second, y.second);
else if (res.first == x.first)
res.second = x.second;
else
res.second = y.second;
return res;
}
void set(int i, pair<int, int> p, int x, int lx, int rx)
{
if (rx - lx == 1)
{
v[x] = merge(p, v[x]);
return;
}
int m = (lx + rx) / 2;
if (i < m)
set(i, p, 2 * x + 1, lx, m);
else
set(i, p, 2 * x + 2, m, rx);
v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
}
void set(int i, pair<int, int> p)
{
set(i, p, 0, 0, siz);
}
pair<int, int> range_mx(int l, int r, int x, int lx, int rx)
{
if (l >= rx || lx >= r)
return make_pair(INF, -1);
if (lx >= l && rx <= r)
return v[x];
int m = (lx + rx) / 2;
return merge(range_mx(l, r, 2 * x + 1, lx, m), range_mx(l, r, 2 * x + 2, m, rx));
}
pair<int, int> range_mx(int l, int r)
{
return range_mx(l, r, 0, 0, siz);
}
};
void solve()
{
int n, q;
cin >> n >> q;
vector<pair<int, int>> v1;
vector<pair<int, pair<int, int>>> v2;
for (int i = 0; i < n; ++i)
{
int l, r;
cin >> l >> r;
v1.push_back({l, r});
v2.push_back({r, {l, i}});
}
int brr[n], crr[n];
sort(v2.begin(), v2.end());
for (int i = 0; i < n; ++i)
{
arr[i] = v2[i].second.first;
brr[v2[i].second.second] = i;
crr[i] = v2[i].second.second;
}
// build(1, 1, n);
Seg st;
st.init(n + 1);
for (int i = n - 1; i >= 0; i--)
{
st.set(i, make_pair(v2[i].second.first, i));
}
for (int i = 0; i < n; ++i)
{
pair<int, pair<int, int>> p = {v2[i].second.first, {-1, -1}};
int it = lower_bound(v2.begin(), v2.end(), p) - v2.begin();
// cout << v2[i].first << ' ' << it << endl;
if (it == i)
{
up[i][0] = par[i] = i;
}
else
{
// auto idx = range(1, 1, n, it + 1, i + 1);
up[i][0] = par[i] = (st.range_mx(it, i)).second;
}
}
for (int j = 1; j < LOGN; ++j)
{
for (int i = 0; i < n; ++i)
{
if (j == 1)
up[i][j] = up[par[i]][j - 1];
else
up[i][j] = up[up[i][j - 1]][j - 1];
// up[j][i] = up[up[j][i - 1]][i - 1];
}
}
while (q--)
{
int s, e;
cin >> s >> e;
s--;
e--;
if (s == e)
{
cout << 0 << '\n';
continue;
}
int ogs = brr[s], oge = brr[e];
if (ogs > oge)
{
if (v1[s].second >= v1[e].first && v1[s].second <= v1[e].second)
{
cout << 1 << '\n';
continue;
}
else
{
cout << "impossible\n";
continue;
}
}
int i = oge;
int mn = v1[s].second;
int ans = 0;
for (int j = LOGN - 1; j >= 0; j--)
{
if (v1[crr[up[i][j]]].first > mn)
{
ans += (1 << j);
i = up[i][j];
}
}
if (v1[crr[i]].first > mn)
{
i = up[i][0];
ans++;
}
ans++;
i = up[i][0];
if (v1[crr[i]].first > mn)
cout << "impossible\n";
else
cout << ans << "\n";
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("prime_subtractorization_input.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int t = 1;
// cin >> t;
for (int tt = 1; tt <= t; ++tt)
{
// cout << "Case #" << tt << ": ";
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
81 ms |
29760 KB |
Output is correct |
3 |
Correct |
91 ms |
29760 KB |
Output is correct |
4 |
Correct |
91 ms |
31036 KB |
Output is correct |
5 |
Correct |
87 ms |
31044 KB |
Output is correct |
6 |
Correct |
80 ms |
31044 KB |
Output is correct |
7 |
Correct |
80 ms |
31044 KB |
Output is correct |
8 |
Correct |
64 ms |
31540 KB |
Output is correct |
9 |
Correct |
64 ms |
31576 KB |
Output is correct |
10 |
Correct |
88 ms |
31300 KB |
Output is correct |
11 |
Correct |
89 ms |
31296 KB |
Output is correct |
12 |
Correct |
60 ms |
30784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
736 KB |
Output is correct |
17 |
Correct |
1 ms |
736 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
23 ms |
3388 KB |
Output is correct |
20 |
Correct |
20 ms |
3376 KB |
Output is correct |
21 |
Correct |
27 ms |
3768 KB |
Output is correct |
22 |
Correct |
19 ms |
3920 KB |
Output is correct |
23 |
Correct |
22 ms |
3620 KB |
Output is correct |
24 |
Correct |
19 ms |
3628 KB |
Output is correct |
25 |
Correct |
19 ms |
3372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
55 ms |
29304 KB |
Output is correct |
20 |
Correct |
52 ms |
28364 KB |
Output is correct |
21 |
Correct |
62 ms |
29000 KB |
Output is correct |
22 |
Correct |
58 ms |
29292 KB |
Output is correct |
23 |
Correct |
51 ms |
29256 KB |
Output is correct |
24 |
Correct |
59 ms |
29136 KB |
Output is correct |
25 |
Correct |
47 ms |
28552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
29764 KB |
Output is correct |
2 |
Correct |
76 ms |
29768 KB |
Output is correct |
3 |
Correct |
83 ms |
30860 KB |
Output is correct |
4 |
Correct |
67 ms |
31556 KB |
Output is correct |
5 |
Correct |
97 ms |
31256 KB |
Output is correct |
6 |
Correct |
106 ms |
31020 KB |
Output is correct |
7 |
Correct |
103 ms |
31036 KB |
Output is correct |
8 |
Correct |
97 ms |
31332 KB |
Output is correct |
9 |
Correct |
57 ms |
29252 KB |
Output is correct |
10 |
Correct |
83 ms |
30828 KB |
Output is correct |
11 |
Correct |
84 ms |
30608 KB |
Output is correct |
12 |
Correct |
90 ms |
30792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
81 ms |
29760 KB |
Output is correct |
3 |
Correct |
91 ms |
29760 KB |
Output is correct |
4 |
Correct |
91 ms |
31036 KB |
Output is correct |
5 |
Correct |
87 ms |
31044 KB |
Output is correct |
6 |
Correct |
80 ms |
31044 KB |
Output is correct |
7 |
Correct |
80 ms |
31044 KB |
Output is correct |
8 |
Correct |
64 ms |
31540 KB |
Output is correct |
9 |
Correct |
64 ms |
31576 KB |
Output is correct |
10 |
Correct |
88 ms |
31300 KB |
Output is correct |
11 |
Correct |
89 ms |
31296 KB |
Output is correct |
12 |
Correct |
60 ms |
30784 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
736 KB |
Output is correct |
29 |
Correct |
1 ms |
736 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
23 ms |
3388 KB |
Output is correct |
32 |
Correct |
20 ms |
3376 KB |
Output is correct |
33 |
Correct |
27 ms |
3768 KB |
Output is correct |
34 |
Correct |
19 ms |
3920 KB |
Output is correct |
35 |
Correct |
22 ms |
3620 KB |
Output is correct |
36 |
Correct |
19 ms |
3628 KB |
Output is correct |
37 |
Correct |
19 ms |
3372 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
55 ms |
29304 KB |
Output is correct |
48 |
Correct |
52 ms |
28364 KB |
Output is correct |
49 |
Correct |
62 ms |
29000 KB |
Output is correct |
50 |
Correct |
58 ms |
29292 KB |
Output is correct |
51 |
Correct |
51 ms |
29256 KB |
Output is correct |
52 |
Correct |
59 ms |
29136 KB |
Output is correct |
53 |
Correct |
47 ms |
28552 KB |
Output is correct |
54 |
Correct |
73 ms |
29764 KB |
Output is correct |
55 |
Correct |
76 ms |
29768 KB |
Output is correct |
56 |
Correct |
83 ms |
30860 KB |
Output is correct |
57 |
Correct |
67 ms |
31556 KB |
Output is correct |
58 |
Correct |
97 ms |
31256 KB |
Output is correct |
59 |
Correct |
106 ms |
31020 KB |
Output is correct |
60 |
Correct |
103 ms |
31036 KB |
Output is correct |
61 |
Correct |
97 ms |
31332 KB |
Output is correct |
62 |
Correct |
57 ms |
29252 KB |
Output is correct |
63 |
Correct |
83 ms |
30828 KB |
Output is correct |
64 |
Correct |
84 ms |
30608 KB |
Output is correct |
65 |
Correct |
90 ms |
30792 KB |
Output is correct |
66 |
Correct |
1 ms |
344 KB |
Output is correct |
67 |
Correct |
77 ms |
31024 KB |
Output is correct |
68 |
Correct |
81 ms |
31084 KB |
Output is correct |
69 |
Correct |
93 ms |
30880 KB |
Output is correct |
70 |
Correct |
89 ms |
31048 KB |
Output is correct |
71 |
Correct |
80 ms |
31048 KB |
Output is correct |
72 |
Correct |
81 ms |
31092 KB |
Output is correct |
73 |
Correct |
70 ms |
31560 KB |
Output is correct |
74 |
Correct |
64 ms |
31556 KB |
Output is correct |
75 |
Correct |
86 ms |
31316 KB |
Output is correct |
76 |
Correct |
88 ms |
31472 KB |
Output is correct |
77 |
Correct |
58 ms |
30788 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
732 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
1 ms |
604 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
22 ms |
3376 KB |
Output is correct |
87 |
Correct |
22 ms |
3376 KB |
Output is correct |
88 |
Correct |
30 ms |
3628 KB |
Output is correct |
89 |
Correct |
19 ms |
3888 KB |
Output is correct |
90 |
Correct |
19 ms |
3724 KB |
Output is correct |
91 |
Correct |
24 ms |
3628 KB |
Output is correct |
92 |
Correct |
19 ms |
3372 KB |
Output is correct |
93 |
Correct |
56 ms |
29348 KB |
Output is correct |
94 |
Correct |
55 ms |
28312 KB |
Output is correct |
95 |
Correct |
67 ms |
29248 KB |
Output is correct |
96 |
Correct |
52 ms |
29244 KB |
Output is correct |
97 |
Correct |
52 ms |
29260 KB |
Output is correct |
98 |
Correct |
59 ms |
29192 KB |
Output is correct |
99 |
Correct |
47 ms |
28484 KB |
Output is correct |
100 |
Correct |
102 ms |
31060 KB |
Output is correct |
101 |
Correct |
104 ms |
31116 KB |
Output is correct |
102 |
Correct |
87 ms |
31300 KB |
Output is correct |
103 |
Correct |
84 ms |
30784 KB |
Output is correct |
104 |
Correct |
80 ms |
30564 KB |
Output is correct |
105 |
Correct |
79 ms |
30784 KB |
Output is correct |
106 |
Correct |
89 ms |
30336 KB |
Output is correct |
107 |
Correct |
105 ms |
30536 KB |
Output is correct |
108 |
Correct |
91 ms |
31160 KB |
Output is correct |
109 |
Correct |
93 ms |
31044 KB |
Output is correct |
110 |
Correct |
104 ms |
31296 KB |
Output is correct |
111 |
Correct |
107 ms |
31040 KB |
Output is correct |