#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define rf(i, a, b) for (int i = (a); i >= (b); i--)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
int n, q;
ve<pii> a;
int to[N];
int go[N][20];
ve<int> u;
pii t[4 * N];
void upd(int v, int tl, int tr, int pos, int val, int idx) {
if (tl == tr) {
umn(t[v], mp(val, idx));
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) upd(v << 1, tl, tm, pos, val, idx);
else upd(v << 1 | 1, tm + 1, tr, pos, val, idx);
t[v] = min(t[v << 1], t[v << 1 | 1]);
}
void upd(int pos, int val, int idx) {
upd(1, 0, sz(u) - 1, pos, val, idx);
}
pii get(int v, int tl, int tr, int l, int r) {
if (l > r) {
return mp(int(1e9), 0);
}
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) >> 1;
return min(get(v << 1, tl, tm, l, min(r, tm)),
get(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r));
}
pii get(int l, int r) {
return get(1, 0, sz(u) - 1, l, r);
}
int main() {
#ifndef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> q;
a.resize(n + 1);
fr (i, 1, n) {
cin >> a[i].fi >> a[i].se;
// to[i] = i;
u.pb(a[i].se);
}
sort(all(u));
u.erase(unique(all(u)), u.end());
fill(t, t + 4 * N, mp(int(1e9), 0));
fr (i, 1, n) {
int id = lower_bound(all(u), a[i].se) - u.begin();
upd(id, a[i].fi, i);
}
fr (i, 1, n) {
int L = lower_bound(all(u), a[i].fi) - u.begin();
int R = upper_bound(all(u), a[i].se) - u.begin() - 1;
to[i] = get(L, R).se;
}
// fr (i, 1, n) {
// fr (j, 1, n) {
// if (i == j) continue;
// if (a[j].se >= a[i].fi && a[j].se <= a[i].se) {
// if (!to[i] || a[to[i]].fi > a[j].fi) {
// to[i] = j;
// }
// }
// }
// }
fr (i, 1, n) {
go[i][0] = to[i];
}
fr (p, 1, 19) {
fr (i, 1, n) {
go[i][p] = go[go[i][p - 1]][p - 1];
}
}
fr (i, 0, q - 1) {
int s, e;
cin >> s >> e;
if (a[s].se > a[e].se) {
cout << "impossible\n";
continue;
}
if (s == e) {
cout << "0\n";
continue;
}
int v = e;
int ans = 0;
rf (p, 19, 0) {
if (go[v][p] && a[go[v][p]].fi > a[s].se) {
ans += pw(p);
v = go[v][p];
}
}
if (to[v] && a[v].fi > a[s].se) {
v = to[v];
ans++;
}
if (a[v].fi <= a[s].se) {
ans++;
cout << ans << "\n";
} else {
cout << "impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
134 ms |
14444 KB |
Output is correct |
3 |
Correct |
138 ms |
16652 KB |
Output is correct |
4 |
Correct |
175 ms |
16772 KB |
Output is correct |
5 |
Correct |
141 ms |
16660 KB |
Output is correct |
6 |
Correct |
139 ms |
16656 KB |
Output is correct |
7 |
Correct |
143 ms |
16780 KB |
Output is correct |
8 |
Correct |
129 ms |
17164 KB |
Output is correct |
9 |
Correct |
127 ms |
17064 KB |
Output is correct |
10 |
Correct |
144 ms |
17040 KB |
Output is correct |
11 |
Correct |
147 ms |
17200 KB |
Output is correct |
12 |
Correct |
83 ms |
16460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
2 ms |
3464 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
2 ms |
3464 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
10 |
Correct |
3 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
2 ms |
3540 KB |
Output is correct |
17 |
Correct |
2 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3540 KB |
Output is correct |
19 |
Correct |
31 ms |
4456 KB |
Output is correct |
20 |
Correct |
30 ms |
4360 KB |
Output is correct |
21 |
Correct |
40 ms |
4612 KB |
Output is correct |
22 |
Correct |
26 ms |
4728 KB |
Output is correct |
23 |
Correct |
29 ms |
4572 KB |
Output is correct |
24 |
Correct |
25 ms |
4564 KB |
Output is correct |
25 |
Correct |
27 ms |
4264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
2 ms |
3464 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3540 KB |
Output is correct |
17 |
Correct |
2 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3540 KB |
Output is correct |
19 |
Correct |
114 ms |
12860 KB |
Output is correct |
20 |
Correct |
108 ms |
14216 KB |
Output is correct |
21 |
Correct |
108 ms |
14716 KB |
Output is correct |
22 |
Correct |
110 ms |
14888 KB |
Output is correct |
23 |
Correct |
91 ms |
14844 KB |
Output is correct |
24 |
Correct |
123 ms |
14836 KB |
Output is correct |
25 |
Correct |
33 ms |
14156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
14504 KB |
Output is correct |
2 |
Correct |
135 ms |
16640 KB |
Output is correct |
3 |
Correct |
162 ms |
16660 KB |
Output is correct |
4 |
Correct |
126 ms |
17104 KB |
Output is correct |
5 |
Correct |
149 ms |
17120 KB |
Output is correct |
6 |
Correct |
156 ms |
16716 KB |
Output is correct |
7 |
Correct |
165 ms |
16776 KB |
Output is correct |
8 |
Correct |
140 ms |
16944 KB |
Output is correct |
9 |
Correct |
103 ms |
14820 KB |
Output is correct |
10 |
Correct |
142 ms |
16400 KB |
Output is correct |
11 |
Correct |
137 ms |
16240 KB |
Output is correct |
12 |
Correct |
147 ms |
16420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
134 ms |
14444 KB |
Output is correct |
3 |
Correct |
138 ms |
16652 KB |
Output is correct |
4 |
Correct |
175 ms |
16772 KB |
Output is correct |
5 |
Correct |
141 ms |
16660 KB |
Output is correct |
6 |
Correct |
139 ms |
16656 KB |
Output is correct |
7 |
Correct |
143 ms |
16780 KB |
Output is correct |
8 |
Correct |
129 ms |
17164 KB |
Output is correct |
9 |
Correct |
127 ms |
17064 KB |
Output is correct |
10 |
Correct |
144 ms |
17040 KB |
Output is correct |
11 |
Correct |
147 ms |
17200 KB |
Output is correct |
12 |
Correct |
83 ms |
16460 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3540 KB |
Output is correct |
17 |
Correct |
2 ms |
3464 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3540 KB |
Output is correct |
25 |
Correct |
3 ms |
3540 KB |
Output is correct |
26 |
Correct |
3 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3540 KB |
Output is correct |
30 |
Correct |
2 ms |
3540 KB |
Output is correct |
31 |
Correct |
31 ms |
4456 KB |
Output is correct |
32 |
Correct |
30 ms |
4360 KB |
Output is correct |
33 |
Correct |
40 ms |
4612 KB |
Output is correct |
34 |
Correct |
26 ms |
4728 KB |
Output is correct |
35 |
Correct |
29 ms |
4572 KB |
Output is correct |
36 |
Correct |
25 ms |
4564 KB |
Output is correct |
37 |
Correct |
27 ms |
4264 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
2 ms |
3412 KB |
Output is correct |
40 |
Correct |
2 ms |
3540 KB |
Output is correct |
41 |
Correct |
2 ms |
3540 KB |
Output is correct |
42 |
Correct |
3 ms |
3540 KB |
Output is correct |
43 |
Correct |
3 ms |
3540 KB |
Output is correct |
44 |
Correct |
3 ms |
3540 KB |
Output is correct |
45 |
Correct |
2 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3540 KB |
Output is correct |
47 |
Correct |
114 ms |
12860 KB |
Output is correct |
48 |
Correct |
108 ms |
14216 KB |
Output is correct |
49 |
Correct |
108 ms |
14716 KB |
Output is correct |
50 |
Correct |
110 ms |
14888 KB |
Output is correct |
51 |
Correct |
91 ms |
14844 KB |
Output is correct |
52 |
Correct |
123 ms |
14836 KB |
Output is correct |
53 |
Correct |
33 ms |
14156 KB |
Output is correct |
54 |
Correct |
128 ms |
14504 KB |
Output is correct |
55 |
Correct |
135 ms |
16640 KB |
Output is correct |
56 |
Correct |
162 ms |
16660 KB |
Output is correct |
57 |
Correct |
126 ms |
17104 KB |
Output is correct |
58 |
Correct |
149 ms |
17120 KB |
Output is correct |
59 |
Correct |
156 ms |
16716 KB |
Output is correct |
60 |
Correct |
165 ms |
16776 KB |
Output is correct |
61 |
Correct |
140 ms |
16944 KB |
Output is correct |
62 |
Correct |
103 ms |
14820 KB |
Output is correct |
63 |
Correct |
142 ms |
16400 KB |
Output is correct |
64 |
Correct |
137 ms |
16240 KB |
Output is correct |
65 |
Correct |
147 ms |
16420 KB |
Output is correct |
66 |
Correct |
2 ms |
3412 KB |
Output is correct |
67 |
Correct |
131 ms |
16652 KB |
Output is correct |
68 |
Correct |
136 ms |
16604 KB |
Output is correct |
69 |
Correct |
163 ms |
16612 KB |
Output is correct |
70 |
Correct |
145 ms |
16656 KB |
Output is correct |
71 |
Correct |
140 ms |
16716 KB |
Output is correct |
72 |
Correct |
143 ms |
16732 KB |
Output is correct |
73 |
Correct |
130 ms |
17172 KB |
Output is correct |
74 |
Correct |
139 ms |
17060 KB |
Output is correct |
75 |
Correct |
143 ms |
17036 KB |
Output is correct |
76 |
Correct |
148 ms |
17052 KB |
Output is correct |
77 |
Correct |
89 ms |
16332 KB |
Output is correct |
78 |
Correct |
2 ms |
3412 KB |
Output is correct |
79 |
Correct |
3 ms |
3540 KB |
Output is correct |
80 |
Correct |
2 ms |
3540 KB |
Output is correct |
81 |
Correct |
3 ms |
3540 KB |
Output is correct |
82 |
Correct |
3 ms |
3540 KB |
Output is correct |
83 |
Correct |
3 ms |
3552 KB |
Output is correct |
84 |
Correct |
2 ms |
3540 KB |
Output is correct |
85 |
Correct |
2 ms |
3540 KB |
Output is correct |
86 |
Correct |
34 ms |
5324 KB |
Output is correct |
87 |
Correct |
30 ms |
5480 KB |
Output is correct |
88 |
Correct |
37 ms |
5708 KB |
Output is correct |
89 |
Correct |
27 ms |
5696 KB |
Output is correct |
90 |
Correct |
27 ms |
5552 KB |
Output is correct |
91 |
Correct |
26 ms |
5588 KB |
Output is correct |
92 |
Correct |
29 ms |
5196 KB |
Output is correct |
93 |
Correct |
104 ms |
14912 KB |
Output is correct |
94 |
Correct |
101 ms |
14068 KB |
Output is correct |
95 |
Correct |
112 ms |
14708 KB |
Output is correct |
96 |
Correct |
111 ms |
15060 KB |
Output is correct |
97 |
Correct |
97 ms |
14844 KB |
Output is correct |
98 |
Correct |
111 ms |
14812 KB |
Output is correct |
99 |
Correct |
31 ms |
14236 KB |
Output is correct |
100 |
Correct |
151 ms |
16792 KB |
Output is correct |
101 |
Correct |
148 ms |
16732 KB |
Output is correct |
102 |
Correct |
145 ms |
16928 KB |
Output is correct |
103 |
Correct |
145 ms |
16396 KB |
Output is correct |
104 |
Correct |
153 ms |
16284 KB |
Output is correct |
105 |
Correct |
162 ms |
16452 KB |
Output is correct |
106 |
Correct |
143 ms |
16064 KB |
Output is correct |
107 |
Correct |
153 ms |
16056 KB |
Output is correct |
108 |
Correct |
139 ms |
16724 KB |
Output is correct |
109 |
Correct |
138 ms |
16724 KB |
Output is correct |
110 |
Correct |
165 ms |
16692 KB |
Output is correct |
111 |
Correct |
183 ms |
16756 KB |
Output is correct |