#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
#define sz(x) int(x.size())
const int mx = 200'000;
const int lg = 18;
int mxv[1+mx][1+lg];
int bst[1+mx];
int jmp[1+mx][1+lg];
int rangemax(int l, int r)
{
int z = bst[r-l+1];
return max(mxv[l][z], mxv[r - (1 << z) + 1][z]);
}
int Z = 262144;
int stree[1+lg][(1<<19)];
int smax(int e, int l, int r)
{
int ans = 0;
l += Z;
r += Z+1;
while(l < r)
{
if(l & 1)
{
ans = max(ans, stree[e][l]);
l++;
}
if(r & 1)
{
r--;
ans = max(ans, stree[e][r]);
}
r >>= 1;
l >>= 1;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q;
cin >> N >> Q;
map<int, int> norm;
vi S(1+N), E(1+N);
for(int i = 1; i <= N; i++)
{
cin >> S[i] >> E[i];
S[i] *= -1;
E[i] *= -1;
swap(S[i], E[i]);
norm[S[i]] = 0;
norm[E[i]] = 0;
}
int ct = 0;
for(auto& z : norm)
z.second = ++ct;
for(int i = 1; i <= N; i++)
{
S[i] = norm[S[i]];
E[i] = norm[E[i]];
}
// cerr << "\n\n";
// for(int i = 1; i <= N; i++)
// cerr << S[i] << ' ' << E[i] << '\n';
// cerr << "\n\n";
for(int i = 0; i <= mx; i++)
for(int j = 0; j <= lg; j++)
mxv[i][j] = 0;
for(int i = 1; i <= N; i++)
mxv[S[i]][0] = max(mxv[S[i]][0], E[i]);
for(int e = 1; e <= lg; e++)
{
for(int i = 0; i <= mx; i++)
{
mxv[i][e] = mxv[i][e-1];
if(i + (1 << (e-1)) <= mx)
mxv[i][e] = max(mxv[i][e], mxv[ i + (1 << (e-1)) ][e-1]);
}
}
bst[1] = 0;
for(int i = 1; i <= mx; i++)
{
bst[i] = bst[i-1];
if((1 << (bst[i]+1)) == i)
bst[i]++;
}
for(int i = 0; i <= mx; i++)
{
jmp[i][0] = max(i, mxv[i][0]);
}
for(int e = 0; e <= lg; e++)
{
if(e > 0)
{
for(int i = 0; i <= mx; i++)
{
// cerr << i << " : " << jmp[i][e-1];
if(i > jmp[i][e-1])
{
// cerr << e-1 << '\n';
// cerr << i << " : " << jmp[i][e-1] << '\n';
while(1);
}
jmp[i][e] = 0;
// for(int h = i; h <= jmp[i][e-1]; h++)
// jmp[i][e] = max(jmp[i][e], jmp[h][e-1]);
// jmp[i][e] = rangemax(i, jmp[i][e-1]);
jmp[i][e] = smax(e-1, i, jmp[i][e-1]);
}
}
for(int i = 0; i <= mx; i++)
stree[e][Z+i] = jmp[i][e];
for(int i = Z-1; i >= 0; i--)
stree[e][i] = max(stree[e][2*i], stree[e][2*i + 1]);
}
// cerr << "check\n";
for(int q = 1; q <= Q; q++)
{
int a, b;
cin >> a >> b;
swap(a, b);
int lt = S[a], rt = E[a];
if(a == b)
{
cout << "0\n";
continue;
}
else if(S[b] < S[a])
{
cout << "impossible\n";
continue;
}
// int tot = 0;
// for(int i = lt; i <= rt; i++)
// tot = max(tot, jmp[i][lg]);
int tot = smax(lg, lt, rt);
if(tot < S[b])
{
cout << "impossible\n";
continue;
}
int ans = 0;
for(int e = lg; e >= 0; e--)
{
// int nrt = 0;
// for(int i = lt; i <= rt; i++)
// nrt = max(nrt, jmp[i][e]);
int nrt = smax(e, lt, rt);
// cerr << "e = " << e << " : ";
// cerr << lt << ' ' << rt << " -> " << lt << ' ' << nrt << " cmp " << S[b] << '\n';
if(nrt < S[b])
{
rt = nrt;
ans += (1<<e);
}
else if(nrt == S[b])
{
rt = nrt;
ans += (1<<e);
break;
}
}
if(rt < S[b])
{
rt = S[b];
ans++;
}
// cerr << "current lt rt = " << lt << ' ' << rt << '\n';
ans++;
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
65236 KB |
Output is correct |
2 |
Correct |
463 ms |
71536 KB |
Output is correct |
3 |
Correct |
411 ms |
72256 KB |
Output is correct |
4 |
Correct |
428 ms |
72808 KB |
Output is correct |
5 |
Correct |
467 ms |
73420 KB |
Output is correct |
6 |
Correct |
470 ms |
74728 KB |
Output is correct |
7 |
Correct |
451 ms |
74068 KB |
Output is correct |
8 |
Correct |
313 ms |
77920 KB |
Output is correct |
9 |
Correct |
299 ms |
78640 KB |
Output is correct |
10 |
Correct |
388 ms |
73868 KB |
Output is correct |
11 |
Correct |
395 ms |
75132 KB |
Output is correct |
12 |
Correct |
196 ms |
72384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
65196 KB |
Output is correct |
2 |
Correct |
106 ms |
65144 KB |
Output is correct |
3 |
Correct |
110 ms |
65316 KB |
Output is correct |
4 |
Correct |
106 ms |
65292 KB |
Output is correct |
5 |
Correct |
102 ms |
65268 KB |
Output is correct |
6 |
Correct |
109 ms |
65208 KB |
Output is correct |
7 |
Correct |
114 ms |
65264 KB |
Output is correct |
8 |
Correct |
110 ms |
65332 KB |
Output is correct |
9 |
Correct |
106 ms |
65188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
65196 KB |
Output is correct |
2 |
Correct |
106 ms |
65144 KB |
Output is correct |
3 |
Correct |
110 ms |
65316 KB |
Output is correct |
4 |
Correct |
106 ms |
65292 KB |
Output is correct |
5 |
Correct |
102 ms |
65268 KB |
Output is correct |
6 |
Correct |
109 ms |
65208 KB |
Output is correct |
7 |
Correct |
114 ms |
65264 KB |
Output is correct |
8 |
Correct |
110 ms |
65332 KB |
Output is correct |
9 |
Correct |
106 ms |
65188 KB |
Output is correct |
10 |
Correct |
116 ms |
65220 KB |
Output is correct |
11 |
Correct |
113 ms |
65184 KB |
Output is correct |
12 |
Correct |
111 ms |
65296 KB |
Output is correct |
13 |
Correct |
112 ms |
65212 KB |
Output is correct |
14 |
Correct |
110 ms |
65316 KB |
Output is correct |
15 |
Correct |
107 ms |
65296 KB |
Output is correct |
16 |
Correct |
106 ms |
65344 KB |
Output is correct |
17 |
Correct |
104 ms |
65348 KB |
Output is correct |
18 |
Correct |
103 ms |
65252 KB |
Output is correct |
19 |
Correct |
274 ms |
66384 KB |
Output is correct |
20 |
Correct |
205 ms |
66768 KB |
Output is correct |
21 |
Correct |
175 ms |
66928 KB |
Output is correct |
22 |
Correct |
187 ms |
67148 KB |
Output is correct |
23 |
Correct |
220 ms |
66888 KB |
Output is correct |
24 |
Correct |
228 ms |
67032 KB |
Output is correct |
25 |
Correct |
258 ms |
66588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
65196 KB |
Output is correct |
2 |
Correct |
106 ms |
65144 KB |
Output is correct |
3 |
Correct |
110 ms |
65316 KB |
Output is correct |
4 |
Correct |
106 ms |
65292 KB |
Output is correct |
5 |
Correct |
102 ms |
65268 KB |
Output is correct |
6 |
Correct |
109 ms |
65208 KB |
Output is correct |
7 |
Correct |
114 ms |
65264 KB |
Output is correct |
8 |
Correct |
110 ms |
65332 KB |
Output is correct |
9 |
Correct |
106 ms |
65188 KB |
Output is correct |
10 |
Correct |
105 ms |
65184 KB |
Output is correct |
11 |
Correct |
119 ms |
65196 KB |
Output is correct |
12 |
Correct |
115 ms |
65296 KB |
Output is correct |
13 |
Correct |
114 ms |
65260 KB |
Output is correct |
14 |
Correct |
118 ms |
65316 KB |
Output is correct |
15 |
Correct |
117 ms |
65244 KB |
Output is correct |
16 |
Correct |
120 ms |
65260 KB |
Output is correct |
17 |
Correct |
109 ms |
65364 KB |
Output is correct |
18 |
Correct |
113 ms |
65256 KB |
Output is correct |
19 |
Correct |
273 ms |
72320 KB |
Output is correct |
20 |
Correct |
260 ms |
71064 KB |
Output is correct |
21 |
Correct |
251 ms |
71104 KB |
Output is correct |
22 |
Correct |
332 ms |
72688 KB |
Output is correct |
23 |
Correct |
417 ms |
75884 KB |
Output is correct |
24 |
Correct |
370 ms |
76276 KB |
Output is correct |
25 |
Correct |
132 ms |
66096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
454 ms |
71884 KB |
Output is correct |
2 |
Correct |
425 ms |
73512 KB |
Output is correct |
3 |
Correct |
442 ms |
72316 KB |
Output is correct |
4 |
Correct |
283 ms |
76592 KB |
Output is correct |
5 |
Correct |
354 ms |
71844 KB |
Output is correct |
6 |
Correct |
593 ms |
76444 KB |
Output is correct |
7 |
Correct |
546 ms |
76440 KB |
Output is correct |
8 |
Correct |
504 ms |
78796 KB |
Output is correct |
9 |
Correct |
383 ms |
75844 KB |
Output is correct |
10 |
Correct |
575 ms |
77480 KB |
Output is correct |
11 |
Correct |
604 ms |
77120 KB |
Output is correct |
12 |
Correct |
564 ms |
77296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
65236 KB |
Output is correct |
2 |
Correct |
463 ms |
71536 KB |
Output is correct |
3 |
Correct |
411 ms |
72256 KB |
Output is correct |
4 |
Correct |
428 ms |
72808 KB |
Output is correct |
5 |
Correct |
467 ms |
73420 KB |
Output is correct |
6 |
Correct |
470 ms |
74728 KB |
Output is correct |
7 |
Correct |
451 ms |
74068 KB |
Output is correct |
8 |
Correct |
313 ms |
77920 KB |
Output is correct |
9 |
Correct |
299 ms |
78640 KB |
Output is correct |
10 |
Correct |
388 ms |
73868 KB |
Output is correct |
11 |
Correct |
395 ms |
75132 KB |
Output is correct |
12 |
Correct |
196 ms |
72384 KB |
Output is correct |
13 |
Correct |
105 ms |
65196 KB |
Output is correct |
14 |
Correct |
106 ms |
65144 KB |
Output is correct |
15 |
Correct |
110 ms |
65316 KB |
Output is correct |
16 |
Correct |
106 ms |
65292 KB |
Output is correct |
17 |
Correct |
102 ms |
65268 KB |
Output is correct |
18 |
Correct |
109 ms |
65208 KB |
Output is correct |
19 |
Correct |
114 ms |
65264 KB |
Output is correct |
20 |
Correct |
110 ms |
65332 KB |
Output is correct |
21 |
Correct |
106 ms |
65188 KB |
Output is correct |
22 |
Correct |
116 ms |
65220 KB |
Output is correct |
23 |
Correct |
113 ms |
65184 KB |
Output is correct |
24 |
Correct |
111 ms |
65296 KB |
Output is correct |
25 |
Correct |
112 ms |
65212 KB |
Output is correct |
26 |
Correct |
110 ms |
65316 KB |
Output is correct |
27 |
Correct |
107 ms |
65296 KB |
Output is correct |
28 |
Correct |
106 ms |
65344 KB |
Output is correct |
29 |
Correct |
104 ms |
65348 KB |
Output is correct |
30 |
Correct |
103 ms |
65252 KB |
Output is correct |
31 |
Correct |
274 ms |
66384 KB |
Output is correct |
32 |
Correct |
205 ms |
66768 KB |
Output is correct |
33 |
Correct |
175 ms |
66928 KB |
Output is correct |
34 |
Correct |
187 ms |
67148 KB |
Output is correct |
35 |
Correct |
220 ms |
66888 KB |
Output is correct |
36 |
Correct |
228 ms |
67032 KB |
Output is correct |
37 |
Correct |
258 ms |
66588 KB |
Output is correct |
38 |
Correct |
105 ms |
65184 KB |
Output is correct |
39 |
Correct |
119 ms |
65196 KB |
Output is correct |
40 |
Correct |
115 ms |
65296 KB |
Output is correct |
41 |
Correct |
114 ms |
65260 KB |
Output is correct |
42 |
Correct |
118 ms |
65316 KB |
Output is correct |
43 |
Correct |
117 ms |
65244 KB |
Output is correct |
44 |
Correct |
120 ms |
65260 KB |
Output is correct |
45 |
Correct |
109 ms |
65364 KB |
Output is correct |
46 |
Correct |
113 ms |
65256 KB |
Output is correct |
47 |
Correct |
273 ms |
72320 KB |
Output is correct |
48 |
Correct |
260 ms |
71064 KB |
Output is correct |
49 |
Correct |
251 ms |
71104 KB |
Output is correct |
50 |
Correct |
332 ms |
72688 KB |
Output is correct |
51 |
Correct |
417 ms |
75884 KB |
Output is correct |
52 |
Correct |
370 ms |
76276 KB |
Output is correct |
53 |
Correct |
132 ms |
66096 KB |
Output is correct |
54 |
Correct |
454 ms |
71884 KB |
Output is correct |
55 |
Correct |
425 ms |
73512 KB |
Output is correct |
56 |
Correct |
442 ms |
72316 KB |
Output is correct |
57 |
Correct |
283 ms |
76592 KB |
Output is correct |
58 |
Correct |
354 ms |
71844 KB |
Output is correct |
59 |
Correct |
593 ms |
76444 KB |
Output is correct |
60 |
Correct |
546 ms |
76440 KB |
Output is correct |
61 |
Correct |
504 ms |
78796 KB |
Output is correct |
62 |
Correct |
383 ms |
75844 KB |
Output is correct |
63 |
Correct |
575 ms |
77480 KB |
Output is correct |
64 |
Correct |
604 ms |
77120 KB |
Output is correct |
65 |
Correct |
564 ms |
77296 KB |
Output is correct |
66 |
Correct |
138 ms |
65248 KB |
Output is correct |
67 |
Correct |
481 ms |
71708 KB |
Output is correct |
68 |
Correct |
491 ms |
72444 KB |
Output is correct |
69 |
Correct |
482 ms |
71548 KB |
Output is correct |
70 |
Correct |
517 ms |
72424 KB |
Output is correct |
71 |
Correct |
553 ms |
72676 KB |
Output is correct |
72 |
Correct |
499 ms |
73364 KB |
Output is correct |
73 |
Correct |
370 ms |
76828 KB |
Output is correct |
74 |
Correct |
339 ms |
79096 KB |
Output is correct |
75 |
Correct |
442 ms |
72208 KB |
Output is correct |
76 |
Correct |
452 ms |
73728 KB |
Output is correct |
77 |
Correct |
211 ms |
71968 KB |
Output is correct |
78 |
Correct |
116 ms |
65148 KB |
Output is correct |
79 |
Correct |
135 ms |
65220 KB |
Output is correct |
80 |
Correct |
132 ms |
65312 KB |
Output is correct |
81 |
Correct |
118 ms |
65292 KB |
Output is correct |
82 |
Correct |
117 ms |
65296 KB |
Output is correct |
83 |
Correct |
124 ms |
65284 KB |
Output is correct |
84 |
Correct |
132 ms |
65460 KB |
Output is correct |
85 |
Correct |
126 ms |
65188 KB |
Output is correct |
86 |
Correct |
302 ms |
66680 KB |
Output is correct |
87 |
Correct |
248 ms |
66876 KB |
Output is correct |
88 |
Correct |
207 ms |
66644 KB |
Output is correct |
89 |
Correct |
199 ms |
67060 KB |
Output is correct |
90 |
Correct |
288 ms |
67368 KB |
Output is correct |
91 |
Correct |
226 ms |
67148 KB |
Output is correct |
92 |
Correct |
284 ms |
66508 KB |
Output is correct |
93 |
Correct |
308 ms |
72164 KB |
Output is correct |
94 |
Correct |
296 ms |
71600 KB |
Output is correct |
95 |
Correct |
319 ms |
71872 KB |
Output is correct |
96 |
Correct |
340 ms |
72624 KB |
Output is correct |
97 |
Correct |
462 ms |
75888 KB |
Output is correct |
98 |
Correct |
412 ms |
75536 KB |
Output is correct |
99 |
Correct |
150 ms |
66188 KB |
Output is correct |
100 |
Correct |
640 ms |
76440 KB |
Output is correct |
101 |
Correct |
612 ms |
76780 KB |
Output is correct |
102 |
Correct |
614 ms |
77748 KB |
Output is correct |
103 |
Correct |
659 ms |
77736 KB |
Output is correct |
104 |
Correct |
746 ms |
76900 KB |
Output is correct |
105 |
Correct |
638 ms |
78184 KB |
Output is correct |
106 |
Correct |
501 ms |
72292 KB |
Output is correct |
107 |
Correct |
490 ms |
73720 KB |
Output is correct |
108 |
Correct |
400 ms |
73548 KB |
Output is correct |
109 |
Correct |
400 ms |
72812 KB |
Output is correct |
110 |
Correct |
739 ms |
76776 KB |
Output is correct |
111 |
Correct |
727 ms |
76404 KB |
Output is correct |