#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5, LOG=17;
int n;
int l[MAXN], r[MAXN];
bool rcompare(int x, int y)
{
if (r[x]<r[y])
return true;
return false;
}
bool lcompare(int x, int y)
{
if (l[x]<l[y])
return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> n >> q;
for (int i=0; i<n; i++)
cin >> l[i] >> r[i];
int sorted[n];
for (int i=0; i<n; i++)
sorted[i]=i;
sort(sorted, sorted+n, rcompare);
int ranked[n];
for (int i=0; i<n; i++)
ranked[sorted[i]]=i;
int lsorted[n];
for (int i=0; i<n; i++)
lsorted[i]=i;
sort(lsorted, lsorted+n, lcompare);
int t=n-1;
set<int> farthest;
for (int i=0; i<n; i++)
farthest.insert(i);
int blift[n][LOG];
for (int i=0; i<n; i++)
for (int j=0; j<LOG; j++)
blift[i][j]=-1;
for (int i=n-1; i>=0; i--)
{
while (t>=0 && l[lsorted[t]]>r[sorted[i]])
{
farthest.erase(ranked[lsorted[t]]);
t=t-1;
}
auto it=farthest.end();
it--;
blift[i][0]=*it;
}
for (int j=1; j<LOG; j++)
for (int i=0; i<n; i++)
blift[i][j]=blift[blift[i][j-1]][j-1];
/*for (int i=0; i<n; i++)
cout << ranked[i] << ' ';
cout << '\n';
for (int i=0; i<n; i++)
cout << blift[i][0] << ' ';
cout << '\n';*/
while (q--)
{
int x, y;
cin >> x >> y;
x=ranked[x-1];
y=ranked[y-1];
if (x>y)
{
cout << "impossible" << '\n';
continue;
}
if (x==y)
{
cout << 0 << '\n';
continue;
}
int total=1;
for (int j=LOG-1; j>=0; j--)
if (blift[x][j]<y)
{
x=blift[x][j];
total=total+(1<<j);
}
cout << total << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
17372 KB |
Output is correct |
2 |
Correct |
68 ms |
17352 KB |
Output is correct |
3 |
Correct |
84 ms |
17492 KB |
Output is correct |
4 |
Incorrect |
68 ms |
17488 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |