#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define oo 1e9
using namespace std;
struct DATA{
int mn = oo, id;
};
const int MAX = 1e5 + 5, LOGMAX = 21;
int n, q;
pii arr[MAX];
DATA tree[8 * MAX];
DATA comb(DATA a, DATA b){
if(a.mn < b.mn){
return a;
}
else{
return b;
}
}
void update(int node, int l, int r, int pos, int val, int id){
if(l == r){
tree[node] = comb({val, id}, tree[node]);
return;
}
int mid = (l + r) / 2;
if(pos <= mid){
update(2 * node, l, mid, pos, val, id);
}
else{
update(2 * node + 1, mid + 1, r, pos, val, id);
}
tree[node] = comb(tree[2 * node], tree[2 * node + 1]);
}
DATA ask(int node, int l, int r, int ql, int qr){
if(qr < l || r < ql) return DATA();
if(ql <= l && r <= qr) return tree[node];
int mid = (l + r) / 2;
return comb(ask(2 * node, l, mid, ql, qr), ask(2 * node + 1, mid + 1, r, ql, qr));
}
void compress(){
vector<pii> v;
for(int i = 1; i <= n; i++){
v.push_back({arr[i].first, i});
v.push_back({arr[i].second, -i});
}
sort(v.begin(), v.end());
int t = 0;
for(int i = 0; i < 2 * n; i++){
if(i == 0 || v[i].first != v[i - 1].first){
t++;
}
if(v[i].second > 0){
arr[v[i].second].first = t;
}
else{
arr[-v[i].second].second = t;
}
}
}
int par[LOGMAX][MAX];
bool isReachable(int i, int j){
return arr[j].first <= arr[i].second && arr[i].second <= arr[j].second;
}
int lift(int s, int d){
int res = 2;
if(isReachable(s, d)){
return 1;
}
for(int i = LOGMAX - 1; i >= 0; i--){
if(!isReachable(s, par[i][d]) && arr[par[i][d]].second >= arr[s].second){
d = par[i][d];
res += (1 << i);
}
}
if(!isReachable(s, par[0][d])){
return -1;
}
return res;
}
int main(){
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> arr[i].first >> arr[i].second;
}
compress();
for(int i = 1; i <= n; i++){
update(1, 1, 2 * n, arr[i].second, arr[i].first, i);
}
for(int i = 1; i <= n; i++){
par[0][i] = ask(1, 1, 2 * n, arr[i].first, arr[i].second).id;
}
for(int j = 1; j < LOGMAX; j++){
for(int i = 1; i <= n; i++){
par[j][i] = par[j - 1][par[j - 1][i]];
}
}
while(q--){
int s, d; cin >> s >> d;
int ans = lift(s, d);
if(ans == -1){
cout << "impossible\n";
}
else{
cout << ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
19348 KB |
Output is correct |
2 |
Correct |
274 ms |
19352 KB |
Output is correct |
3 |
Correct |
301 ms |
19324 KB |
Output is correct |
4 |
Correct |
288 ms |
19972 KB |
Output is correct |
5 |
Incorrect |
287 ms |
19848 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |