#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 5e3 + 5, INF = 1e9, MAX_Q = 1e5 + 5;
int n, q;
array<int, MAX_N> l, r;
unordered_map<int, vector<pii>> queries; // {index, s}
vector<pii> ord; // {r, i}
int n_ccs;
unordered_map<int, int> cc;
array<int, MAX_N> cc_l, cc_r;
void precomp() {
for (int i = 1; i <= n; i++) ord.push_back({r[i], i});
sort(ord.rbegin(), ord.rend());
set<int> coords;
for (int i = 1; i <= n; i++) coords.insert(l[i]), coords.insert(r[i]);
while (coords.size()) {
n_ccs++, cc[*coords.begin()] = n_ccs;
coords.erase(coords.begin());
}
for (int i = 1; i <= n; i++) cc_l[i] = cc[l[i]], cc_r[i] = cc[r[i]];
}
array<int, 8 * MAX_N> st;
void update(int l, int r, int x, int u = 1, int lo = 1, int hi = n_ccs) {
if (l <= lo && hi <= r) { st[u] = min(st[u], x); return; }
int mid = (lo + hi) / 2;
if (l <= mid) update(l, r, x, 2 * u, lo, mid);
if (r > mid) update(l, r, x, 2 * u + 1, mid + 1, hi);
}
int query(int i, int u = 1, int lo = 1, int hi = n_ccs) {
if (lo == hi) return st[u];
int mid = (lo + hi) / 2;
if (i <= mid) return min(st[u], query(i, 2 * u, lo, mid));
else return min(st[u], query(i, 2 * u + 1, mid + 1, hi));
}
array<int, MAX_N> dp;
void comp(int f) {
fill(st.begin() + 1, st.begin() + 4 * n_ccs, INF);
for (pii x : ord) {
int i = x.second;
if (i == f) dp[i] = 0;
else if (l[f] <= r[i] && r[i] <= r[f]) dp[i] = 1;
else dp[i] = query(cc_r[i]) + 1;
update(cc_l[i], cc_r[i], dp[i]);
}
}
array<int, MAX_Q> ans;
int main() {
// freopen("event.in", "r", stdin);
cin.sync_with_stdio(false), cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
for (int i = 1; i <= q; i++) {
int s, f; cin >> s >> f;
queries[f].push_back({i, s});
}
precomp();
for (pair<int, vector<pii>> x : queries) {
comp(x.first);
for (pii y : x.second) ans[y.first] = dp[y.second];
}
for (int i = 1; i <= q; i++)
cout << ((ans[i] >= INF) ? "impossible" : to_string(ans[i])) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
608 KB |
Output is correct |
7 |
Correct |
19 ms |
604 KB |
Output is correct |
8 |
Correct |
9 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
608 KB |
Output is correct |
7 |
Correct |
19 ms |
604 KB |
Output is correct |
8 |
Correct |
9 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
202 ms |
3152 KB |
Output is correct |
20 |
Correct |
231 ms |
2916 KB |
Output is correct |
21 |
Execution timed out |
1573 ms |
2864 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
608 KB |
Output is correct |
7 |
Correct |
19 ms |
604 KB |
Output is correct |
8 |
Correct |
9 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
6 ms |
612 KB |
Output is correct |
14 |
Correct |
5 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
608 KB |
Output is correct |
16 |
Correct |
11 ms |
692 KB |
Output is correct |
17 |
Correct |
9 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |