/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 17;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, q, rev[N], p[N], s[N], e[N], a[N];
pii seg[lg][N], cte=Mp((int)1e9, (int)-1e9);
bool cmp(int i, int j) {
return (e[i] < e[j]);
}
void upd(int id, int ind, pii x) {
while(ind > 0) {
seg[id][ind].F=min(seg[id][ind].F, x.F);
seg[id][ind].S=max(seg[id][ind].S, x.S);
ind /= 2;
}
}
pii query(int l, int r, int id, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(l >= rc || lc >= r) return cte;
if(lc >= l && rc <= r) return seg[id][ind];
int mid = (lc + rc)/2;
pii x = query(l, r, id, 2*ind, lc, mid);
pii y = query(l, r, id, 2*ind+1, mid, rc);
return Mp(min(x.F, y.F), max(x.S, y.S));
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>q;
for(int i=1; i<=n; i++) {
cin>>s[i]>>e[i]; p[i] = i;
}
sort(p+1, p+n+1, cmp);
for(int i=1; i<=n; i++) rev[p[i]] = i, a[i] = e[p[i]];
a[n+1] = 2e9;
for(int i=0; i<lg; i++) fill(seg[i], seg[i]+N, cte);
for(int i=1; i<=n; i++) {
int pl=lower_bound(a+1, a+n+2, s[p[i]]) - a;
int pr=upper_bound(a+1, a+n+2, e[p[i]]) - a - 1;
// cout<<p[i]<<" ==> "<<pl<<' '<<pr<<endl;
upd(0, (1<<lg)-1+i, Mp(pl, pr));
}
for(int i=1; i<lg; i++) {
for(int j=1; j<=n; j++) {
pii tmp = seg[i-1][(1<<lg)-1+j];
pii res = query(tmp.F, tmp.S+1, i-1);
upd(i, (1<<lg)-1+j, res);
}
}
for(int v,u,tc=1; tc<=q; tc++) {
cin>>v>>u;
if(v == u) {
cout<<0<<endl; continue;
}
v=rev[v], u=rev[u];
pii now = {u, u}; int ans = 0;
for(int i=lg-1; i>=0; i--) {
pii tmp = query(now.F, now.S+1, i);
if(v < tmp.F || v > tmp.S) {
ans += (1<<i);
now = tmp;
}
}
if(ans == (1<<lg) - 1) {
cout<<"impossible\n"; continue;
}
cout<<ans+1<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
45656 KB |
Output is correct |
2 |
Correct |
481 ms |
49456 KB |
Output is correct |
3 |
Correct |
545 ms |
49236 KB |
Output is correct |
4 |
Correct |
544 ms |
49464 KB |
Output is correct |
5 |
Correct |
524 ms |
49468 KB |
Output is correct |
6 |
Correct |
513 ms |
49476 KB |
Output is correct |
7 |
Correct |
494 ms |
49492 KB |
Output is correct |
8 |
Correct |
346 ms |
49872 KB |
Output is correct |
9 |
Correct |
358 ms |
49744 KB |
Output is correct |
10 |
Correct |
491 ms |
49748 KB |
Output is correct |
11 |
Correct |
447 ms |
49728 KB |
Output is correct |
12 |
Correct |
305 ms |
48976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45660 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45796 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
11 ms |
45660 KB |
Output is correct |
6 |
Correct |
7 ms |
45808 KB |
Output is correct |
7 |
Correct |
8 ms |
45656 KB |
Output is correct |
8 |
Correct |
10 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45660 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45796 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
11 ms |
45660 KB |
Output is correct |
6 |
Correct |
7 ms |
45808 KB |
Output is correct |
7 |
Correct |
8 ms |
45656 KB |
Output is correct |
8 |
Correct |
10 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45752 KB |
Output is correct |
10 |
Correct |
6 ms |
45660 KB |
Output is correct |
11 |
Correct |
5 ms |
45752 KB |
Output is correct |
12 |
Correct |
8 ms |
45660 KB |
Output is correct |
13 |
Correct |
8 ms |
45812 KB |
Output is correct |
14 |
Correct |
8 ms |
45776 KB |
Output is correct |
15 |
Correct |
8 ms |
45804 KB |
Output is correct |
16 |
Correct |
7 ms |
45656 KB |
Output is correct |
17 |
Correct |
7 ms |
45660 KB |
Output is correct |
18 |
Correct |
7 ms |
45660 KB |
Output is correct |
19 |
Correct |
205 ms |
47052 KB |
Output is correct |
20 |
Correct |
243 ms |
47168 KB |
Output is correct |
21 |
Correct |
200 ms |
47440 KB |
Output is correct |
22 |
Correct |
171 ms |
47440 KB |
Output is correct |
23 |
Correct |
158 ms |
47448 KB |
Output is correct |
24 |
Correct |
159 ms |
47244 KB |
Output is correct |
25 |
Correct |
181 ms |
46928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45660 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
8 ms |
45796 KB |
Output is correct |
4 |
Correct |
8 ms |
45660 KB |
Output is correct |
5 |
Correct |
11 ms |
45660 KB |
Output is correct |
6 |
Correct |
7 ms |
45808 KB |
Output is correct |
7 |
Correct |
8 ms |
45656 KB |
Output is correct |
8 |
Correct |
10 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45752 KB |
Output is correct |
10 |
Correct |
6 ms |
45656 KB |
Output is correct |
11 |
Correct |
5 ms |
45660 KB |
Output is correct |
12 |
Correct |
8 ms |
45660 KB |
Output is correct |
13 |
Correct |
8 ms |
45800 KB |
Output is correct |
14 |
Correct |
7 ms |
45800 KB |
Output is correct |
15 |
Correct |
8 ms |
45656 KB |
Output is correct |
16 |
Correct |
7 ms |
45808 KB |
Output is correct |
17 |
Correct |
7 ms |
45660 KB |
Output is correct |
18 |
Correct |
7 ms |
45660 KB |
Output is correct |
19 |
Correct |
253 ms |
47484 KB |
Output is correct |
20 |
Correct |
272 ms |
46676 KB |
Output is correct |
21 |
Correct |
199 ms |
47620 KB |
Output is correct |
22 |
Correct |
242 ms |
47720 KB |
Output is correct |
23 |
Correct |
192 ms |
47700 KB |
Output is correct |
24 |
Correct |
238 ms |
47700 KB |
Output is correct |
25 |
Correct |
131 ms |
47040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
470 ms |
49492 KB |
Output is correct |
2 |
Correct |
524 ms |
49492 KB |
Output is correct |
3 |
Correct |
565 ms |
49220 KB |
Output is correct |
4 |
Correct |
336 ms |
49744 KB |
Output is correct |
5 |
Correct |
494 ms |
49744 KB |
Output is correct |
6 |
Correct |
493 ms |
49492 KB |
Output is correct |
7 |
Correct |
484 ms |
49488 KB |
Output is correct |
8 |
Correct |
543 ms |
49720 KB |
Output is correct |
9 |
Correct |
186 ms |
47708 KB |
Output is correct |
10 |
Correct |
439 ms |
49236 KB |
Output is correct |
11 |
Correct |
372 ms |
48952 KB |
Output is correct |
12 |
Correct |
489 ms |
49232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
45656 KB |
Output is correct |
2 |
Correct |
481 ms |
49456 KB |
Output is correct |
3 |
Correct |
545 ms |
49236 KB |
Output is correct |
4 |
Correct |
544 ms |
49464 KB |
Output is correct |
5 |
Correct |
524 ms |
49468 KB |
Output is correct |
6 |
Correct |
513 ms |
49476 KB |
Output is correct |
7 |
Correct |
494 ms |
49492 KB |
Output is correct |
8 |
Correct |
346 ms |
49872 KB |
Output is correct |
9 |
Correct |
358 ms |
49744 KB |
Output is correct |
10 |
Correct |
491 ms |
49748 KB |
Output is correct |
11 |
Correct |
447 ms |
49728 KB |
Output is correct |
12 |
Correct |
305 ms |
48976 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
45660 KB |
Output is correct |
15 |
Correct |
8 ms |
45796 KB |
Output is correct |
16 |
Correct |
8 ms |
45660 KB |
Output is correct |
17 |
Correct |
11 ms |
45660 KB |
Output is correct |
18 |
Correct |
7 ms |
45808 KB |
Output is correct |
19 |
Correct |
8 ms |
45656 KB |
Output is correct |
20 |
Correct |
10 ms |
45660 KB |
Output is correct |
21 |
Correct |
7 ms |
45752 KB |
Output is correct |
22 |
Correct |
6 ms |
45660 KB |
Output is correct |
23 |
Correct |
5 ms |
45752 KB |
Output is correct |
24 |
Correct |
8 ms |
45660 KB |
Output is correct |
25 |
Correct |
8 ms |
45812 KB |
Output is correct |
26 |
Correct |
8 ms |
45776 KB |
Output is correct |
27 |
Correct |
8 ms |
45804 KB |
Output is correct |
28 |
Correct |
7 ms |
45656 KB |
Output is correct |
29 |
Correct |
7 ms |
45660 KB |
Output is correct |
30 |
Correct |
7 ms |
45660 KB |
Output is correct |
31 |
Correct |
205 ms |
47052 KB |
Output is correct |
32 |
Correct |
243 ms |
47168 KB |
Output is correct |
33 |
Correct |
200 ms |
47440 KB |
Output is correct |
34 |
Correct |
171 ms |
47440 KB |
Output is correct |
35 |
Correct |
158 ms |
47448 KB |
Output is correct |
36 |
Correct |
159 ms |
47244 KB |
Output is correct |
37 |
Correct |
181 ms |
46928 KB |
Output is correct |
38 |
Correct |
6 ms |
45656 KB |
Output is correct |
39 |
Correct |
5 ms |
45660 KB |
Output is correct |
40 |
Correct |
8 ms |
45660 KB |
Output is correct |
41 |
Correct |
8 ms |
45800 KB |
Output is correct |
42 |
Correct |
7 ms |
45800 KB |
Output is correct |
43 |
Correct |
8 ms |
45656 KB |
Output is correct |
44 |
Correct |
7 ms |
45808 KB |
Output is correct |
45 |
Correct |
7 ms |
45660 KB |
Output is correct |
46 |
Correct |
7 ms |
45660 KB |
Output is correct |
47 |
Correct |
253 ms |
47484 KB |
Output is correct |
48 |
Correct |
272 ms |
46676 KB |
Output is correct |
49 |
Correct |
199 ms |
47620 KB |
Output is correct |
50 |
Correct |
242 ms |
47720 KB |
Output is correct |
51 |
Correct |
192 ms |
47700 KB |
Output is correct |
52 |
Correct |
238 ms |
47700 KB |
Output is correct |
53 |
Correct |
131 ms |
47040 KB |
Output is correct |
54 |
Correct |
470 ms |
49492 KB |
Output is correct |
55 |
Correct |
524 ms |
49492 KB |
Output is correct |
56 |
Correct |
565 ms |
49220 KB |
Output is correct |
57 |
Correct |
336 ms |
49744 KB |
Output is correct |
58 |
Correct |
494 ms |
49744 KB |
Output is correct |
59 |
Correct |
493 ms |
49492 KB |
Output is correct |
60 |
Correct |
484 ms |
49488 KB |
Output is correct |
61 |
Correct |
543 ms |
49720 KB |
Output is correct |
62 |
Correct |
186 ms |
47708 KB |
Output is correct |
63 |
Correct |
439 ms |
49236 KB |
Output is correct |
64 |
Correct |
372 ms |
48952 KB |
Output is correct |
65 |
Correct |
489 ms |
49232 KB |
Output is correct |
66 |
Correct |
7 ms |
45776 KB |
Output is correct |
67 |
Correct |
522 ms |
49468 KB |
Output is correct |
68 |
Correct |
554 ms |
49360 KB |
Output is correct |
69 |
Correct |
581 ms |
49520 KB |
Output is correct |
70 |
Correct |
554 ms |
49496 KB |
Output is correct |
71 |
Correct |
570 ms |
49408 KB |
Output is correct |
72 |
Correct |
512 ms |
49348 KB |
Output is correct |
73 |
Correct |
344 ms |
49744 KB |
Output is correct |
74 |
Correct |
343 ms |
49744 KB |
Output is correct |
75 |
Correct |
486 ms |
49716 KB |
Output is correct |
76 |
Correct |
510 ms |
49788 KB |
Output is correct |
77 |
Correct |
303 ms |
49088 KB |
Output is correct |
78 |
Correct |
5 ms |
45656 KB |
Output is correct |
79 |
Correct |
10 ms |
45656 KB |
Output is correct |
80 |
Correct |
8 ms |
45660 KB |
Output is correct |
81 |
Correct |
10 ms |
45732 KB |
Output is correct |
82 |
Correct |
8 ms |
45660 KB |
Output is correct |
83 |
Correct |
11 ms |
45768 KB |
Output is correct |
84 |
Correct |
7 ms |
45660 KB |
Output is correct |
85 |
Correct |
8 ms |
45656 KB |
Output is correct |
86 |
Correct |
218 ms |
47200 KB |
Output is correct |
87 |
Correct |
229 ms |
47188 KB |
Output is correct |
88 |
Correct |
207 ms |
47440 KB |
Output is correct |
89 |
Correct |
169 ms |
47496 KB |
Output is correct |
90 |
Correct |
156 ms |
47440 KB |
Output is correct |
91 |
Correct |
156 ms |
47440 KB |
Output is correct |
92 |
Correct |
180 ms |
46928 KB |
Output is correct |
93 |
Correct |
250 ms |
47696 KB |
Output is correct |
94 |
Correct |
253 ms |
46676 KB |
Output is correct |
95 |
Correct |
235 ms |
47444 KB |
Output is correct |
96 |
Correct |
251 ms |
47572 KB |
Output is correct |
97 |
Correct |
192 ms |
47464 KB |
Output is correct |
98 |
Correct |
253 ms |
47464 KB |
Output is correct |
99 |
Correct |
126 ms |
46988 KB |
Output is correct |
100 |
Correct |
488 ms |
49488 KB |
Output is correct |
101 |
Correct |
483 ms |
49580 KB |
Output is correct |
102 |
Correct |
603 ms |
49740 KB |
Output is correct |
103 |
Correct |
472 ms |
49232 KB |
Output is correct |
104 |
Correct |
352 ms |
48964 KB |
Output is correct |
105 |
Correct |
454 ms |
49236 KB |
Output is correct |
106 |
Correct |
458 ms |
48724 KB |
Output is correct |
107 |
Correct |
541 ms |
48720 KB |
Output is correct |
108 |
Correct |
428 ms |
49488 KB |
Output is correct |
109 |
Correct |
374 ms |
49588 KB |
Output is correct |
110 |
Correct |
377 ms |
49488 KB |
Output is correct |
111 |
Correct |
370 ms |
49492 KB |
Output is correct |