#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, q, idord[maxn], lf[maxn], rg[maxn], nx[maxn][25], pv[maxn][25];
void solve() {
cin >> n >> q;
vector<pair<pair<int,int>,int>> ord;
vector<int> cc;
for(int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
ord.pb(mp(mp(r,l),i));
lf[i] = l;
rg[i] = r;
cc.pb(lf[i]);
cc.pb(rg[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
for(int i = 1; i <= n; i++) {
lf[i] = upper_bound(all(cc),lf[i])-cc.begin();
rg[i] = upper_bound(all(cc),rg[i])-cc.begin();
}
// sort(all(ord));
for(int i = 1; i <= n; i++) {
nx[i][0] = 0;
for(int j = 1; j <= n; j++) {
if(j == i || !(lf[j] <= rg[i] && rg[i] <= rg[j]) || !(rg[j] > rg[i])) continue;
if(nx[i][0] == 0 || rg[j] > rg[nx[i][0]]) nx[i][0] = j;
}
pv[i][0] = 0;
for(int j = 1; j <= n; j++) {
if(j == i || !(lf[i] <= rg[j] && rg[j] <= rg[i]) || !(lf[j] < lf[i])) continue;
if(pv[i][0] == 0 || lf[j] < lf[pv[i][0]]) pv[i][0] = j;
}
// int l = i+1;
// int r = n;
// while(l <= r) {
// int mid = (l+r)/2;
// if(lf[mid] <= rg[i]) {
// nx[i][0] = mid;
// l = mid+1;
// }
// else {
// r = mid-1;
// }
// }
// cout << i << " " << nx[i][0] << " " << pv[i][0] << endl;
}
for(int j = 1; j <= 20; j++) {
for(int i = 1; i <= n; i++) {
nx[i][j] = nx[nx[i][j-1]][j-1];
pv[i][j] = pv[pv[i][j-1]][j-1];
}
}
while(q--) {
int s,e; cin >> s >> e;
int ans = 0;
for(int j = 20; j >= 0; j--) {
if(nx[s][j] != 0 && rg[nx[s][j]] < rg[e]) {
ans+= (1<<j);
s = nx[s][j];
}
}
for(int j = 20; j >= 0; j--) {
if(pv[e][j] != 0 && lf[pv[e][j]] > lf[s]) {
ans+= (1<<j);
e = pv[e][j];
}
}
if(s == e) {
cout << ans << endl;
}
else if(lf[e] <= rg[s] && rg[s] <= rg[e]) {
cout << ans+1 << endl;
}
else if(nx[s][0] != 0 && lf[e] <= rg[nx[s][0]] && rg[nx[s][0]] <= rg[e]) {
cout << ans+2 << endl;
}
else if(pv[e][0] != 0 && lf[pv[e][0]] <= rg[s] && rg[s] <= rg[pv[e][0]]) {
cout << ans+2 << endl;
}
else {
cout << "impossible" << endl;
}
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1591 ms |
6936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
9 ms |
700 KB |
Output is correct |
7 |
Correct |
11 ms |
748 KB |
Output is correct |
8 |
Correct |
9 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
9 ms |
700 KB |
Output is correct |
7 |
Correct |
11 ms |
748 KB |
Output is correct |
8 |
Correct |
9 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
13 |
Correct |
9 ms |
820 KB |
Output is correct |
14 |
Correct |
8 ms |
728 KB |
Output is correct |
15 |
Correct |
9 ms |
704 KB |
Output is correct |
16 |
Correct |
12 ms |
808 KB |
Output is correct |
17 |
Correct |
8 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
260 ms |
3148 KB |
Output is correct |
20 |
Correct |
223 ms |
3212 KB |
Output is correct |
21 |
Correct |
251 ms |
3504 KB |
Output is correct |
22 |
Correct |
271 ms |
3632 KB |
Output is correct |
23 |
Correct |
328 ms |
3452 KB |
Output is correct |
24 |
Correct |
245 ms |
3416 KB |
Output is correct |
25 |
Correct |
220 ms |
3012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
9 ms |
700 KB |
Output is correct |
7 |
Correct |
11 ms |
748 KB |
Output is correct |
8 |
Correct |
9 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
13 |
Correct |
9 ms |
744 KB |
Output is correct |
14 |
Correct |
7 ms |
724 KB |
Output is correct |
15 |
Correct |
10 ms |
724 KB |
Output is correct |
16 |
Correct |
11 ms |
804 KB |
Output is correct |
17 |
Correct |
9 ms |
720 KB |
Output is correct |
18 |
Correct |
5 ms |
724 KB |
Output is correct |
19 |
Execution timed out |
1586 ms |
6708 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1558 ms |
6724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1591 ms |
6936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |