#include <bits/stdc++.h>
using namespace std;
#define int int64_t
struct seg {
vector<pair<int, int>> tree;
seg(int n) {
tree.assign(n*4, {1e18, -1});
}
void update(int v, int rl, int rr, int pos, pair<int, int> x) {
if (rl == rr) {
tree[v] = x;
return;
}
int rm = (rl + rr) / 2;
if (pos <= rm) update(v*2, rl, rm, pos, x);
else update(v*2+1, rm+1, rr, pos, x);
tree[v] = min(tree[v*2], tree[v*2+1]);
}
pair<int, int> query(int v, int rl, int rr, int ql, int qr) {
if (ql > qr) return {1e18, -1};
if (rl == ql && rr == qr) return tree[v];
int rm = (rl + rr) / 2;
return min(query(v*2, rl, rm, ql, min(rm, qr)), query(v*2+1, rm+1, rr, max(rm+1, ql), qr));
}
};
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, q; cin >> n >> q;
vector<tuple<int, int, int>> events(n);
for (int i = 0; i<n; i++) {
cin >> get<1>(events[i]) >> get<0>(events[i]);
get<2>(events[i]) = i;
}
sort(events.begin(), events.end());
vector<pair<int, int>> segtreeRanges;
for (auto i: events) {
int l = upper_bound(events.begin(), events.end(), tuple<int, int, int>{get<1>(i)-1, 1e18, 1e18}) - events.begin();
int r = upper_bound(events.begin(), events.end(), tuple<int, int, int>{get<0>(i), 1e18, 1e18}) - events.begin() - 1;
segtreeRanges.push_back({l, r});
}
for (auto &i: events) swap(get<0>(i), get<1>(i));
seg tree(n);
for (int i = 0; i<n; i++) {
tree.update(1, 0, n-1, i, {get<0>(events[i]), get<2>(events[i])});
}
vector<pair<int, int>> parents(n);
for (int i = 0; i<n; i++) {
parents[get<2>(events[i])] = tree.query(1, 0, n-1, segtreeRanges[i].first, segtreeRanges[i].second);
}
// for (auto i: parents) {
// cerr << i.first << " " << i.second << "\n";
// }
for (auto &i: events) swap(get<0>(i), get<2>(i));
sort(events.begin(), events.end());
for (auto &i: events) swap(get<0>(i), get<2>(i));
// for (auto i: events) cerr << get<0>(i) << " " << get<1>(i) << "\n";
for (int i = 0; i<q; i++) {
int s, e; cin >> s >> e;
s--;
e--;
// cerr << s << " " << e << "\n";
if (s == e) {cout << 0 << "\n"; continue;}
int cnt = 1;
while (parents[e].second != e) {
if (get<0>(events[e]) <= get<1>(events[s]) && get<1>(events[s]) <= get<1>(events[e])) {cout << cnt << "\n"; break;}
e = parents[e].second;
cnt++;
if (get<0>(events[e]) <= get<1>(events[s]) && get<1>(events[s]) <= get<1>(events[e])) {cout << cnt << "\n"; break;}
}
if (!(get<0>(events[e]) <= get<1>(events[s]) && get<1>(events[s]) <= get<1>(events[e]))) cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1600 ms |
12076 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
412 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
412 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
790 ms |
1452 KB |
Output is correct |
20 |
Execution timed out |
1582 ms |
1284 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
412 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
235 ms |
13604 KB |
Output is correct |
20 |
Correct |
129 ms |
12264 KB |
Output is correct |
21 |
Correct |
122 ms |
12192 KB |
Output is correct |
22 |
Correct |
93 ms |
12192 KB |
Output is correct |
23 |
Correct |
73 ms |
12396 KB |
Output is correct |
24 |
Correct |
87 ms |
12212 KB |
Output is correct |
25 |
Correct |
55 ms |
12168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1591 ms |
12188 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1600 ms |
12076 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |