#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
struct item {
int L, R;
item() : L(INT_MAX), R(0) {}
item (int L, int R) : L(L), R(R) {}
bool operator < (const item &o) const {
if (R == o.R) return L < o.L;
return R < o.R;
}
} event[mn];
struct IT {
vector<int> tr;
IT (int sz) : tr(4 * sz) {}
int opr (int a, int b) {
return (event[a].L < event[b].L ? a : b);
}
void update (int pos, int val, int k, int l, int r) {
for (; l < r;) {
int mid = (l + r) >> 1;
if (pos <= mid) k <<= 1, r = mid;
else k <<= 1, k |= 1, l = mid + 1;
}
tr[k] = opr(tr[k], val);
for (k /= 2; k; k /= 2)
tr[k] = opr(tr[k << 1], tr[k << 1 | 1]);
}
int query (int a, int b, int k, int l, int r) {
if (b < l || r < a) return 0;
if (a <= l && r <= b) return tr[k];
int mid = (l + r) >> 1;
return opr(query(a, b, 2 * k, l, mid), query(a, b, 2 * k + 1, mid + 1, r));
}
};
vector<int> cmp;
int nxt[18][mn];
int getID (int targ) {
return lower_bound(all(cmp), targ) - cmp.begin();
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> event[i].L >> event[i].R;
cmp.push_back(event[i].L), cmp.push_back(event[i].R);
}
sort(all(cmp)), filter(cmp);
vector<int> ord(n);
for (int i = 1; i <= n; i++) ord[i - 1] = i;
sort(all(ord), [&] (int a, int b)
{ return (event[a].R == event[b].R ? event[a].L < event[b].L : event[a].R < event[b].R); });
IT tree(cmp.size());
for (int i : ord) {
event[i].L = getID(event[i].L), event[i].R = getID(event[i].R);
tree.update(event[i].R, i, 1, 0, cmp.size() - 1);
nxt[0][i] = tree.query(event[i].L, event[i].R, 1, 0, cmp.size() - 1);
// cout << "next " << i << " -> " << nxt[0][i] << "\n";
}
for (int s = 1; s < 18; s++)
for (int i = 1; i <= n; i++)
nxt[s][i] = nxt[s - 1][nxt[s - 1][i]];
for (int i = 0; i < q; i++) {
int s, e; cin >> s >> e;
if (s == e) {
cout << 0 << "\n";
continue;
}
if (event[s].R > event[e].R) {
cout << "impossible\n";
continue;
}
if (event[e].L <= event[s].R) {
cout << 1 << "\n";
continue;
}
int ans = 0;
for (int lift = 17; lift >= 0; lift--) {
if (event[nxt[lift][e]].L > event[s].R) e = nxt[lift][e], ans |= (1 << lift);
}
e = nxt[0][e], ans++;
if (event[e].L > event[s].R) cout << "impossible\n";
else cout << ans + 1 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
108 ms |
11376 KB |
Output is correct |
3 |
Correct |
100 ms |
11456 KB |
Output is correct |
4 |
Correct |
102 ms |
11584 KB |
Output is correct |
5 |
Correct |
99 ms |
11716 KB |
Output is correct |
6 |
Correct |
108 ms |
11888 KB |
Output is correct |
7 |
Correct |
103 ms |
11888 KB |
Output is correct |
8 |
Correct |
111 ms |
13676 KB |
Output is correct |
9 |
Correct |
109 ms |
16576 KB |
Output is correct |
10 |
Correct |
101 ms |
15040 KB |
Output is correct |
11 |
Correct |
94 ms |
15552 KB |
Output is correct |
12 |
Correct |
63 ms |
14304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
2 ms |
7248 KB |
Output is correct |
4 |
Correct |
2 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
4 ms |
7480 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Correct |
3 ms |
7400 KB |
Output is correct |
9 |
Correct |
3 ms |
7248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
2 ms |
7248 KB |
Output is correct |
4 |
Correct |
2 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
4 ms |
7480 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Correct |
3 ms |
7400 KB |
Output is correct |
9 |
Correct |
3 ms |
7248 KB |
Output is correct |
10 |
Correct |
3 ms |
7248 KB |
Output is correct |
11 |
Correct |
2 ms |
7248 KB |
Output is correct |
12 |
Correct |
3 ms |
7248 KB |
Output is correct |
13 |
Correct |
3 ms |
7448 KB |
Output is correct |
14 |
Correct |
2 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
2 ms |
7248 KB |
Output is correct |
18 |
Correct |
2 ms |
7248 KB |
Output is correct |
19 |
Correct |
26 ms |
9032 KB |
Output is correct |
20 |
Correct |
26 ms |
9040 KB |
Output is correct |
21 |
Correct |
28 ms |
9160 KB |
Output is correct |
22 |
Correct |
21 ms |
9296 KB |
Output is correct |
23 |
Correct |
20 ms |
9296 KB |
Output is correct |
24 |
Correct |
20 ms |
9296 KB |
Output is correct |
25 |
Correct |
23 ms |
8272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
2 ms |
7248 KB |
Output is correct |
4 |
Correct |
2 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
4 ms |
7480 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Correct |
3 ms |
7400 KB |
Output is correct |
9 |
Correct |
3 ms |
7248 KB |
Output is correct |
10 |
Correct |
2 ms |
7248 KB |
Output is correct |
11 |
Correct |
2 ms |
7248 KB |
Output is correct |
12 |
Correct |
3 ms |
7260 KB |
Output is correct |
13 |
Correct |
3 ms |
7248 KB |
Output is correct |
14 |
Correct |
2 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
3 ms |
7416 KB |
Output is correct |
18 |
Correct |
3 ms |
7248 KB |
Output is correct |
19 |
Correct |
79 ms |
12740 KB |
Output is correct |
20 |
Correct |
67 ms |
11972 KB |
Output is correct |
21 |
Correct |
77 ms |
12712 KB |
Output is correct |
22 |
Correct |
71 ms |
13508 KB |
Output is correct |
23 |
Correct |
80 ms |
14288 KB |
Output is correct |
24 |
Correct |
80 ms |
14464 KB |
Output is correct |
25 |
Correct |
21 ms |
10700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
11460 KB |
Output is correct |
2 |
Correct |
97 ms |
11372 KB |
Output is correct |
3 |
Correct |
99 ms |
11584 KB |
Output is correct |
4 |
Correct |
90 ms |
13508 KB |
Output is correct |
5 |
Correct |
95 ms |
11976 KB |
Output is correct |
6 |
Correct |
101 ms |
13252 KB |
Output is correct |
7 |
Correct |
104 ms |
13252 KB |
Output is correct |
8 |
Correct |
100 ms |
13284 KB |
Output is correct |
9 |
Correct |
74 ms |
12484 KB |
Output is correct |
10 |
Correct |
113 ms |
12908 KB |
Output is correct |
11 |
Correct |
101 ms |
12740 KB |
Output is correct |
12 |
Correct |
102 ms |
12928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
108 ms |
11376 KB |
Output is correct |
3 |
Correct |
100 ms |
11456 KB |
Output is correct |
4 |
Correct |
102 ms |
11584 KB |
Output is correct |
5 |
Correct |
99 ms |
11716 KB |
Output is correct |
6 |
Correct |
108 ms |
11888 KB |
Output is correct |
7 |
Correct |
103 ms |
11888 KB |
Output is correct |
8 |
Correct |
111 ms |
13676 KB |
Output is correct |
9 |
Correct |
109 ms |
16576 KB |
Output is correct |
10 |
Correct |
101 ms |
15040 KB |
Output is correct |
11 |
Correct |
94 ms |
15552 KB |
Output is correct |
12 |
Correct |
63 ms |
14304 KB |
Output is correct |
13 |
Correct |
2 ms |
7248 KB |
Output is correct |
14 |
Correct |
3 ms |
7248 KB |
Output is correct |
15 |
Correct |
2 ms |
7248 KB |
Output is correct |
16 |
Correct |
2 ms |
7248 KB |
Output is correct |
17 |
Correct |
3 ms |
7248 KB |
Output is correct |
18 |
Correct |
4 ms |
7480 KB |
Output is correct |
19 |
Correct |
3 ms |
7416 KB |
Output is correct |
20 |
Correct |
3 ms |
7400 KB |
Output is correct |
21 |
Correct |
3 ms |
7248 KB |
Output is correct |
22 |
Correct |
3 ms |
7248 KB |
Output is correct |
23 |
Correct |
2 ms |
7248 KB |
Output is correct |
24 |
Correct |
3 ms |
7248 KB |
Output is correct |
25 |
Correct |
3 ms |
7448 KB |
Output is correct |
26 |
Correct |
2 ms |
7248 KB |
Output is correct |
27 |
Correct |
3 ms |
7248 KB |
Output is correct |
28 |
Correct |
3 ms |
7248 KB |
Output is correct |
29 |
Correct |
2 ms |
7248 KB |
Output is correct |
30 |
Correct |
2 ms |
7248 KB |
Output is correct |
31 |
Correct |
26 ms |
9032 KB |
Output is correct |
32 |
Correct |
26 ms |
9040 KB |
Output is correct |
33 |
Correct |
28 ms |
9160 KB |
Output is correct |
34 |
Correct |
21 ms |
9296 KB |
Output is correct |
35 |
Correct |
20 ms |
9296 KB |
Output is correct |
36 |
Correct |
20 ms |
9296 KB |
Output is correct |
37 |
Correct |
23 ms |
8272 KB |
Output is correct |
38 |
Correct |
2 ms |
7248 KB |
Output is correct |
39 |
Correct |
2 ms |
7248 KB |
Output is correct |
40 |
Correct |
3 ms |
7260 KB |
Output is correct |
41 |
Correct |
3 ms |
7248 KB |
Output is correct |
42 |
Correct |
2 ms |
7248 KB |
Output is correct |
43 |
Correct |
3 ms |
7248 KB |
Output is correct |
44 |
Correct |
3 ms |
7248 KB |
Output is correct |
45 |
Correct |
3 ms |
7416 KB |
Output is correct |
46 |
Correct |
3 ms |
7248 KB |
Output is correct |
47 |
Correct |
79 ms |
12740 KB |
Output is correct |
48 |
Correct |
67 ms |
11972 KB |
Output is correct |
49 |
Correct |
77 ms |
12712 KB |
Output is correct |
50 |
Correct |
71 ms |
13508 KB |
Output is correct |
51 |
Correct |
80 ms |
14288 KB |
Output is correct |
52 |
Correct |
80 ms |
14464 KB |
Output is correct |
53 |
Correct |
21 ms |
10700 KB |
Output is correct |
54 |
Correct |
84 ms |
11460 KB |
Output is correct |
55 |
Correct |
97 ms |
11372 KB |
Output is correct |
56 |
Correct |
99 ms |
11584 KB |
Output is correct |
57 |
Correct |
90 ms |
13508 KB |
Output is correct |
58 |
Correct |
95 ms |
11976 KB |
Output is correct |
59 |
Correct |
101 ms |
13252 KB |
Output is correct |
60 |
Correct |
104 ms |
13252 KB |
Output is correct |
61 |
Correct |
100 ms |
13284 KB |
Output is correct |
62 |
Correct |
74 ms |
12484 KB |
Output is correct |
63 |
Correct |
113 ms |
12908 KB |
Output is correct |
64 |
Correct |
101 ms |
12740 KB |
Output is correct |
65 |
Correct |
102 ms |
12928 KB |
Output is correct |
66 |
Correct |
2 ms |
7416 KB |
Output is correct |
67 |
Correct |
92 ms |
14556 KB |
Output is correct |
68 |
Correct |
102 ms |
14532 KB |
Output is correct |
69 |
Correct |
109 ms |
14020 KB |
Output is correct |
70 |
Correct |
107 ms |
14924 KB |
Output is correct |
71 |
Correct |
102 ms |
14032 KB |
Output is correct |
72 |
Correct |
109 ms |
15052 KB |
Output is correct |
73 |
Correct |
97 ms |
16728 KB |
Output is correct |
74 |
Correct |
93 ms |
16580 KB |
Output is correct |
75 |
Correct |
98 ms |
15052 KB |
Output is correct |
76 |
Correct |
91 ms |
15556 KB |
Output is correct |
77 |
Correct |
59 ms |
14276 KB |
Output is correct |
78 |
Correct |
2 ms |
7248 KB |
Output is correct |
79 |
Correct |
2 ms |
7248 KB |
Output is correct |
80 |
Correct |
2 ms |
7248 KB |
Output is correct |
81 |
Correct |
2 ms |
7248 KB |
Output is correct |
82 |
Correct |
2 ms |
7248 KB |
Output is correct |
83 |
Correct |
2 ms |
7248 KB |
Output is correct |
84 |
Correct |
2 ms |
7248 KB |
Output is correct |
85 |
Correct |
2 ms |
7248 KB |
Output is correct |
86 |
Correct |
25 ms |
9096 KB |
Output is correct |
87 |
Correct |
26 ms |
8980 KB |
Output is correct |
88 |
Correct |
25 ms |
9544 KB |
Output is correct |
89 |
Correct |
20 ms |
9296 KB |
Output is correct |
90 |
Correct |
20 ms |
9296 KB |
Output is correct |
91 |
Correct |
18 ms |
9296 KB |
Output is correct |
92 |
Correct |
22 ms |
9056 KB |
Output is correct |
93 |
Correct |
65 ms |
12740 KB |
Output is correct |
94 |
Correct |
61 ms |
11972 KB |
Output is correct |
95 |
Correct |
77 ms |
12752 KB |
Output is correct |
96 |
Correct |
67 ms |
13508 KB |
Output is correct |
97 |
Correct |
74 ms |
14280 KB |
Output is correct |
98 |
Correct |
76 ms |
14340 KB |
Output is correct |
99 |
Correct |
20 ms |
10692 KB |
Output is correct |
100 |
Correct |
104 ms |
16336 KB |
Output is correct |
101 |
Correct |
108 ms |
16324 KB |
Output is correct |
102 |
Correct |
112 ms |
16324 KB |
Output is correct |
103 |
Correct |
111 ms |
16072 KB |
Output is correct |
104 |
Correct |
108 ms |
15808 KB |
Output is correct |
105 |
Correct |
111 ms |
15844 KB |
Output is correct |
106 |
Correct |
96 ms |
13764 KB |
Output is correct |
107 |
Correct |
109 ms |
14020 KB |
Output is correct |
108 |
Correct |
102 ms |
14784 KB |
Output is correct |
109 |
Correct |
106 ms |
14788 KB |
Output is correct |
110 |
Correct |
118 ms |
16072 KB |
Output is correct |
111 |
Correct |
126 ms |
16324 KB |
Output is correct |