#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++)
for (int j=0; j<LOG; j++)
blift[i][j]=-1;
for (int i=0; i<n; i++)
blift[i][0]=prev[i];
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];
/*for (int j=0; j<3; j++)
{
for (int i=0; i<n; i++)
cout << blift[i][j]+1 << ' ';
cout << '\n';
}*/
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;
//int i=lower_bound(compress.begin(), compress.end(), l[y])-compress.begin();
//cout << "x y " << x+1 << ' ' << y+1 << '\n';
//cout << "y " << y+1 << '\n';
for (int j=LOG-1; j>=0; j--)
if (blift[y][j]!=-1 && l[blift[y][j]]>r[x])
{
y=blift[y][j];
//cout << "y " << y+1 << '\n';
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 |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
98 ms |
12500 KB |
Output is correct |
3 |
Correct |
101 ms |
12404 KB |
Output is correct |
4 |
Correct |
113 ms |
12488 KB |
Output is correct |
5 |
Correct |
115 ms |
15776 KB |
Output is correct |
6 |
Correct |
106 ms |
15748 KB |
Output is correct |
7 |
Correct |
108 ms |
15824 KB |
Output is correct |
8 |
Correct |
96 ms |
16208 KB |
Output is correct |
9 |
Correct |
100 ms |
16204 KB |
Output is correct |
10 |
Correct |
108 ms |
15868 KB |
Output is correct |
11 |
Correct |
111 ms |
15688 KB |
Output is correct |
12 |
Correct |
55 ms |
15336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 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 |
2788 KB |
Output is correct |
6 |
Correct |
1 ms |
2784 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 |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 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 |
2788 KB |
Output is correct |
6 |
Correct |
1 ms |
2784 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 |
2652 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 |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2708 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 |
27 ms |
4520 KB |
Output is correct |
20 |
Correct |
21 ms |
4432 KB |
Output is correct |
21 |
Correct |
21 ms |
4872 KB |
Output is correct |
22 |
Correct |
16 ms |
4944 KB |
Output is correct |
23 |
Correct |
17 ms |
4700 KB |
Output is correct |
24 |
Correct |
17 ms |
4700 KB |
Output is correct |
25 |
Correct |
18 ms |
4460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 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 |
2788 KB |
Output is correct |
6 |
Correct |
1 ms |
2784 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 |
2652 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 |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2724 KB |
Output is correct |
15 |
Correct |
1 ms |
2780 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
83 ms |
13772 KB |
Output is correct |
20 |
Correct |
82 ms |
13008 KB |
Output is correct |
21 |
Correct |
105 ms |
13828 KB |
Output is correct |
22 |
Correct |
90 ms |
13632 KB |
Output is correct |
23 |
Correct |
103 ms |
13796 KB |
Output is correct |
24 |
Correct |
101 ms |
13736 KB |
Output is correct |
25 |
Correct |
40 ms |
12292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
12540 KB |
Output is correct |
2 |
Correct |
115 ms |
12412 KB |
Output is correct |
3 |
Correct |
118 ms |
12416 KB |
Output is correct |
4 |
Correct |
105 ms |
16072 KB |
Output is correct |
5 |
Correct |
122 ms |
15844 KB |
Output is correct |
6 |
Correct |
123 ms |
15860 KB |
Output is correct |
7 |
Correct |
141 ms |
15696 KB |
Output is correct |
8 |
Correct |
140 ms |
15800 KB |
Output is correct |
9 |
Correct |
103 ms |
13820 KB |
Output is correct |
10 |
Correct |
120 ms |
15348 KB |
Output is correct |
11 |
Correct |
118 ms |
15092 KB |
Output is correct |
12 |
Correct |
123 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
98 ms |
12500 KB |
Output is correct |
3 |
Correct |
101 ms |
12404 KB |
Output is correct |
4 |
Correct |
113 ms |
12488 KB |
Output is correct |
5 |
Correct |
115 ms |
15776 KB |
Output is correct |
6 |
Correct |
106 ms |
15748 KB |
Output is correct |
7 |
Correct |
108 ms |
15824 KB |
Output is correct |
8 |
Correct |
96 ms |
16208 KB |
Output is correct |
9 |
Correct |
100 ms |
16204 KB |
Output is correct |
10 |
Correct |
108 ms |
15868 KB |
Output is correct |
11 |
Correct |
111 ms |
15688 KB |
Output is correct |
12 |
Correct |
55 ms |
15336 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 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 |
2788 KB |
Output is correct |
18 |
Correct |
1 ms |
2784 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 |
2652 KB |
Output is correct |
23 |
Correct |
0 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 |
2708 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 |
27 ms |
4520 KB |
Output is correct |
32 |
Correct |
21 ms |
4432 KB |
Output is correct |
33 |
Correct |
21 ms |
4872 KB |
Output is correct |
34 |
Correct |
16 ms |
4944 KB |
Output is correct |
35 |
Correct |
17 ms |
4700 KB |
Output is correct |
36 |
Correct |
17 ms |
4700 KB |
Output is correct |
37 |
Correct |
18 ms |
4460 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 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 |
2652 KB |
Output is correct |
42 |
Correct |
1 ms |
2724 KB |
Output is correct |
43 |
Correct |
1 ms |
2780 KB |
Output is correct |
44 |
Correct |
1 ms |
2648 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
83 ms |
13772 KB |
Output is correct |
48 |
Correct |
82 ms |
13008 KB |
Output is correct |
49 |
Correct |
105 ms |
13828 KB |
Output is correct |
50 |
Correct |
90 ms |
13632 KB |
Output is correct |
51 |
Correct |
103 ms |
13796 KB |
Output is correct |
52 |
Correct |
101 ms |
13736 KB |
Output is correct |
53 |
Correct |
40 ms |
12292 KB |
Output is correct |
54 |
Correct |
104 ms |
12540 KB |
Output is correct |
55 |
Correct |
115 ms |
12412 KB |
Output is correct |
56 |
Correct |
118 ms |
12416 KB |
Output is correct |
57 |
Correct |
105 ms |
16072 KB |
Output is correct |
58 |
Correct |
122 ms |
15844 KB |
Output is correct |
59 |
Correct |
123 ms |
15860 KB |
Output is correct |
60 |
Correct |
141 ms |
15696 KB |
Output is correct |
61 |
Correct |
140 ms |
15800 KB |
Output is correct |
62 |
Correct |
103 ms |
13820 KB |
Output is correct |
63 |
Correct |
120 ms |
15348 KB |
Output is correct |
64 |
Correct |
118 ms |
15092 KB |
Output is correct |
65 |
Correct |
123 ms |
15440 KB |
Output is correct |
66 |
Correct |
0 ms |
2508 KB |
Output is correct |
67 |
Correct |
110 ms |
15716 KB |
Output is correct |
68 |
Correct |
110 ms |
15732 KB |
Output is correct |
69 |
Correct |
118 ms |
15600 KB |
Output is correct |
70 |
Correct |
112 ms |
15768 KB |
Output is correct |
71 |
Correct |
140 ms |
15668 KB |
Output is correct |
72 |
Correct |
115 ms |
15752 KB |
Output is correct |
73 |
Correct |
103 ms |
15992 KB |
Output is correct |
74 |
Correct |
96 ms |
15996 KB |
Output is correct |
75 |
Correct |
126 ms |
16064 KB |
Output is correct |
76 |
Correct |
130 ms |
15808 KB |
Output is correct |
77 |
Correct |
72 ms |
15472 KB |
Output is correct |
78 |
Correct |
0 ms |
2652 KB |
Output is correct |
79 |
Correct |
1 ms |
2780 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 |
2648 KB |
Output is correct |
83 |
Correct |
1 ms |
2780 KB |
Output is correct |
84 |
Correct |
1 ms |
2780 KB |
Output is correct |
85 |
Correct |
1 ms |
2652 KB |
Output is correct |
86 |
Correct |
34 ms |
4388 KB |
Output is correct |
87 |
Correct |
25 ms |
4568 KB |
Output is correct |
88 |
Correct |
26 ms |
4848 KB |
Output is correct |
89 |
Correct |
17 ms |
4916 KB |
Output is correct |
90 |
Correct |
19 ms |
4700 KB |
Output is correct |
91 |
Correct |
18 ms |
4768 KB |
Output is correct |
92 |
Correct |
18 ms |
4504 KB |
Output is correct |
93 |
Correct |
97 ms |
13860 KB |
Output is correct |
94 |
Correct |
92 ms |
13184 KB |
Output is correct |
95 |
Correct |
88 ms |
13624 KB |
Output is correct |
96 |
Correct |
94 ms |
13680 KB |
Output is correct |
97 |
Correct |
105 ms |
13788 KB |
Output is correct |
98 |
Correct |
95 ms |
13848 KB |
Output is correct |
99 |
Correct |
39 ms |
12492 KB |
Output is correct |
100 |
Correct |
133 ms |
15800 KB |
Output is correct |
101 |
Correct |
117 ms |
15816 KB |
Output is correct |
102 |
Correct |
116 ms |
15856 KB |
Output is correct |
103 |
Correct |
117 ms |
15348 KB |
Output is correct |
104 |
Correct |
116 ms |
15180 KB |
Output is correct |
105 |
Correct |
118 ms |
15424 KB |
Output is correct |
106 |
Correct |
99 ms |
15052 KB |
Output is correct |
107 |
Correct |
109 ms |
15052 KB |
Output is correct |
108 |
Correct |
102 ms |
15820 KB |
Output is correct |
109 |
Correct |
107 ms |
15816 KB |
Output is correct |
110 |
Correct |
118 ms |
15692 KB |
Output is correct |
111 |
Correct |
120 ms |
15696 KB |
Output is correct |