#include<bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
template<typename T>
int len(T &a){
return a.size();
}
using ll = long long;
const int inf = 1e9;
const ll infl = 1e18;
#define ff first
#define ss second
struct Sparse{
vector<vector<pair<int,int>>> t;
int n, logn;
pair<int,int> join(pair<int,int> a, pair<int,int> b){
return min(a, b);
}
auto build(vector<int> &a){
n = len(a);
logn = 32 - __builtin_clz(n);
t.assign(n, vector<pair<int,int>> (logn, pair{inf, 0}));
for(int i = 0; i < n; i ++){
t[i][0] = {a[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) <= n; i ++){
t[i][j] = join(t[i][j - 1], t[i + (1 << (j - 1))][j - 1]);
}
}
}
int get(int l, int r){
if(l > r) return inf;
int len = r - l + 1;
len = 31 - __builtin_clz(len);
return join(t[l][len], t[r - (1 << len) + 1][len]).ss;
}
};
void solve(){
int n, q; cin >> n >> q;
vector<tuple<int,int,int>> e(n);
vector<pair<int,int>> ev(n);
for(int i = 0; i < n; i ++){
auto &[r, l, id] = e[i];
cin >> l >> r;
ev[i] = {l, r};
//l = -l;
id = i;
}
sort(all(e));
Sparse t;
vector<int> lef(n);
vector<int> pos(n);
for(int i = 0; i < n; i ++){
auto [r, l, id] = e[i];
//l = -l;
lef[i] = l;
pos[id] = i;
//cout << r << ' ' << l << endl;
}
t.build(lef);
//for(auto u : lef) cout << u << ' '; cout << endl;
vector<int> g(n);
iota(all(g), 0);
for(int i = 0; i < n; i ++){
auto [r, l, id] = e[i];
//l = -l;
int ch = lower_bound(all(e), tuple{l, -1, -1}) - e.begin();
//ch --;
//cout << i << ' ' << ch << endl;
int mn = t.get(ch, i);
auto [rr, ll, idd] = e[mn];
g[id] = idd;
}
// for(int i = 0; i < n; i ++) cout << i << ' ' << g[i] << endl;
while(q --){
int a, b; cin >> a >> b; a --; b --;
if(a == b){cout << "0\n"; continue;}
bool ok = 0;
int d = 0;
while(g[b] != b){
d ++;
if(ev[a].ss > ev[b].ss){break;}
if(ev[a].ss >= ev[b].ff){
ok = 1; break;
}
b = g[b];
}
if(ev[a].ss >= ev[b].ff && ok == 0){
d ++; ok = 1;
}
if(ok) cout << d << '\n'; else cout << "impossible\n";
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int testcases = 1;
//cin >> testcases;
while(testcases --){
solve();
if(testcases) cout << '\n';
#ifdef LOCAL
else cout << '\n';
cout << "__________________" << endl;
#endif
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1571 ms |
19892 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1537 ms |
19888 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1571 ms |
19892 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |