//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 20;
const int mod = 998244353;
void solve(){
int n, q; cin >> n >> q;
vector<T>a(n);
vector<int>s;
for (auto &[l, r]: a){
cin >> l >> r;
s.emplace_back(l);
s.emplace_back(r);
}
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
auto get = [&](int x){ return (int)(lower_bound(s.begin(), s.end(), x)-s.begin());};
for (auto &[l, r]: a){
l = get(l); r = get(r);
debug(l, r);
}
int m = (int)s.size();
vector<vector<int>>L(m), R(m);
for (int i = 0; i<n; i++){
auto [l, r] = a[i];
L[l].emplace_back(i);
R[r].emplace_back(i);
}
set<T>secik; //r, i
vector up(n+1, vector<int>(K));
for (int rep = 0; rep<m; rep++){
for (auto i: L[rep]){
secik.insert({a[i].second, i});
}
for (auto i: R[rep]){
secik.erase({rep, i});
if (!secik.empty()) up[i][0] = secik.rbegin()->second;
else up[i][0] = i;
debug(i, up[i][0]);
secik.insert({rep, i});
}
for (auto i: R[rep]){
secik.erase({rep, i});
}
}
for (int j = 1; j<K; j++){
for (int i = 0; i<n;i++){
up[i][j] = up[up[i][j-1]][j-1];
}
}
while (q--){
int l, r; cin >> l >> r;
--l;--r;
int ans = oo;
int ll = 0, rr = n;
while (rr >= ll){
int mid = (ll+rr)/2;
int curr = l;
for (int i = K-1; i>=0; i--){
// debug(i, l, up[l][i]);
if (mid&(1<<i)){
curr = up[curr][i];
}
}
if (a[curr].second > a[r].second){
rr = mid-1;
} else if (a[curr].second >= a[r].first){
ans = mid;
rr = mid-1;
} else {
ll = mid+1;
}
}
if (ans == oo) {
cout << "impossible\n";
continue;
}
for (int i = K-1; i>=0; i--){
if (ans&(1<<i)){
l = up[l][i];
}
}
debug(ans);
if (a[l].second < a[r].first || a[l].second > a[r].second){
cout << "impossible\n";
continue;
}
if (l != r) ans++;
cout << ans << "\n";
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
199 ms |
37624 KB |
Output is correct |
3 |
Correct |
306 ms |
37768 KB |
Output is correct |
4 |
Correct |
333 ms |
37648 KB |
Output is correct |
5 |
Correct |
280 ms |
38336 KB |
Output is correct |
6 |
Correct |
255 ms |
38604 KB |
Output is correct |
7 |
Correct |
237 ms |
38912 KB |
Output is correct |
8 |
Correct |
181 ms |
42884 KB |
Output is correct |
9 |
Correct |
197 ms |
42884 KB |
Output is correct |
10 |
Correct |
368 ms |
38204 KB |
Output is correct |
11 |
Correct |
325 ms |
39948 KB |
Output is correct |
12 |
Correct |
117 ms |
34336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
728 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
728 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
728 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
37832 KB |
Output is correct |
2 |
Correct |
293 ms |
37752 KB |
Output is correct |
3 |
Correct |
392 ms |
37736 KB |
Output is correct |
4 |
Correct |
190 ms |
42932 KB |
Output is correct |
5 |
Correct |
357 ms |
38092 KB |
Output is correct |
6 |
Correct |
353 ms |
42700 KB |
Output is correct |
7 |
Correct |
321 ms |
42444 KB |
Output is correct |
8 |
Correct |
215 ms |
42700 KB |
Output is correct |
9 |
Correct |
151 ms |
46792 KB |
Output is correct |
10 |
Correct |
218 ms |
42236 KB |
Output is correct |
11 |
Correct |
208 ms |
43632 KB |
Output is correct |
12 |
Correct |
244 ms |
42188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
199 ms |
37624 KB |
Output is correct |
3 |
Correct |
306 ms |
37768 KB |
Output is correct |
4 |
Correct |
333 ms |
37648 KB |
Output is correct |
5 |
Correct |
280 ms |
38336 KB |
Output is correct |
6 |
Correct |
255 ms |
38604 KB |
Output is correct |
7 |
Correct |
237 ms |
38912 KB |
Output is correct |
8 |
Correct |
181 ms |
42884 KB |
Output is correct |
9 |
Correct |
197 ms |
42884 KB |
Output is correct |
10 |
Correct |
368 ms |
38204 KB |
Output is correct |
11 |
Correct |
325 ms |
39948 KB |
Output is correct |
12 |
Correct |
117 ms |
34336 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
728 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |