#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
vector<int> lmao[100005];
int S[100005], E[100005], dist[5005][5005];
int n, q;
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++)cin >> S[i] >> E[i];
set <pi> st;
for(int i=1;i<=n;i++)st.insert({E[i], i});
for(int i=1;i<=n;i++){
set<pi> :: iterator it = st.lower_bound({S[i], 0});
while(it != st.end()){
pi tmp = *it;
if(tmp.fi > E[i])break;
lmao[tmp.se].push_back(i);
it++;
}
}
for(int i=1;i<=n;i++){
queue<int> qu;
for(int j=1;j<=n;j++)dist[i][j] = 1e9;
dist[i][i] = 0;
qu.push(i);
while(!qu.empty()){
int a = qu.front(); qu.pop();
for(auto j : lmao[a]){
if(dist[i][j] > dist[i][a] + 1)dist[i][j] = dist[i][a] + 1, qu.push(j);
}
}
}
while(q--){
int x, y; cin >> x >> y;
if(dist[x][y] != 1e9)cout << dist[x][y] << '\n';
else cout << "impossible\n";
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
events.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
50 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
29848 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
16 ms |
14660 KB |
Output is correct |
4 |
Correct |
11 ms |
14676 KB |
Output is correct |
5 |
Correct |
18 ms |
14708 KB |
Output is correct |
6 |
Correct |
52 ms |
16404 KB |
Output is correct |
7 |
Correct |
148 ms |
18132 KB |
Output is correct |
8 |
Correct |
164 ms |
20084 KB |
Output is correct |
9 |
Correct |
866 ms |
22660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
16 ms |
14660 KB |
Output is correct |
4 |
Correct |
11 ms |
14676 KB |
Output is correct |
5 |
Correct |
18 ms |
14708 KB |
Output is correct |
6 |
Correct |
52 ms |
16404 KB |
Output is correct |
7 |
Correct |
148 ms |
18132 KB |
Output is correct |
8 |
Correct |
164 ms |
20084 KB |
Output is correct |
9 |
Correct |
866 ms |
22660 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
16 ms |
14676 KB |
Output is correct |
13 |
Correct |
11 ms |
14676 KB |
Output is correct |
14 |
Correct |
17 ms |
14680 KB |
Output is correct |
15 |
Correct |
54 ms |
16312 KB |
Output is correct |
16 |
Correct |
151 ms |
18084 KB |
Output is correct |
17 |
Correct |
169 ms |
20136 KB |
Output is correct |
18 |
Correct |
860 ms |
22640 KB |
Output is correct |
19 |
Execution timed out |
1595 ms |
80048 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
16 ms |
14660 KB |
Output is correct |
4 |
Correct |
11 ms |
14676 KB |
Output is correct |
5 |
Correct |
18 ms |
14708 KB |
Output is correct |
6 |
Correct |
52 ms |
16404 KB |
Output is correct |
7 |
Correct |
148 ms |
18132 KB |
Output is correct |
8 |
Correct |
164 ms |
20084 KB |
Output is correct |
9 |
Correct |
866 ms |
22660 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
16 ms |
14688 KB |
Output is correct |
13 |
Correct |
12 ms |
14676 KB |
Output is correct |
14 |
Correct |
17 ms |
14600 KB |
Output is correct |
15 |
Correct |
59 ms |
16368 KB |
Output is correct |
16 |
Correct |
134 ms |
18124 KB |
Output is correct |
17 |
Correct |
165 ms |
20044 KB |
Output is correct |
18 |
Correct |
848 ms |
22680 KB |
Output is correct |
19 |
Execution timed out |
1575 ms |
32128 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1590 ms |
29844 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
29848 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |