#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 4;
const int INF = 1e9;
const int N = 18;
pair<int,int> a[maxn];
struct seg_tree {
vector<int> t;
seg_tree() {
t.resize(4 * maxn);
}
int MIN(int i, int j) {
return (a[i].fi < a[j].fi ? i : j);
}
void update(int pos, int val, int v = 1, int l = 1, int r = maxn - 1) {
if (l == r) {
t[v] = MIN(t[v], val);
return;
}
int m = (l + r) / 2;
if (pos <= m) update(pos, val, 2 * v, l, m);
else update(pos, val, 2 * v + 1, m + 1, r);
t[v] = MIN(t[2 * v], t[2 * v + 1]);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[v];
int m = (tl + tr) / 2;
int L = get(l, r, 2 * v, tl, m);
int R = get(l, r, 2 * v + 1, m + 1, tr);
return MIN(L, R);
}
} IT;
int n, q, pre[maxn][20];
vector<int> vals;
vector<pair<int,int>> R[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
a[i] = {l, r};
vals.pb(l); vals.pb(r);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
a[0] = {INF, INF};
for (int i = 1; i <= n; i++) {
a[i].fi = lower_bound(vals.begin(), vals.end(), a[i].fi) - vals.begin() + 1;
a[i].se = lower_bound(vals.begin(), vals.end(), a[i].se) - vals.begin() + 1;
R[a[i].se].pb({a[i].fi, i});
}
int sz = vals.size();
for (int i = 1; i <= sz; i++) {
sort(R[i].begin(), R[i].end());
for (auto it : R[i]) {
pre[it.se][0] = IT.get(it.fi, i);
IT.update(i, it.se);
}
}
for (int j = 1; j <= N; j++)
for (int i = n; i >= 1; i--)
pre[i][j] = pre[pre[i][j - 1]][j - 1];
a[0] = {-INF, -INF};
while (q--) {
int s, t; cin >> s >> t;
if (s == t) {
cout << "0\n";
continue;
}
int ans = 0;
for (int j = N; j >= 0; j--)
if (a[pre[t][j]].fi > a[s].se && pre[t][j] != s) {
ans += (1 << j);
t = pre[t][j];
}
if (a[t].se < a[s].se) ans = -1;
else if (a[t].fi > a[s].se) {
if (!pre[t][0]) ans = -1;
else ans++;
}
if (ans == -1) cout << "impossible";
else cout << ans + 1;
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
149 ms |
21304 KB |
Output is correct |
3 |
Correct |
140 ms |
21180 KB |
Output is correct |
4 |
Correct |
170 ms |
21260 KB |
Output is correct |
5 |
Correct |
151 ms |
22152 KB |
Output is correct |
6 |
Correct |
179 ms |
24228 KB |
Output is correct |
7 |
Correct |
153 ms |
24344 KB |
Output is correct |
8 |
Correct |
162 ms |
24652 KB |
Output is correct |
9 |
Correct |
148 ms |
24800 KB |
Output is correct |
10 |
Correct |
172 ms |
24648 KB |
Output is correct |
11 |
Correct |
163 ms |
24688 KB |
Output is correct |
12 |
Correct |
98 ms |
22512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8088 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
6 ms |
8236 KB |
Output is correct |
4 |
Correct |
6 ms |
8276 KB |
Output is correct |
5 |
Correct |
6 ms |
8308 KB |
Output is correct |
6 |
Correct |
4 ms |
8276 KB |
Output is correct |
7 |
Correct |
4 ms |
8232 KB |
Output is correct |
8 |
Correct |
5 ms |
8276 KB |
Output is correct |
9 |
Correct |
4 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8088 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
6 ms |
8236 KB |
Output is correct |
4 |
Correct |
6 ms |
8276 KB |
Output is correct |
5 |
Correct |
6 ms |
8308 KB |
Output is correct |
6 |
Correct |
4 ms |
8276 KB |
Output is correct |
7 |
Correct |
4 ms |
8232 KB |
Output is correct |
8 |
Correct |
5 ms |
8276 KB |
Output is correct |
9 |
Correct |
4 ms |
8276 KB |
Output is correct |
10 |
Correct |
5 ms |
8172 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
6 ms |
8276 KB |
Output is correct |
13 |
Correct |
6 ms |
8276 KB |
Output is correct |
14 |
Correct |
4 ms |
8220 KB |
Output is correct |
15 |
Correct |
5 ms |
8260 KB |
Output is correct |
16 |
Correct |
5 ms |
8224 KB |
Output is correct |
17 |
Correct |
5 ms |
8228 KB |
Output is correct |
18 |
Correct |
4 ms |
8276 KB |
Output is correct |
19 |
Correct |
38 ms |
9208 KB |
Output is correct |
20 |
Correct |
32 ms |
9288 KB |
Output is correct |
21 |
Correct |
36 ms |
9532 KB |
Output is correct |
22 |
Correct |
33 ms |
9436 KB |
Output is correct |
23 |
Correct |
40 ms |
9400 KB |
Output is correct |
24 |
Correct |
32 ms |
9428 KB |
Output is correct |
25 |
Correct |
33 ms |
9112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8088 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
6 ms |
8236 KB |
Output is correct |
4 |
Correct |
6 ms |
8276 KB |
Output is correct |
5 |
Correct |
6 ms |
8308 KB |
Output is correct |
6 |
Correct |
4 ms |
8276 KB |
Output is correct |
7 |
Correct |
4 ms |
8232 KB |
Output is correct |
8 |
Correct |
5 ms |
8276 KB |
Output is correct |
9 |
Correct |
4 ms |
8276 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
5 ms |
8276 KB |
Output is correct |
13 |
Correct |
6 ms |
8276 KB |
Output is correct |
14 |
Correct |
6 ms |
8276 KB |
Output is correct |
15 |
Correct |
5 ms |
8276 KB |
Output is correct |
16 |
Correct |
4 ms |
8276 KB |
Output is correct |
17 |
Correct |
4 ms |
8276 KB |
Output is correct |
18 |
Correct |
4 ms |
8276 KB |
Output is correct |
19 |
Correct |
106 ms |
20676 KB |
Output is correct |
20 |
Correct |
103 ms |
20608 KB |
Output is correct |
21 |
Correct |
119 ms |
19284 KB |
Output is correct |
22 |
Correct |
111 ms |
22572 KB |
Output is correct |
23 |
Correct |
121 ms |
22708 KB |
Output is correct |
24 |
Correct |
126 ms |
22576 KB |
Output is correct |
25 |
Correct |
50 ms |
19676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
21276 KB |
Output is correct |
2 |
Correct |
141 ms |
21248 KB |
Output is correct |
3 |
Correct |
166 ms |
21228 KB |
Output is correct |
4 |
Correct |
144 ms |
22588 KB |
Output is correct |
5 |
Correct |
158 ms |
24516 KB |
Output is correct |
6 |
Correct |
166 ms |
24288 KB |
Output is correct |
7 |
Correct |
204 ms |
24256 KB |
Output is correct |
8 |
Correct |
156 ms |
24392 KB |
Output is correct |
9 |
Correct |
125 ms |
22540 KB |
Output is correct |
10 |
Correct |
200 ms |
24000 KB |
Output is correct |
11 |
Correct |
178 ms |
23736 KB |
Output is correct |
12 |
Correct |
164 ms |
24004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
149 ms |
21304 KB |
Output is correct |
3 |
Correct |
140 ms |
21180 KB |
Output is correct |
4 |
Correct |
170 ms |
21260 KB |
Output is correct |
5 |
Correct |
151 ms |
22152 KB |
Output is correct |
6 |
Correct |
179 ms |
24228 KB |
Output is correct |
7 |
Correct |
153 ms |
24344 KB |
Output is correct |
8 |
Correct |
162 ms |
24652 KB |
Output is correct |
9 |
Correct |
148 ms |
24800 KB |
Output is correct |
10 |
Correct |
172 ms |
24648 KB |
Output is correct |
11 |
Correct |
163 ms |
24688 KB |
Output is correct |
12 |
Correct |
98 ms |
22512 KB |
Output is correct |
13 |
Correct |
5 ms |
8088 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
6 ms |
8236 KB |
Output is correct |
16 |
Correct |
6 ms |
8276 KB |
Output is correct |
17 |
Correct |
6 ms |
8308 KB |
Output is correct |
18 |
Correct |
4 ms |
8276 KB |
Output is correct |
19 |
Correct |
4 ms |
8232 KB |
Output is correct |
20 |
Correct |
5 ms |
8276 KB |
Output is correct |
21 |
Correct |
4 ms |
8276 KB |
Output is correct |
22 |
Correct |
5 ms |
8172 KB |
Output is correct |
23 |
Correct |
4 ms |
8148 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Correct |
6 ms |
8276 KB |
Output is correct |
26 |
Correct |
4 ms |
8220 KB |
Output is correct |
27 |
Correct |
5 ms |
8260 KB |
Output is correct |
28 |
Correct |
5 ms |
8224 KB |
Output is correct |
29 |
Correct |
5 ms |
8228 KB |
Output is correct |
30 |
Correct |
4 ms |
8276 KB |
Output is correct |
31 |
Correct |
38 ms |
9208 KB |
Output is correct |
32 |
Correct |
32 ms |
9288 KB |
Output is correct |
33 |
Correct |
36 ms |
9532 KB |
Output is correct |
34 |
Correct |
33 ms |
9436 KB |
Output is correct |
35 |
Correct |
40 ms |
9400 KB |
Output is correct |
36 |
Correct |
32 ms |
9428 KB |
Output is correct |
37 |
Correct |
33 ms |
9112 KB |
Output is correct |
38 |
Correct |
3 ms |
8148 KB |
Output is correct |
39 |
Correct |
4 ms |
8148 KB |
Output is correct |
40 |
Correct |
5 ms |
8276 KB |
Output is correct |
41 |
Correct |
6 ms |
8276 KB |
Output is correct |
42 |
Correct |
6 ms |
8276 KB |
Output is correct |
43 |
Correct |
5 ms |
8276 KB |
Output is correct |
44 |
Correct |
4 ms |
8276 KB |
Output is correct |
45 |
Correct |
4 ms |
8276 KB |
Output is correct |
46 |
Correct |
4 ms |
8276 KB |
Output is correct |
47 |
Correct |
106 ms |
20676 KB |
Output is correct |
48 |
Correct |
103 ms |
20608 KB |
Output is correct |
49 |
Correct |
119 ms |
19284 KB |
Output is correct |
50 |
Correct |
111 ms |
22572 KB |
Output is correct |
51 |
Correct |
121 ms |
22708 KB |
Output is correct |
52 |
Correct |
126 ms |
22576 KB |
Output is correct |
53 |
Correct |
50 ms |
19676 KB |
Output is correct |
54 |
Correct |
130 ms |
21276 KB |
Output is correct |
55 |
Correct |
141 ms |
21248 KB |
Output is correct |
56 |
Correct |
166 ms |
21228 KB |
Output is correct |
57 |
Correct |
144 ms |
22588 KB |
Output is correct |
58 |
Correct |
158 ms |
24516 KB |
Output is correct |
59 |
Correct |
166 ms |
24288 KB |
Output is correct |
60 |
Correct |
204 ms |
24256 KB |
Output is correct |
61 |
Correct |
156 ms |
24392 KB |
Output is correct |
62 |
Correct |
125 ms |
22540 KB |
Output is correct |
63 |
Correct |
200 ms |
24000 KB |
Output is correct |
64 |
Correct |
178 ms |
23736 KB |
Output is correct |
65 |
Correct |
164 ms |
24004 KB |
Output is correct |
66 |
Correct |
4 ms |
8148 KB |
Output is correct |
67 |
Correct |
147 ms |
24392 KB |
Output is correct |
68 |
Correct |
177 ms |
24308 KB |
Output is correct |
69 |
Correct |
174 ms |
24260 KB |
Output is correct |
70 |
Correct |
149 ms |
24476 KB |
Output is correct |
71 |
Correct |
167 ms |
24428 KB |
Output is correct |
72 |
Correct |
149 ms |
24540 KB |
Output is correct |
73 |
Correct |
134 ms |
24776 KB |
Output is correct |
74 |
Correct |
164 ms |
24768 KB |
Output is correct |
75 |
Correct |
168 ms |
24648 KB |
Output is correct |
76 |
Correct |
150 ms |
24752 KB |
Output is correct |
77 |
Correct |
115 ms |
22504 KB |
Output is correct |
78 |
Correct |
4 ms |
8072 KB |
Output is correct |
79 |
Correct |
5 ms |
8276 KB |
Output is correct |
80 |
Correct |
5 ms |
8324 KB |
Output is correct |
81 |
Correct |
5 ms |
8192 KB |
Output is correct |
82 |
Correct |
5 ms |
8276 KB |
Output is correct |
83 |
Correct |
4 ms |
8276 KB |
Output is correct |
84 |
Correct |
4 ms |
8276 KB |
Output is correct |
85 |
Correct |
5 ms |
8276 KB |
Output is correct |
86 |
Correct |
35 ms |
10276 KB |
Output is correct |
87 |
Correct |
45 ms |
10328 KB |
Output is correct |
88 |
Correct |
38 ms |
10444 KB |
Output is correct |
89 |
Correct |
32 ms |
10572 KB |
Output is correct |
90 |
Correct |
35 ms |
10376 KB |
Output is correct |
91 |
Correct |
37 ms |
10432 KB |
Output is correct |
92 |
Correct |
33 ms |
10024 KB |
Output is correct |
93 |
Correct |
111 ms |
22576 KB |
Output is correct |
94 |
Correct |
112 ms |
21776 KB |
Output is correct |
95 |
Correct |
124 ms |
21160 KB |
Output is correct |
96 |
Correct |
120 ms |
22628 KB |
Output is correct |
97 |
Correct |
133 ms |
22516 KB |
Output is correct |
98 |
Correct |
145 ms |
22548 KB |
Output is correct |
99 |
Correct |
53 ms |
19660 KB |
Output is correct |
100 |
Correct |
177 ms |
24624 KB |
Output is correct |
101 |
Correct |
192 ms |
24560 KB |
Output is correct |
102 |
Correct |
170 ms |
24608 KB |
Output is correct |
103 |
Correct |
190 ms |
24264 KB |
Output is correct |
104 |
Correct |
166 ms |
23872 KB |
Output is correct |
105 |
Correct |
150 ms |
24136 KB |
Output is correct |
106 |
Correct |
156 ms |
23236 KB |
Output is correct |
107 |
Correct |
170 ms |
23752 KB |
Output is correct |
108 |
Correct |
145 ms |
23088 KB |
Output is correct |
109 |
Correct |
159 ms |
23140 KB |
Output is correct |
110 |
Correct |
172 ms |
24388 KB |
Output is correct |
111 |
Correct |
173 ms |
24340 KB |
Output is correct |