#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[100005];
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++;
}
}
while(q--){
int x, y; cin >> x >> y;
queue<int>qu;
for(int i=1;i<=n;i++)dist[i] = 1e9;
dist[x] = 0;
qu.push(x);
while(!qu.empty()){
int a = qu.front(); qu.pop();
for(auto i : lmao[a]){
if(dist[i] > dist[a] + 1)dist[i] = dist[a] + 1, qu.push(i);
}
}
if(dist[y] != 1e9)cout << dist[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:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1572 ms |
14352 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
9 ms |
4436 KB |
Output is correct |
7 |
Correct |
21 ms |
6228 KB |
Output is correct |
8 |
Correct |
29 ms |
8276 KB |
Output is correct |
9 |
Correct |
100 ms |
10836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
9 ms |
4436 KB |
Output is correct |
7 |
Correct |
21 ms |
6228 KB |
Output is correct |
8 |
Correct |
29 ms |
8276 KB |
Output is correct |
9 |
Correct |
100 ms |
10836 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
3 ms |
2772 KB |
Output is correct |
15 |
Correct |
10 ms |
4436 KB |
Output is correct |
16 |
Correct |
20 ms |
6240 KB |
Output is correct |
17 |
Correct |
26 ms |
8276 KB |
Output is correct |
18 |
Correct |
113 ms |
10872 KB |
Output is correct |
19 |
Execution timed out |
1586 ms |
62024 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
9 ms |
4436 KB |
Output is correct |
7 |
Correct |
21 ms |
6228 KB |
Output is correct |
8 |
Correct |
29 ms |
8276 KB |
Output is correct |
9 |
Correct |
100 ms |
10836 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
4 ms |
2800 KB |
Output is correct |
14 |
Correct |
3 ms |
2772 KB |
Output is correct |
15 |
Correct |
9 ms |
4436 KB |
Output is correct |
16 |
Correct |
21 ms |
6248 KB |
Output is correct |
17 |
Correct |
26 ms |
8280 KB |
Output is correct |
18 |
Correct |
105 ms |
10860 KB |
Output is correct |
19 |
Correct |
918 ms |
14400 KB |
Output is correct |
20 |
Correct |
701 ms |
20232 KB |
Output is correct |
21 |
Execution timed out |
1542 ms |
136840 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1581 ms |
14300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1572 ms |
14352 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |