#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_M = 5005;
const int INF = 1e9 + 7;
int S[MAX_N], E[MAX_N];
int dist[MAX_N];
int can_reach[MAX_M][MAX_M];
vector <int> graph[MAX_N];
int nxt[MAX_N][20];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
for(int i = 1; i <= N; i++) {
cin >> S[i] >> E[i];
}
if(N <= 1000 and Q <= 100 and false) {
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
if(i != j and S[j] <= E[i] and E[i] <= E[j]) {
graph[i].push_back(j);
}
}
}
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
dist[j] = -1;
}
queue <int> q;
q.push(i);
dist[i] = 0;
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto v : graph[u]) {
if(dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
for(int j = 1; j <= N; j++) {
can_reach[i][j] = dist[j];
}
}
while(Q--) {
int s, e;
cin >> s >> e;
if(can_reach[s][e] == -1) {
cout << "impossible\n";
}
else {
cout << can_reach[s][e] << '\n';
}
}
}
else {
vector <pair <int, int>> vec;
for(int i = 1; i <= N; i++) {
vec.emplace_back(E[i], S[i]);
}
sort(vec.begin(), vec.end());
for(int l = 0, r = 0; l < N; l++) {
while(r < N and l >= r) {
r++;
}
while(r < N and vec[l].first < vec[r].second) {
r++;
}
nxt[l][0] = N;
if(r < N) {
nxt[l][0] = r;
}
}
nxt[N][0] = N;
for(int j = 1; j < 20; j++) {
for(int i = 0; i <= N; i++) {
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
}
}
while(Q--) {
int s, e;
cin >> s >> e;
int ps = lower_bound(vec.begin(), vec.end(), make_pair(E[s], S[s])) - vec.begin();
int pe = lower_bound(vec.begin(), vec.end(), make_pair(E[e], S[e])) - vec.begin();
int ans = 0;
for(int i = 19; i >= 0; i--) {
if(nxt[ps][i] <= pe) {
ps = nxt[ps][i];
ans += (1<<i);
}
}
if(ps == pe) {
cout << ans << '\n';
}
else {
cout << "impossible\n";
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
84 ms |
13772 KB |
Output is correct |
3 |
Correct |
110 ms |
15792 KB |
Output is correct |
4 |
Correct |
106 ms |
15776 KB |
Output is correct |
5 |
Correct |
89 ms |
15828 KB |
Output is correct |
6 |
Correct |
90 ms |
15896 KB |
Output is correct |
7 |
Correct |
100 ms |
15968 KB |
Output is correct |
8 |
Correct |
95 ms |
16308 KB |
Output is correct |
9 |
Correct |
97 ms |
16264 KB |
Output is correct |
10 |
Incorrect |
124 ms |
16296 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
14248 KB |
Output is correct |
2 |
Correct |
119 ms |
14028 KB |
Output is correct |
3 |
Correct |
118 ms |
14036 KB |
Output is correct |
4 |
Correct |
81 ms |
14632 KB |
Output is correct |
5 |
Incorrect |
162 ms |
14500 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
84 ms |
13772 KB |
Output is correct |
3 |
Correct |
110 ms |
15792 KB |
Output is correct |
4 |
Correct |
106 ms |
15776 KB |
Output is correct |
5 |
Correct |
89 ms |
15828 KB |
Output is correct |
6 |
Correct |
90 ms |
15896 KB |
Output is correct |
7 |
Correct |
100 ms |
15968 KB |
Output is correct |
8 |
Correct |
95 ms |
16308 KB |
Output is correct |
9 |
Correct |
97 ms |
16264 KB |
Output is correct |
10 |
Incorrect |
124 ms |
16296 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |