#include <bits/stdc++.h>
using namespace std;
const int MAXN=(1<<18), LOG=17;
int l[MAXN], r[MAXN];
int tree[2*MAXN-1];
int compare(int x, int y)
{
if (x==-1 || y==-1)
return max(x, y);
if (l[x]<l[y])
return x;
return y;
}
int find(int x, int l, int r, int lt, int rt)
{
if (l>=rt || r<=lt)
return -1;
if (l>=lt && r<=rt)
return tree[x];
return compare(find(2*x+1, l, (l+r)/2, lt, rt), find(2*x+2, (l+r)/2, r, lt, rt));
}
void update(int x, int l, int r, int index, int value)
{
if (l>index || r<=index)
return;
if (r-l==1)
{
tree[x]=compare(tree[x], value);
return;
}
update(2*x+1, l, (l+r)/2, index, value);
update(2*x+2, (l+r)/2, r, index, value);
tree[x]=compare(tree[2*x+1], tree[2*x+2]);
return;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i=0; i<n; i++)
cin >> l[i] >> r[i];
vector<int> temp;
for (int i=0; i<n; i++)
{
temp.push_back(l[i]);
temp.push_back(r[i]);
}
sort(temp.begin(), temp.end());
vector<int> compress;
for (auto x : temp)
if (compress.empty() || x!=compress.back())
compress.push_back(x);
for (int i=0; i<2*MAXN-1; i++)
tree[i]=-1;
for (int i=0; i<n; i++)
{
int rightindex=lower_bound(compress.begin(), compress.end(), r[i])-compress.begin();
update(0, 0, MAXN, rightindex, i);
}
int prev[n];
for (int i=0; i<n; i++)
prev[i]=-1;
for (int i=0; i<n; i++)
{
int leftindex=lower_bound(compress.begin(), compress.end(), l[i])-compress.begin();
int rightindex=lower_bound(compress.begin(), compress.end(), r[i])-compress.begin();
prev[i]=find(0, 0, MAXN, leftindex, rightindex+1);
if (prev[i]==i)
prev[i]=-1;
}
int blift[n][LOG];
for (int i=0; i<n; i++)
blift[i][0]=prev[i];
for (int i=0; i<n; i++)
for (int j=1; j<LOG; j++)
blift[i][j]=-1;
for (int j=1; j<LOG; j++)
for (int i=0; i<n; i++)
if (blift[i][j-1]!=-1)
blift[i][j]=blift[blift[i][j-1]][j-1];
while (q--)
{
int x, y;
cin >> x >> y;
x=x-1;
y=y-1;
if (x==y)
{
cout << 0 << '\n';
continue;
}
if (r[x]>r[y])
{
cout << "impossible" << '\n';
continue;
}
int total=0;
for (int j=LOG-1; j>=0; j--)
if (blift[y][j]!=-1 && l[blift[y][j]]>r[x])
{
y=blift[y][j];
total=total+(1<<j);
}
if (l[y]<=r[x])
cout << total+1 << '\n';
else if (blift[y][0]!=-1 && l[blift[y][0]]<=r[x])
cout << total+2 << '\n';
else
cout << "impossible" << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
99 ms |
12548 KB |
Output is correct |
3 |
Correct |
103 ms |
12604 KB |
Output is correct |
4 |
Correct |
111 ms |
12492 KB |
Output is correct |
5 |
Correct |
107 ms |
12496 KB |
Output is correct |
6 |
Correct |
105 ms |
12744 KB |
Output is correct |
7 |
Correct |
113 ms |
12752 KB |
Output is correct |
8 |
Correct |
98 ms |
12928 KB |
Output is correct |
9 |
Correct |
95 ms |
13136 KB |
Output is correct |
10 |
Correct |
115 ms |
13008 KB |
Output is correct |
11 |
Correct |
109 ms |
12636 KB |
Output is correct |
12 |
Correct |
56 ms |
13008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2664 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
19 ms |
3420 KB |
Output is correct |
20 |
Correct |
21 ms |
3616 KB |
Output is correct |
21 |
Correct |
22 ms |
3932 KB |
Output is correct |
22 |
Correct |
16 ms |
3932 KB |
Output is correct |
23 |
Correct |
17 ms |
3724 KB |
Output is correct |
24 |
Correct |
16 ms |
3672 KB |
Output is correct |
25 |
Correct |
17 ms |
3392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
85 ms |
11984 KB |
Output is correct |
20 |
Correct |
91 ms |
11972 KB |
Output is correct |
21 |
Correct |
97 ms |
11900 KB |
Output is correct |
22 |
Correct |
95 ms |
11676 KB |
Output is correct |
23 |
Correct |
118 ms |
11792 KB |
Output is correct |
24 |
Correct |
99 ms |
11848 KB |
Output is correct |
25 |
Correct |
34 ms |
11216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
12488 KB |
Output is correct |
2 |
Correct |
135 ms |
12492 KB |
Output is correct |
3 |
Correct |
113 ms |
12492 KB |
Output is correct |
4 |
Correct |
97 ms |
12948 KB |
Output is correct |
5 |
Correct |
120 ms |
12996 KB |
Output is correct |
6 |
Correct |
122 ms |
12688 KB |
Output is correct |
7 |
Correct |
137 ms |
12536 KB |
Output is correct |
8 |
Correct |
143 ms |
12672 KB |
Output is correct |
9 |
Correct |
101 ms |
11908 KB |
Output is correct |
10 |
Correct |
119 ms |
12432 KB |
Output is correct |
11 |
Correct |
115 ms |
12040 KB |
Output is correct |
12 |
Correct |
115 ms |
12368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
99 ms |
12548 KB |
Output is correct |
3 |
Correct |
103 ms |
12604 KB |
Output is correct |
4 |
Correct |
111 ms |
12492 KB |
Output is correct |
5 |
Correct |
107 ms |
12496 KB |
Output is correct |
6 |
Correct |
105 ms |
12744 KB |
Output is correct |
7 |
Correct |
113 ms |
12752 KB |
Output is correct |
8 |
Correct |
98 ms |
12928 KB |
Output is correct |
9 |
Correct |
95 ms |
13136 KB |
Output is correct |
10 |
Correct |
115 ms |
13008 KB |
Output is correct |
11 |
Correct |
109 ms |
12636 KB |
Output is correct |
12 |
Correct |
56 ms |
13008 KB |
Output is correct |
13 |
Correct |
0 ms |
2652 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2664 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
19 ms |
3420 KB |
Output is correct |
32 |
Correct |
21 ms |
3616 KB |
Output is correct |
33 |
Correct |
22 ms |
3932 KB |
Output is correct |
34 |
Correct |
16 ms |
3932 KB |
Output is correct |
35 |
Correct |
17 ms |
3724 KB |
Output is correct |
36 |
Correct |
16 ms |
3672 KB |
Output is correct |
37 |
Correct |
17 ms |
3392 KB |
Output is correct |
38 |
Correct |
1 ms |
2648 KB |
Output is correct |
39 |
Correct |
0 ms |
2652 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
2648 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
1 ms |
2652 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
85 ms |
11984 KB |
Output is correct |
48 |
Correct |
91 ms |
11972 KB |
Output is correct |
49 |
Correct |
97 ms |
11900 KB |
Output is correct |
50 |
Correct |
95 ms |
11676 KB |
Output is correct |
51 |
Correct |
118 ms |
11792 KB |
Output is correct |
52 |
Correct |
99 ms |
11848 KB |
Output is correct |
53 |
Correct |
34 ms |
11216 KB |
Output is correct |
54 |
Correct |
113 ms |
12488 KB |
Output is correct |
55 |
Correct |
135 ms |
12492 KB |
Output is correct |
56 |
Correct |
113 ms |
12492 KB |
Output is correct |
57 |
Correct |
97 ms |
12948 KB |
Output is correct |
58 |
Correct |
120 ms |
12996 KB |
Output is correct |
59 |
Correct |
122 ms |
12688 KB |
Output is correct |
60 |
Correct |
137 ms |
12536 KB |
Output is correct |
61 |
Correct |
143 ms |
12672 KB |
Output is correct |
62 |
Correct |
101 ms |
11908 KB |
Output is correct |
63 |
Correct |
119 ms |
12432 KB |
Output is correct |
64 |
Correct |
115 ms |
12040 KB |
Output is correct |
65 |
Correct |
115 ms |
12368 KB |
Output is correct |
66 |
Correct |
0 ms |
2652 KB |
Output is correct |
67 |
Correct |
102 ms |
12540 KB |
Output is correct |
68 |
Correct |
106 ms |
12548 KB |
Output is correct |
69 |
Correct |
119 ms |
12416 KB |
Output is correct |
70 |
Correct |
114 ms |
12492 KB |
Output is correct |
71 |
Correct |
108 ms |
12732 KB |
Output is correct |
72 |
Correct |
105 ms |
12748 KB |
Output is correct |
73 |
Correct |
99 ms |
13140 KB |
Output is correct |
74 |
Correct |
96 ms |
12948 KB |
Output is correct |
75 |
Correct |
109 ms |
12748 KB |
Output is correct |
76 |
Correct |
106 ms |
12692 KB |
Output is correct |
77 |
Correct |
60 ms |
13060 KB |
Output is correct |
78 |
Correct |
1 ms |
2652 KB |
Output is correct |
79 |
Correct |
1 ms |
2652 KB |
Output is correct |
80 |
Correct |
1 ms |
2652 KB |
Output is correct |
81 |
Correct |
1 ms |
2652 KB |
Output is correct |
82 |
Correct |
1 ms |
2652 KB |
Output is correct |
83 |
Correct |
1 ms |
2652 KB |
Output is correct |
84 |
Correct |
1 ms |
2652 KB |
Output is correct |
85 |
Correct |
1 ms |
2652 KB |
Output is correct |
86 |
Correct |
21 ms |
3560 KB |
Output is correct |
87 |
Correct |
19 ms |
3676 KB |
Output is correct |
88 |
Correct |
22 ms |
3756 KB |
Output is correct |
89 |
Correct |
16 ms |
3864 KB |
Output is correct |
90 |
Correct |
17 ms |
3672 KB |
Output is correct |
91 |
Correct |
16 ms |
3676 KB |
Output is correct |
92 |
Correct |
17 ms |
3420 KB |
Output is correct |
93 |
Correct |
83 ms |
11960 KB |
Output is correct |
94 |
Correct |
82 ms |
11984 KB |
Output is correct |
95 |
Correct |
90 ms |
11980 KB |
Output is correct |
96 |
Correct |
92 ms |
11596 KB |
Output is correct |
97 |
Correct |
99 ms |
11860 KB |
Output is correct |
98 |
Correct |
96 ms |
11856 KB |
Output is correct |
99 |
Correct |
35 ms |
11216 KB |
Output is correct |
100 |
Correct |
115 ms |
12620 KB |
Output is correct |
101 |
Correct |
124 ms |
12676 KB |
Output is correct |
102 |
Correct |
116 ms |
12880 KB |
Output is correct |
103 |
Correct |
114 ms |
12364 KB |
Output is correct |
104 |
Correct |
116 ms |
12040 KB |
Output is correct |
105 |
Correct |
121 ms |
12420 KB |
Output is correct |
106 |
Correct |
101 ms |
12564 KB |
Output is correct |
107 |
Correct |
110 ms |
12744 KB |
Output is correct |
108 |
Correct |
110 ms |
12752 KB |
Output is correct |
109 |
Correct |
118 ms |
12792 KB |
Output is correct |
110 |
Correct |
124 ms |
12444 KB |
Output is correct |
111 |
Correct |
122 ms |
12628 KB |
Output is correct |