bool home = 0;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;
struct T {
int l;
int r;
int init;
};
bool operator < (T a, T b) {
if (a.r != b.r) {
return a.r < b.r;
} else {
return a.l < b.l;
}
}
T v[N];
int dp[N];
int nxt[N];
int ant[N];
int inv[N];
int low[N];
int high[N];
int cnt_rs_under(int limit) {
int low = 1, high = n, cnt = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (v[mid].r <= limit) {
cnt = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return cnt;
}
pair<int, int> join(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
const int K = 20;
const int INF = (int) 1e9 + 7;
pair<int, int> after[K][N];
pair<int, int> tree[K][4 * N];
void build(int v, int tl, int tr, int k) {
if (tl == tr) {
tree[k][v] = after[k][tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, k);
build(2 * v + 1, tm + 1, tr, k);
tree[k][v] = join(tree[k][2 * v], tree[k][2 * v + 1]);
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r, int k) {
if (tr < l || r < tl) {
return make_pair(INF, -INF);
}
if (l <= tl && tr <= r) {
return tree[k][v];
}
int tm = (tl + tr) / 2;
return join(get(2 * v, tl, tm, l, r, k), get(2 * v + 1, tm + 1, tr, l, r, k));
}
pair<int, int> get(int l, int r, int k) {
return get(1, 1, n, l, r, k);
}
void build(int k) {
build(1, 1, n, k);
}
pair<int, int> do_2_pw_k_steps(pair<int, int> cur, int k) {
int L = cur.first, R = cur.second;
assert(1 <= L && L <= R && R <= n);
return get(L, R, k);
}
int main() {
if (!home) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
} else {
freopen("input.txt", "r", stdin);
}
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> v[i].l >> v[i].r;
v[i].init = i;
}
sort(v + 1, v + n + 1);
for (int i = 1; i <= n; i++) {
int x = cnt_rs_under(v[i].l - 1) + 1;
int y = cnt_rs_under(v[i].r);
low[i] = x;
high[i] = y;
after[0][i] = {low[i], high[i]};
}
build(0);
for (int k = 1; k < K; k++) {
for (int i = 1; i <= n; i++) {
after[k][i] = do_2_pw_k_steps(after[k - 1][i], k - 1);
}
build(k);
}
for (int i = 1; i <= n; i++) {
inv[v[i].init] = i;
}
for (int iq = 1; iq <= q; iq++) {
int ff, ss;
cin >> ff >> ss;
ff = inv[ff];
ss = inv[ss];
int cost = 0;
pair<int, int> cur = {ss, ss};
while (!(cur.first <= ff && ff <= cur.second)) {
pair<int, int> nxt_cur = do_2_pw_k_steps(cur, 0);
if (cur == nxt_cur) {
break;
}
cost++;
cur = nxt_cur;
}
if (!(cur.first <= ff && ff <= cur.second)) {
cout << "impossible\n";
} else {
cout << cost << "\n";
}
}
return 0;
}
/**
**/
Compilation message
events.cpp: In function 'int main()':
events.cpp:96:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Execution timed out |
1595 ms |
59356 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
15 ms |
1108 KB |
Output is correct |
4 |
Correct |
5 ms |
980 KB |
Output is correct |
5 |
Correct |
7 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
15 ms |
1108 KB |
Output is correct |
4 |
Correct |
5 ms |
980 KB |
Output is correct |
5 |
Correct |
7 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
15 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1100 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Execution timed out |
1589 ms |
4104 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
15 ms |
1108 KB |
Output is correct |
4 |
Correct |
5 ms |
980 KB |
Output is correct |
5 |
Correct |
7 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
15 ms |
980 KB |
Output is correct |
13 |
Correct |
6 ms |
1108 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
980 KB |
Output is correct |
16 |
Correct |
3 ms |
996 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Execution timed out |
1600 ms |
59468 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1601 ms |
59480 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Execution timed out |
1595 ms |
59356 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |