#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i,n) for (int i=0; i<n; ++i)
#define pb(x) push_back(x);
#define f first
#define s second
void solve() {
int n, m; cin>>n>>m;
vector<pair<int,int>> a(n);
forn(i,n) cin>>a[i].first>>a[i].second;
vector<vector<int>> adj(n);
if (n<=5000) {
forn(i,n) {
forn(j,n) {
if (i==j) continue;
if (a[j].f<=a[i].s && a[i].s<=a[j].s) adj[i].pb(j);
}
}
} else {
set<pair<int,int>> s;
forn(i,n) s.insert({a[i].s,i});
auto p = s.end(); --p;
for (;;--p) {
auto x=*p; int i=x.s;
auto it = s.upper_bound({a[i].f,-1});
auto z=*it; if (z.f<a[x.s].f) ++it;
while (it!=s.end() && (*it).f<=x.f) {
if ((*it).s!=i) adj[(*it).s].pb(i);
++it;
}
if (p==s.begin()) break;
}
}
vector<int> vis(n,0);
vector<int> dist(n,-1);
forn(i,m) {
int u,v; cin>>u>>v;
--u, --v;
queue<int> q;
q.push(u);
vis[u]=1, dist[u]=0;
while (!q.empty()) {
int u=q.front(); q.pop();
if (u==v) break;
for (auto v:adj[u]) {
if (!vis[v]) {
vis[v]=1;
dist[v]=dist[u]+1;
q.push(v);
}
}
}
if (dist[v]==-1) cout<<"impossible\n";
else cout<<dist[v]<<'\n';
dist.assign(n,-1);
vis.assign(n,0);
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1564 ms |
13880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
1892 KB |
Output is correct |
7 |
Correct |
25 ms |
3548 KB |
Output is correct |
8 |
Correct |
21 ms |
5716 KB |
Output is correct |
9 |
Correct |
52 ms |
8408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
1892 KB |
Output is correct |
7 |
Correct |
25 ms |
3548 KB |
Output is correct |
8 |
Correct |
21 ms |
5716 KB |
Output is correct |
9 |
Correct |
52 ms |
8408 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
348 KB |
Output is correct |
13 |
Correct |
5 ms |
412 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
9 ms |
1876 KB |
Output is correct |
16 |
Correct |
18 ms |
3568 KB |
Output is correct |
17 |
Correct |
19 ms |
5716 KB |
Output is correct |
18 |
Correct |
59 ms |
8404 KB |
Output is correct |
19 |
Execution timed out |
1588 ms |
54872 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
1892 KB |
Output is correct |
7 |
Correct |
25 ms |
3548 KB |
Output is correct |
8 |
Correct |
21 ms |
5716 KB |
Output is correct |
9 |
Correct |
52 ms |
8408 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
340 KB |
Output is correct |
13 |
Correct |
7 ms |
356 KB |
Output is correct |
14 |
Correct |
7 ms |
408 KB |
Output is correct |
15 |
Correct |
10 ms |
1880 KB |
Output is correct |
16 |
Correct |
18 ms |
3644 KB |
Output is correct |
17 |
Correct |
18 ms |
5716 KB |
Output is correct |
18 |
Correct |
47 ms |
8408 KB |
Output is correct |
19 |
Correct |
495 ms |
13880 KB |
Output is correct |
20 |
Correct |
278 ms |
18548 KB |
Output is correct |
21 |
Execution timed out |
1563 ms |
113080 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1565 ms |
13872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1564 ms |
13880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |