#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];
cmp.push_back(s[i]);
cmp.push_back(e[i]);
}
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
auto find = [&] (ll x) {
return lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin() + 1;
};
rep(i, 1, n) {
s[i] = find(s[i]);
e[i] = find(e[i]);
in[s[i]].push_back(i);
out[e[i]].push_back(i);
}
if (q <= 100) {
sub4 :: solve();
return 0;
}
set <int> save;
rep(timer, 1, 2 * n) {
for (int i: in[timer]) save.insert(i);
for (int i: out[timer]) {
for (int j: save)
d[i].push_back(j);
}
for (int i: out[timer])
save.erase(i);
}
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 |
1592 ms |
44236 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 |
11988 KB |
Output is correct |
3 |
Correct |
8 ms |
12116 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12084 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
8 ms |
12116 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12084 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 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 |
12176 KB |
Output is correct |
14 |
Correct |
7 ms |
12116 KB |
Output is correct |
15 |
Correct |
7 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12116 KB |
Output is correct |
19 |
Execution timed out |
1578 ms |
52256 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 |
11988 KB |
Output is correct |
3 |
Correct |
8 ms |
12116 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12084 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
8 ms |
12116 KB |
Output is correct |
13 |
Correct |
7 ms |
12116 KB |
Output is correct |
14 |
Correct |
7 ms |
12212 KB |
Output is correct |
15 |
Correct |
7 ms |
12116 KB |
Output is correct |
16 |
Correct |
8 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12116 KB |
Output is correct |
19 |
Correct |
163 ms |
23760 KB |
Output is correct |
20 |
Correct |
99 ms |
23768 KB |
Output is correct |
21 |
Correct |
98 ms |
20752 KB |
Output is correct |
22 |
Correct |
105 ms |
23712 KB |
Output is correct |
23 |
Correct |
102 ms |
23764 KB |
Output is correct |
24 |
Correct |
141 ms |
23724 KB |
Output is correct |
25 |
Correct |
70 ms |
18632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1592 ms |
43948 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 |
1592 ms |
44236 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |