#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
const int MAX_LOG_N = 16;
const int MAX_T = 2e5;
struct event {
int i, s, e;
};
int prv[MAX_LOG_N + 1][MAX_N + 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);
}
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 <= n; i++ ) {
prv[0][i] = i;
for ( int j = 1; j <= n; j++ ) {
if ( canSwitch( events[j], events[i] ) ) {
if ( events[j].s < events[prv[0][i]].s )
prv[0][i] = j;
}
}
}
for ( int p = 1; p <= MAX_LOG_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 = MAX_LOG_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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
7108 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
6 ms |
6492 KB |
Output is correct |
4 |
Correct |
7 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
5 ms |
6492 KB |
Output is correct |
7 |
Correct |
7 ms |
6488 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
6 ms |
6492 KB |
Output is correct |
4 |
Correct |
7 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
5 ms |
6492 KB |
Output is correct |
7 |
Correct |
7 ms |
6488 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
5 ms |
6640 KB |
Output is correct |
13 |
Correct |
4 ms |
6644 KB |
Output is correct |
14 |
Correct |
4 ms |
6492 KB |
Output is correct |
15 |
Correct |
5 ms |
6492 KB |
Output is correct |
16 |
Correct |
6 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
256 ms |
7392 KB |
Output is correct |
20 |
Correct |
234 ms |
7716 KB |
Output is correct |
21 |
Correct |
238 ms |
8528 KB |
Output is correct |
22 |
Correct |
242 ms |
8824 KB |
Output is correct |
23 |
Correct |
260 ms |
8872 KB |
Output is correct |
24 |
Correct |
234 ms |
8784 KB |
Output is correct |
25 |
Correct |
238 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
6 ms |
6492 KB |
Output is correct |
4 |
Correct |
7 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
5 ms |
6492 KB |
Output is correct |
7 |
Correct |
7 ms |
6488 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6560 KB |
Output is correct |
12 |
Correct |
4 ms |
6492 KB |
Output is correct |
13 |
Correct |
5 ms |
6492 KB |
Output is correct |
14 |
Correct |
4 ms |
6492 KB |
Output is correct |
15 |
Correct |
6 ms |
6492 KB |
Output is correct |
16 |
Correct |
6 ms |
6492 KB |
Output is correct |
17 |
Correct |
4 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Execution timed out |
1595 ms |
7172 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1513 ms |
7252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
7108 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |