#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, q, where[N], pref[N], tin[N], tout[N], tim, depth[N];
struct Event{
int l, r;
bool operator < (const Event &sc) const{
if(r == sc.r) return l < sc.l;
else return r < sc.r;
}
};
Event arr[N];
vector<pair<Event, int>> v;
vector<int> tree[N];
bool vis[N];
void dfs(int v, int p){
tin[v] = ++tim;
depth[v] = depth[p] + 1;
vis[v] = true;
for(auto e : tree[v]){
if(e != p) dfs(e, v);
}
tout[v] = ++tim;
}
bool isanc(int a, int b){
return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
bool canmove(Event a, Event b){
return b.l <= a.r && a.r <= b.r;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> arr[i].l >> arr[i].r;
for(int i = 1; i <= n; i++) v.push_back({arr[i], i});
sort(v.begin(), v.end());
set<pair<int, int>> st;
for(auto p : v){
while(!st.empty() && prev(st.end())->first >= p.first.l){
tree[p.second].push_back(prev(st.end())->second);
st.erase(prev(st.end()));
}
st.insert({p.first.r, p.second});
}
for(int i = n - 1; i >= 0; i--){
if(!vis[v[i].second]){
dfs(v[i].second, 0);
}
}
while(q--){
int s, e;
cin >> s >> e;
if(isanc(e, s)) cout << depth[s] - depth[e] << "\n";
else{
if(canmove(arr[s], arr[e])) cout << 1 << "\n";
else cout << "impossible\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
95 ms |
14248 KB |
Output is correct |
3 |
Correct |
83 ms |
14272 KB |
Output is correct |
4 |
Correct |
73 ms |
14256 KB |
Output is correct |
5 |
Correct |
65 ms |
13496 KB |
Output is correct |
6 |
Correct |
73 ms |
12832 KB |
Output is correct |
7 |
Correct |
70 ms |
12660 KB |
Output is correct |
8 |
Correct |
68 ms |
11608 KB |
Output is correct |
9 |
Correct |
68 ms |
11696 KB |
Output is correct |
10 |
Correct |
68 ms |
12260 KB |
Output is correct |
11 |
Correct |
67 ms |
10572 KB |
Output is correct |
12 |
Correct |
59 ms |
11088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
14304 KB |
Output is correct |
2 |
Correct |
69 ms |
14272 KB |
Output is correct |
3 |
Correct |
86 ms |
14312 KB |
Output is correct |
4 |
Correct |
70 ms |
11596 KB |
Output is correct |
5 |
Correct |
74 ms |
12356 KB |
Output is correct |
6 |
Incorrect |
73 ms |
14500 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
95 ms |
14248 KB |
Output is correct |
3 |
Correct |
83 ms |
14272 KB |
Output is correct |
4 |
Correct |
73 ms |
14256 KB |
Output is correct |
5 |
Correct |
65 ms |
13496 KB |
Output is correct |
6 |
Correct |
73 ms |
12832 KB |
Output is correct |
7 |
Correct |
70 ms |
12660 KB |
Output is correct |
8 |
Correct |
68 ms |
11608 KB |
Output is correct |
9 |
Correct |
68 ms |
11696 KB |
Output is correct |
10 |
Correct |
68 ms |
12260 KB |
Output is correct |
11 |
Correct |
67 ms |
10572 KB |
Output is correct |
12 |
Correct |
59 ms |
11088 KB |
Output is correct |
13 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |