#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
template <typename T>
struct SegmentTree{
int n = 0;
vector<T> tree;
T neutral_element = make_pair(1e9, -1); // e.g. 0 for sum, INF for min ...
SegmentTree(){};
SegmentTree(int _n){
n = _n;
tree.assign(n * 4 + 5, neutral_element);
}
inline T combine(T lf, T rg){
return min(lf, rg); // change it
}
inline void build(int v, int tl, int tr, const vector<T> &a){
if (tl == tr){
tree[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm, a);
build(v << 1 | 1, tm + 1, tr, a);
tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
}
inline void init(const vector<T> &a){
n = (int)a.size();
tree.assign(n * 4 + 5, neutral_element);
build(1, 0, n - 1, a);
}
inline void update(int v, int tl, int tr, int pos, T val){
if (tl == tr){
tree[v] = min(tree[v], val); // pay attention!
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm){
update(v << 1, tl, tm, pos, val);
} else {
update(v << 1 | 1, tm + 1, tr, pos, val);
}
tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
}
inline void update(int pos, T val){
update(1, 0, n - 1, pos, val);
}
inline T get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return neutral_element;
int tm = (tl + tr) >> 1;
return combine(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
inline T get(int l, int r){
return get(1, 0, n - 1, l, r);
}
};
int f[N][18];
int l[N], r[N];
int get_parent(int v, int k){
while (k > 0){
if (v == -1) break;
int i = __builtin_ctz(k);
v = f[v][i];
}
return v;
}
bool can_switch(int i, int j){
return l[j] <= r[i] && r[i] <= r[j];
}
void solve(){
int n, q;
cin >> n >> q;
map<int, int> mp;
for (int i = 0; i < n; i++){
cin >> l[i] >> r[i];
mp[l[i]], mp[r[i]];
}
int z = 0;
for (auto &now : mp) now.second = z++;
SegmentTree<pair<int, int>> st(n * 2);
for (int i = 0; i < n; i++) {
st.update(mp[r[i]], make_pair(mp[l[i]], i));
}
for (int i = 0; i < n; i++){
int j = st.get(mp[l[i]], mp[r[i]]).second;
f[i][0] = (j != -1 && l[j] < l[i] ? j : -1);
}
for (int j = 1; j < 18; j++){
for (int i = 0; i < n; i++){
int mid = f[i][j - 1];
f[i][j] = (mid != -1 ? f[mid][j - 1] : -1);
}
}
while (q--){
int x, y;
cin >> x >> y;
--x, --y;
if (x == y){
cout << 0 << endl;
continue;
}
if (can_switch(x, y)){
cout << 1 << endl;
continue;
}
int cnt = 2;
for (int i = 17; i >= 0; i--){
int to = f[y][i];
if (to != -1 && l[to] > r[x]){
cnt += 1 << i;
y = to;
}
}
if (y != -1 && f[y][0] != -1){
cout << cnt << endl;
} else {
cout << "impossible" << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
377 ms |
21084 KB |
Output is correct |
3 |
Correct |
392 ms |
22736 KB |
Output is correct |
4 |
Correct |
383 ms |
22488 KB |
Output is correct |
5 |
Correct |
457 ms |
23268 KB |
Output is correct |
6 |
Correct |
402 ms |
23476 KB |
Output is correct |
7 |
Correct |
434 ms |
23844 KB |
Output is correct |
8 |
Correct |
471 ms |
27628 KB |
Output is correct |
9 |
Correct |
388 ms |
27596 KB |
Output is correct |
10 |
Incorrect |
410 ms |
22432 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
19700 KB |
Output is correct |
2 |
Correct |
416 ms |
22592 KB |
Output is correct |
3 |
Correct |
409 ms |
22580 KB |
Output is correct |
4 |
Correct |
401 ms |
27924 KB |
Output is correct |
5 |
Incorrect |
386 ms |
22468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
377 ms |
21084 KB |
Output is correct |
3 |
Correct |
392 ms |
22736 KB |
Output is correct |
4 |
Correct |
383 ms |
22488 KB |
Output is correct |
5 |
Correct |
457 ms |
23268 KB |
Output is correct |
6 |
Correct |
402 ms |
23476 KB |
Output is correct |
7 |
Correct |
434 ms |
23844 KB |
Output is correct |
8 |
Correct |
471 ms |
27628 KB |
Output is correct |
9 |
Correct |
388 ms |
27596 KB |
Output is correct |
10 |
Incorrect |
410 ms |
22432 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |