#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 2e5+10;
const int lgmx = 20;
const int inf = 1e9;
#define X first
#define Y second
pii segs[maxn];
int alxs[maxn];
pii mn[maxn][lgmx];
map<int, int> mp;
int lft[maxn][lgmx];
int n;
inline void compress()
{
for(int i = 0; i < n; i++)
{
alxs[2*i] = segs[i].X;
alxs[2*i+1] = segs[i].Y;
}
sort(alxs, alxs+2*n);
int c = 0;
mp[alxs[0]] = c++;
for(int i = 1; i < 2*n; i++)
{
if(alxs[i] != alxs[i-1])
mp[alxs[i]] = c++;
}
for(int i = 0; i < n; i++)
{
segs[i].X = mp[segs[i].X];
segs[i].Y = mp[segs[i].Y];
//cerr << segs[i].X << " " << segs[i].Y << endl;
}
}
inline pii get(int l, int r)
{
int x = __lg(r-l);
return min( mn[l][x], mn[r-(1<<x)][x] );
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int q; cin >> n >> q;
for(int i = 0; i < n; i++)
cin >> segs[i].first >> segs[i].second;
compress();
for(int j = 0; j < maxn; j++)
for(int i = 0; i < lgmx; i++)
mn[j][i] = pii(inf, inf);
for(int i = 0; i < n; i++)
mn[segs[i].Y][0] = min(mn[segs[i].Y][0], pii(segs[i].X, i));
for(int k = 1; k < lgmx; k++)
{
int x = (1<<(k-1));
for(int i = 0; i < 2*n; i++)
{
mn[i][k] = min( mn[i][k-1], mn[ min(2*n-1, i+x) ][k-1] );
}
}
//for(int i = 0; i < 12; i++) cerr << i << ": " << mn[i][1].X << "," << mn[i][1].Y << endl; cerr << endl;
for(int i = 0; i < n; i++)
{
pii p = get(segs[i].X, segs[i].Y+1);
if(p.X >= segs[i].X)
lft[i][0] = i;
else
lft[i][0] = p.Y;
//cerr << lft[i][0] << endl;
}
for(int k = 1; k < lgmx; k++)
{
for(int i = 0; i < n; i++)
lft[i][k] = lft[lft[i][k-1]][k-1];
}
while(q--)
{
int s, e; cin >> s >> e;
s--; e--;
if(segs[s].Y > segs[e].Y or segs[lft[e][lgmx-1]].X > segs[s].Y)
{
cout << "impossible" << endl;
continue;
}
if(s == e)
{
cout << 0 << endl;
continue;
}
if(segs[e].X <= segs[s].Y and segs[s].Y <= segs[e].Y)
{
cout << 1 << endl;
continue;
}
int x = e;
int t = 2;
//cerr << lft[e][0] << endl;
for(int k = lgmx-1; k >= 0; k--)
{
if(segs[lft[x][k]].X > segs[s].Y)
{
t += (1<<k);
x = lft[x][k];
}
}
cout << t << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
31640 KB |
Output is correct |
2 |
Correct |
199 ms |
46272 KB |
Output is correct |
3 |
Correct |
215 ms |
46200 KB |
Output is correct |
4 |
Correct |
222 ms |
46208 KB |
Output is correct |
5 |
Correct |
198 ms |
46832 KB |
Output is correct |
6 |
Correct |
199 ms |
47280 KB |
Output is correct |
7 |
Correct |
223 ms |
47384 KB |
Output is correct |
8 |
Correct |
207 ms |
51588 KB |
Output is correct |
9 |
Correct |
212 ms |
51864 KB |
Output is correct |
10 |
Correct |
199 ms |
47052 KB |
Output is correct |
11 |
Correct |
220 ms |
48884 KB |
Output is correct |
12 |
Correct |
140 ms |
47196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31728 KB |
Output is correct |
4 |
Correct |
16 ms |
31772 KB |
Output is correct |
5 |
Correct |
16 ms |
31716 KB |
Output is correct |
6 |
Correct |
15 ms |
31700 KB |
Output is correct |
7 |
Correct |
16 ms |
31760 KB |
Output is correct |
8 |
Correct |
16 ms |
31828 KB |
Output is correct |
9 |
Correct |
17 ms |
31708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31728 KB |
Output is correct |
4 |
Correct |
16 ms |
31772 KB |
Output is correct |
5 |
Correct |
16 ms |
31716 KB |
Output is correct |
6 |
Correct |
15 ms |
31700 KB |
Output is correct |
7 |
Correct |
16 ms |
31760 KB |
Output is correct |
8 |
Correct |
16 ms |
31828 KB |
Output is correct |
9 |
Correct |
17 ms |
31708 KB |
Output is correct |
10 |
Correct |
15 ms |
31544 KB |
Output is correct |
11 |
Correct |
15 ms |
31572 KB |
Output is correct |
12 |
Correct |
16 ms |
31700 KB |
Output is correct |
13 |
Correct |
17 ms |
31772 KB |
Output is correct |
14 |
Correct |
17 ms |
31780 KB |
Output is correct |
15 |
Correct |
17 ms |
31844 KB |
Output is correct |
16 |
Correct |
16 ms |
31828 KB |
Output is correct |
17 |
Correct |
16 ms |
31824 KB |
Output is correct |
18 |
Correct |
15 ms |
31700 KB |
Output is correct |
19 |
Correct |
48 ms |
33100 KB |
Output is correct |
20 |
Correct |
49 ms |
33004 KB |
Output is correct |
21 |
Correct |
47 ms |
33352 KB |
Output is correct |
22 |
Correct |
40 ms |
33376 KB |
Output is correct |
23 |
Correct |
42 ms |
33484 KB |
Output is correct |
24 |
Correct |
41 ms |
33460 KB |
Output is correct |
25 |
Correct |
44 ms |
33084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
15 ms |
31552 KB |
Output is correct |
3 |
Correct |
15 ms |
31728 KB |
Output is correct |
4 |
Correct |
16 ms |
31772 KB |
Output is correct |
5 |
Correct |
16 ms |
31716 KB |
Output is correct |
6 |
Correct |
15 ms |
31700 KB |
Output is correct |
7 |
Correct |
16 ms |
31760 KB |
Output is correct |
8 |
Correct |
16 ms |
31828 KB |
Output is correct |
9 |
Correct |
17 ms |
31708 KB |
Output is correct |
10 |
Correct |
16 ms |
31600 KB |
Output is correct |
11 |
Correct |
14 ms |
31640 KB |
Output is correct |
12 |
Correct |
16 ms |
31968 KB |
Output is correct |
13 |
Correct |
16 ms |
31720 KB |
Output is correct |
14 |
Correct |
16 ms |
31680 KB |
Output is correct |
15 |
Correct |
15 ms |
31692 KB |
Output is correct |
16 |
Correct |
16 ms |
31828 KB |
Output is correct |
17 |
Correct |
16 ms |
31840 KB |
Output is correct |
18 |
Correct |
15 ms |
31700 KB |
Output is correct |
19 |
Correct |
162 ms |
45960 KB |
Output is correct |
20 |
Correct |
167 ms |
46020 KB |
Output is correct |
21 |
Correct |
162 ms |
45956 KB |
Output is correct |
22 |
Correct |
168 ms |
47688 KB |
Output is correct |
23 |
Correct |
214 ms |
50708 KB |
Output is correct |
24 |
Correct |
211 ms |
50656 KB |
Output is correct |
25 |
Correct |
92 ms |
41068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
46332 KB |
Output is correct |
2 |
Correct |
193 ms |
46456 KB |
Output is correct |
3 |
Correct |
208 ms |
46252 KB |
Output is correct |
4 |
Correct |
210 ms |
51484 KB |
Output is correct |
5 |
Correct |
212 ms |
47096 KB |
Output is correct |
6 |
Correct |
241 ms |
51540 KB |
Output is correct |
7 |
Correct |
244 ms |
51592 KB |
Output is correct |
8 |
Correct |
231 ms |
51640 KB |
Output is correct |
9 |
Correct |
221 ms |
50744 KB |
Output is correct |
10 |
Correct |
244 ms |
51208 KB |
Output is correct |
11 |
Correct |
250 ms |
50892 KB |
Output is correct |
12 |
Correct |
246 ms |
51140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
31640 KB |
Output is correct |
2 |
Correct |
199 ms |
46272 KB |
Output is correct |
3 |
Correct |
215 ms |
46200 KB |
Output is correct |
4 |
Correct |
222 ms |
46208 KB |
Output is correct |
5 |
Correct |
198 ms |
46832 KB |
Output is correct |
6 |
Correct |
199 ms |
47280 KB |
Output is correct |
7 |
Correct |
223 ms |
47384 KB |
Output is correct |
8 |
Correct |
207 ms |
51588 KB |
Output is correct |
9 |
Correct |
212 ms |
51864 KB |
Output is correct |
10 |
Correct |
199 ms |
47052 KB |
Output is correct |
11 |
Correct |
220 ms |
48884 KB |
Output is correct |
12 |
Correct |
140 ms |
47196 KB |
Output is correct |
13 |
Correct |
14 ms |
31576 KB |
Output is correct |
14 |
Correct |
15 ms |
31552 KB |
Output is correct |
15 |
Correct |
15 ms |
31728 KB |
Output is correct |
16 |
Correct |
16 ms |
31772 KB |
Output is correct |
17 |
Correct |
16 ms |
31716 KB |
Output is correct |
18 |
Correct |
15 ms |
31700 KB |
Output is correct |
19 |
Correct |
16 ms |
31760 KB |
Output is correct |
20 |
Correct |
16 ms |
31828 KB |
Output is correct |
21 |
Correct |
17 ms |
31708 KB |
Output is correct |
22 |
Correct |
15 ms |
31544 KB |
Output is correct |
23 |
Correct |
15 ms |
31572 KB |
Output is correct |
24 |
Correct |
16 ms |
31700 KB |
Output is correct |
25 |
Correct |
17 ms |
31772 KB |
Output is correct |
26 |
Correct |
17 ms |
31780 KB |
Output is correct |
27 |
Correct |
17 ms |
31844 KB |
Output is correct |
28 |
Correct |
16 ms |
31828 KB |
Output is correct |
29 |
Correct |
16 ms |
31824 KB |
Output is correct |
30 |
Correct |
15 ms |
31700 KB |
Output is correct |
31 |
Correct |
48 ms |
33100 KB |
Output is correct |
32 |
Correct |
49 ms |
33004 KB |
Output is correct |
33 |
Correct |
47 ms |
33352 KB |
Output is correct |
34 |
Correct |
40 ms |
33376 KB |
Output is correct |
35 |
Correct |
42 ms |
33484 KB |
Output is correct |
36 |
Correct |
41 ms |
33460 KB |
Output is correct |
37 |
Correct |
44 ms |
33084 KB |
Output is correct |
38 |
Correct |
16 ms |
31600 KB |
Output is correct |
39 |
Correct |
14 ms |
31640 KB |
Output is correct |
40 |
Correct |
16 ms |
31968 KB |
Output is correct |
41 |
Correct |
16 ms |
31720 KB |
Output is correct |
42 |
Correct |
16 ms |
31680 KB |
Output is correct |
43 |
Correct |
15 ms |
31692 KB |
Output is correct |
44 |
Correct |
16 ms |
31828 KB |
Output is correct |
45 |
Correct |
16 ms |
31840 KB |
Output is correct |
46 |
Correct |
15 ms |
31700 KB |
Output is correct |
47 |
Correct |
162 ms |
45960 KB |
Output is correct |
48 |
Correct |
167 ms |
46020 KB |
Output is correct |
49 |
Correct |
162 ms |
45956 KB |
Output is correct |
50 |
Correct |
168 ms |
47688 KB |
Output is correct |
51 |
Correct |
214 ms |
50708 KB |
Output is correct |
52 |
Correct |
211 ms |
50656 KB |
Output is correct |
53 |
Correct |
92 ms |
41068 KB |
Output is correct |
54 |
Correct |
191 ms |
46332 KB |
Output is correct |
55 |
Correct |
193 ms |
46456 KB |
Output is correct |
56 |
Correct |
208 ms |
46252 KB |
Output is correct |
57 |
Correct |
210 ms |
51484 KB |
Output is correct |
58 |
Correct |
212 ms |
47096 KB |
Output is correct |
59 |
Correct |
241 ms |
51540 KB |
Output is correct |
60 |
Correct |
244 ms |
51592 KB |
Output is correct |
61 |
Correct |
231 ms |
51640 KB |
Output is correct |
62 |
Correct |
221 ms |
50744 KB |
Output is correct |
63 |
Correct |
244 ms |
51208 KB |
Output is correct |
64 |
Correct |
250 ms |
50892 KB |
Output is correct |
65 |
Correct |
246 ms |
51140 KB |
Output is correct |
66 |
Correct |
14 ms |
31584 KB |
Output is correct |
67 |
Correct |
187 ms |
46748 KB |
Output is correct |
68 |
Correct |
193 ms |
47416 KB |
Output is correct |
69 |
Correct |
235 ms |
46584 KB |
Output is correct |
70 |
Correct |
193 ms |
47360 KB |
Output is correct |
71 |
Correct |
207 ms |
47536 KB |
Output is correct |
72 |
Correct |
212 ms |
48332 KB |
Output is correct |
73 |
Correct |
207 ms |
51736 KB |
Output is correct |
74 |
Correct |
232 ms |
53516 KB |
Output is correct |
75 |
Correct |
208 ms |
47264 KB |
Output is correct |
76 |
Correct |
202 ms |
48652 KB |
Output is correct |
77 |
Correct |
155 ms |
46828 KB |
Output is correct |
78 |
Correct |
15 ms |
31572 KB |
Output is correct |
79 |
Correct |
18 ms |
31700 KB |
Output is correct |
80 |
Correct |
16 ms |
31684 KB |
Output is correct |
81 |
Correct |
15 ms |
31700 KB |
Output is correct |
82 |
Correct |
16 ms |
31720 KB |
Output is correct |
83 |
Correct |
16 ms |
31840 KB |
Output is correct |
84 |
Correct |
16 ms |
31716 KB |
Output is correct |
85 |
Correct |
16 ms |
31668 KB |
Output is correct |
86 |
Correct |
48 ms |
33536 KB |
Output is correct |
87 |
Correct |
49 ms |
33740 KB |
Output is correct |
88 |
Correct |
53 ms |
33428 KB |
Output is correct |
89 |
Correct |
42 ms |
34008 KB |
Output is correct |
90 |
Correct |
40 ms |
34204 KB |
Output is correct |
91 |
Correct |
42 ms |
34080 KB |
Output is correct |
92 |
Correct |
45 ms |
33636 KB |
Output is correct |
93 |
Correct |
185 ms |
47260 KB |
Output is correct |
94 |
Correct |
165 ms |
46656 KB |
Output is correct |
95 |
Correct |
195 ms |
46888 KB |
Output is correct |
96 |
Correct |
211 ms |
47600 KB |
Output is correct |
97 |
Correct |
237 ms |
50908 KB |
Output is correct |
98 |
Correct |
232 ms |
50828 KB |
Output is correct |
99 |
Correct |
99 ms |
41240 KB |
Output is correct |
100 |
Correct |
272 ms |
51640 KB |
Output is correct |
101 |
Correct |
248 ms |
51816 KB |
Output is correct |
102 |
Correct |
263 ms |
52808 KB |
Output is correct |
103 |
Correct |
251 ms |
52684 KB |
Output is correct |
104 |
Correct |
280 ms |
51912 KB |
Output is correct |
105 |
Correct |
236 ms |
52812 KB |
Output is correct |
106 |
Correct |
187 ms |
47292 KB |
Output is correct |
107 |
Correct |
205 ms |
48520 KB |
Output is correct |
108 |
Correct |
203 ms |
48544 KB |
Output is correct |
109 |
Correct |
183 ms |
47856 KB |
Output is correct |
110 |
Correct |
249 ms |
51780 KB |
Output is correct |
111 |
Correct |
236 ms |
51344 KB |
Output is correct |