#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int K = 20;
const int INF = 1e6+10;
int up[K][N], d[K][N], s[N], lf[N], e[N], mx[N], idx[N], n, q;
vector<pii> v;
vector<int> add[N], rm[N];
bool cmp(pii a, pii b) {
if (a.first == b.first) return s[a.second] < s[b.second];
else return a.first < b.first;
}
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(), cmp);
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);
}
for (int i=0; i<n; ++i) idx[v[i].second]=i;
multiset<pii> ms;
for (int i=0; i<n; ++i) {
for (auto u : add[i]) ms.insert({e[u], idx[u]});
for (auto u : rm[i]) ms.erase(ms.find({e[u], idx[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]];
}
}
/*
cout<<"v: ";
for (int i=0; i<n; ++i) cout<<v[i].first<<" ";
cout<<"\n";
cout<<"idx: ";
for (int i=0; i<n; ++i) cout<<idx[i]<<" ";
cout<<"\n";
cout<<"mx: ";
for (int i=0; i<n; ++i) cout<<mx[i]<<" ";
cout<<"\n";
*/
while (q--) {
int l, r; cin>>l>>r; --l, --r;
l=idx[l], r=idx[r];
//cout<<"QUE "<<l<<", "<<r<<"\n";
if (l == r) {
cout<<0<<"\n";
continue;
}
if (v[r].first < v[l].first) {
cout<<"impossible\n";
continue;
}
int ans=0;
for (int j=K-1; j>=0; --j) {
//cout<<"HERE "<<up[j][l]<<"\n";
if (up[j][l] <= r) ans+=d[j][l], l=up[j][l];
}
if (l == r) ans+=0;
else {
if (s[v[r].second] <= e[v[l].second] && e[v[l].second] <= e[v[r].second]) ++ans, l=r;
else {
cout<<"impossible\n";
continue;
}
}
assert(l == r);
cout<<ans<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21140 KB |
Output is correct |
2 |
Correct |
72 ms |
30240 KB |
Output is correct |
3 |
Correct |
75 ms |
33480 KB |
Output is correct |
4 |
Correct |
80 ms |
33488 KB |
Output is correct |
5 |
Correct |
79 ms |
33504 KB |
Output is correct |
6 |
Correct |
71 ms |
33428 KB |
Output is correct |
7 |
Correct |
81 ms |
33540 KB |
Output is correct |
8 |
Correct |
67 ms |
33992 KB |
Output is correct |
9 |
Correct |
67 ms |
33760 KB |
Output is correct |
10 |
Correct |
88 ms |
33660 KB |
Output is correct |
11 |
Correct |
77 ms |
33740 KB |
Output is correct |
12 |
Correct |
61 ms |
31436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21260 KB |
Output is correct |
4 |
Correct |
3 ms |
21144 KB |
Output is correct |
5 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21260 KB |
Output is correct |
4 |
Correct |
3 ms |
21144 KB |
Output is correct |
5 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21260 KB |
Output is correct |
4 |
Correct |
3 ms |
21144 KB |
Output is correct |
5 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
30316 KB |
Output is correct |
2 |
Correct |
73 ms |
33288 KB |
Output is correct |
3 |
Correct |
83 ms |
33448 KB |
Output is correct |
4 |
Correct |
68 ms |
33944 KB |
Output is correct |
5 |
Correct |
78 ms |
33740 KB |
Output is correct |
6 |
Correct |
79 ms |
33484 KB |
Output is correct |
7 |
Correct |
93 ms |
33420 KB |
Output is correct |
8 |
Correct |
79 ms |
32208 KB |
Output is correct |
9 |
Correct |
57 ms |
33488 KB |
Output is correct |
10 |
Correct |
73 ms |
33212 KB |
Output is correct |
11 |
Correct |
93 ms |
33556 KB |
Output is correct |
12 |
Correct |
72 ms |
33232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21140 KB |
Output is correct |
2 |
Correct |
72 ms |
30240 KB |
Output is correct |
3 |
Correct |
75 ms |
33480 KB |
Output is correct |
4 |
Correct |
80 ms |
33488 KB |
Output is correct |
5 |
Correct |
79 ms |
33504 KB |
Output is correct |
6 |
Correct |
71 ms |
33428 KB |
Output is correct |
7 |
Correct |
81 ms |
33540 KB |
Output is correct |
8 |
Correct |
67 ms |
33992 KB |
Output is correct |
9 |
Correct |
67 ms |
33760 KB |
Output is correct |
10 |
Correct |
88 ms |
33660 KB |
Output is correct |
11 |
Correct |
77 ms |
33740 KB |
Output is correct |
12 |
Correct |
61 ms |
31436 KB |
Output is correct |
13 |
Correct |
3 ms |
21336 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21260 KB |
Output is correct |
16 |
Correct |
3 ms |
21144 KB |
Output is correct |
17 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |