#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
const int MAX_LOG_N = 16;
const int MAX_T = 2e5;
const int MAX_LOG_T = 17;
struct valpos {
int val, pos;
bool operator < ( const valpos &x ) const {
return val < x.val;
}
};
struct event {
int i, s, e;
};
int prv[MAX_LOG_N + 1][MAX_N + 1];
valpos minim[MAX_LOG_T + 1][MAX_T + 1];
event events[MAX_N + 1];
map<int, int> nrm;
bool canSwitch( event a, event b ) {
return (b.s <= a.e && a.e <= b.e);
}
valpos queryMinim( int l, int r ) {
int p = log2( r - l + 1 );
return min( minim[p][l], minim[p][r - (1 << p)] );
}
int main() {
int n, q;
cin >> n >> q;
for ( int i = 1; i <= n; i++ ) {
cin >> events[i].s >> events[i].e;
nrm[events[i].s] = nrm[events[i].e] = 1;
events[i].i = i + 1;
}
int t = 0;
for ( auto p: nrm )
nrm[p.first] = ++t;
for ( int i = 1; i <= n; i++ ) {
events[i].s = nrm[events[i].s];
events[i].e = nrm[events[i].e];
}
for ( int i = 1; i <= t; i++ )
minim[0][i] = { t + 1, 0 };
for ( int i = 1; i <= n; i++ )
minim[0][events[i].e] = min( minim[0][events[i].e], { events[i].s, i } );
for ( int p = 1; p <= log2( t ); p++ ) {
for ( int i = 1; i <= t - (1 << p) + 1; i++ )
minim[p][i] = min( minim[p - 1][i], minim[p - 1][i + (1 << (p - 1))] );
}
for ( int i = 1; i <= n; i++ )
prv[0][i] = queryMinim( events[i].s, events[i].e ).pos;
for ( int p = 1; p <= log2( n ); p++ ) {
for ( int i = 1; i <= n; i++ )
prv[p][i] = prv[p - 1][prv[p - 1][i]];
}
while ( q-- ) {
int s, e;
cin >> s >> e;
int i = e, x = 0;
for ( int p = log2( n ); p >= 0; p-- ) {
if ( events[prv[p][i]].s > events[s].e ) {
i = prv[p][i];
x += (1 << p);
}
}
if ( events[i].s > events[s].e ) {
i = prv[0][i];
x++;
}
if ( s == e )
cout << 0 << "\n";
else if ( canSwitch( events[s], events[i] ) )
cout << x + 1 << "\n";
else
cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
305 ms |
40564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |