#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int K = 5;
const int INF = 1e6+10;
int up[K][N], d[K][N], s[N], lf[N], e[N], mx[N], n, q;
vector<pii> v;
vector<int> add[N], rm[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin>>n>>q;
for (int i=0; i<n; ++i) {
cin>>s[i]>>e[i];
v.push_back({e[i], i});
} sort(v.begin(), v.end());
for (int i=0; i<n; ++i) {
int j=lower_bound(v.begin(), v.end(), make_pair(s[v[i].second], -1))-v.begin();
lf[i]=j;
add[lf[i]].push_back(v[i].second);
rm[i+1].push_back(v[i].second);
}
multiset<pii> ms;
for (int i=0; i<n; ++i) {
for (auto u : add[i]) ms.insert({e[u], u});
for (auto u : rm[i]) ms.erase(ms.find({e[u], u}));
mx[i]=ms.rbegin()->second;
}
for (int i=n-1; i>=0; --i) {
up[0][i]=mx[i], d[0][i]=(mx[i] != i);
for (int j=1; j<K; ++j) {
up[j][i]=up[j-1][up[j-1][i]];
d[j][i]=d[j-1][i]+d[j-1][up[j-1][i]];
}
}
while (q--) {
int l, r; cin>>l>>r; --l, --r;
if (e[r] < e[l]) {
cout<<"impossible\n";
continue;
}
int ans=0;
for (int j=K-1; j>=0; --j) {
if (up[j][l] <= r) ans+=d[j][l], l=up[j][l];
}
if (l == r) ans+=0;
else {
if (s[r] <= e[l]) ++ans, l=r;
else {
cout<<"impossible\n";
continue;
}
}
assert(l == r);
cout<<ans<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
56 ms |
21788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
21888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
56 ms |
21788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |