#include <bits/stdc++.h>
#define ll long long
#define pii pair <ll, ll>
#define st first
#define nd second
#define rep(i, n, m) for (ll i = (n); i <= (m); i ++)
#define rrep(i, n, m) for (ll i = (n); i >= (m); i --)
using namespace std;
const long long INF = 1e18;
const long long N = 1e5 + 5;
ll n, q, s[N], e[N];
vector <int> cmp, out[2 * N], in[2 * N];
vector <int> d[N];
const ll Nmax = 5005;
int dist[Nmax][Nmax];
void bfs(int st, int *f) {
rep(i, 1, n) f[i] = 1e9;
f[st] = 0;
queue <int> q;
q.push(st);
while (q.size()) {
int u = q.front(); q.pop();
for (int v: d[u]) if (f[v] > f[u] + 1) {
f[v] = f[u] + 1;
q.push(v);
}
}
}
namespace sub4 {
pii a[N];
ll pos[N], f[N];
void solve() {
rep(i, 1, n) a[i] = {e[i], i};
sort(a + 1, a + 1 + n);
rep(i, 1, n) pos[a[i].nd] = i;
while (q --) {
int u, v;
cin >> u >> v;
int i = pos[u];
int j = pos[v];
int l = j;
if (i > j && a[j].st < a[i].st) {
cout << "impossible\n";
continue;
}
if (i > j) {
cout << 1 << '\n';
continue;
}
while (l + 1 <= n && a[l + 1].st == a[j].st) l ++;
f[l + 1] = 1e9;
//cout << i << ' ' << j << '\n';
rrep(k, l, i) {
int id = a[k].nd;
f[k] = min(f[k + 1], s[id]);
}
int ans = 0;
while (i < j) {
ans ++;
int cur = a[i].st, pre = i;
while (i < l && cur >= f[i + 1]) {
i ++;
}
if (i > j) {
int id = a[j].nd;
if (cur >= s[v] && cur <= e[v])
break;
ans ++;
break;
}
if (i == pre) break;
}
//cout << i << ' ' << j << '\n';
if (i >= j)
cout << ans << '\n';
else
cout << "impossible\n";
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
rep(i, 1, n)
cin >> s[i] >> e[i];
if (q <= 100) {
sub4 :: solve();
return 0;
}
rep(i, 1, n) rep(j, 1, n) if (i != j)
if (s[j] <= e[i] && e[i] <= e[j])
d[i].push_back(j);
rep(i, 1, n) bfs(i, dist[i]);
while (q --) {
int u, v;
cin >> u >> v;
if (dist[u][v] >= (int)1e9)
cout << "impossible\n";
else
cout << dist[u][v] << '\n';
}
return 0;
}
Compilation message
events.cpp: In function 'void sub4::solve()':
events.cpp:74:25: warning: unused variable 'id' [-Wunused-variable]
74 | int id = a[j].nd;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Execution timed out |
1594 ms |
13976 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12060 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12060 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12004 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
7 ms |
12116 KB |
Output is correct |
14 |
Correct |
6 ms |
12048 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12084 KB |
Output is correct |
19 |
Execution timed out |
1599 ms |
51056 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12048 KB |
Output is correct |
3 |
Correct |
7 ms |
12072 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12060 KB |
Output is correct |
6 |
Correct |
6 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12004 KB |
Output is correct |
10 |
Correct |
6 ms |
12088 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
12132 KB |
Output is correct |
13 |
Correct |
6 ms |
12132 KB |
Output is correct |
14 |
Correct |
7 ms |
12096 KB |
Output is correct |
15 |
Correct |
7 ms |
12076 KB |
Output is correct |
16 |
Correct |
7 ms |
12132 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12116 KB |
Output is correct |
19 |
Correct |
85 ms |
16780 KB |
Output is correct |
20 |
Correct |
37 ms |
16728 KB |
Output is correct |
21 |
Correct |
42 ms |
16728 KB |
Output is correct |
22 |
Correct |
44 ms |
16632 KB |
Output is correct |
23 |
Correct |
40 ms |
16740 KB |
Output is correct |
24 |
Correct |
70 ms |
16748 KB |
Output is correct |
25 |
Correct |
61 ms |
16596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1581 ms |
13764 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Execution timed out |
1594 ms |
13976 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |