#include <bits/stdc++.h>
using namespace std;
int n, q;
int s[100005], e[100005];
int minl[200005];
int f[200005];
void normalize()
{
vector<int> pp;
map<int, int> norma;
for (int i = 1; i <= n; i++)
pp.push_back(s[i]), pp.push_back(e[i]);
sort(pp.begin(), pp.end());
int poo = 0;
for (auto it : pp)
if (!norma[it])
norma[it] = ++poo;
for (int i = 1; i <= n; i++)
s[i] = norma[s[i]], e[i] = norma[e[i]];
for (int i = 1; i <= 2 * n; i++)
minl[i] = i;
for (int i = 1; i <= n; i++)
minl[e[i]] = min(minl[e[i]], s[i]);
}
pair<int, int> aint[800005];///val, pos
void build(int nod, int l, int r)
{
if (l == r)
{
aint[nod] = {minl[l], l};
return;
}
int mij = (l + r) / 2;
build(2 * nod, l, mij);
build(2 * nod + 1, mij + 1, r);
aint[nod] = min(aint[2 * nod], aint[2 * nod + 1]);
}
pair<int, int> query(int nod, int l, int r, int st, int dr)
{
if (r < st or dr < l)
return {1e9, 0};
else if (st <= l and r <= dr)
return aint[nod];
int mij = (l + r) / 2;
return min(query(2 * nod, l, mij, st, dr), query(2 * nod + 1, mij + 1, r, st, dr));
}
int lg[200005];
int bl[20][200005], bl1[20][200005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> s[i] >> e[i];
normalize();
build(1, 1, 2 * n);
for (int i = 1; i <= 2 * n; i++)
f[i] = query(1, 1, 2 * n, minl[i], i).second;
for (int i = 2; i <= 2 * n; i++)
lg[i] = 1 + lg[i / 2];
for (int i = 1; i <= 2 * n; i++)
bl[0][i] = f[i];
for (int j = 1; j <= lg[2 * n]; j++)
for (int i = 1; i <= 2 * n; i++)
bl[j][i] = bl[j - 1][bl[j - 1][i]];
for (int i = 1; i <= 2 * n; i++)
bl1[0][i] = i;
for (int j = 1; j <= lg[2 * n]; j++)
for (int i = 1; i <= 2 * n; i++)
bl1[j][i] = bl[j - 1][bl1[j - 1][i]];
for (int i = 1; i <= q; i++)
{
int x, y;
cin >> x >> y;
int xx = x, yy = y;
if (x == y)
{
cout << "0\n";
continue;
}
x = e[x], y = e[y];
swap(x, y);
if (x < y)
cout << "impossible\n";
else if (s[yy] <= e[xx])
cout << "1\n";
else
{
int xs = x, ys = y;
int ans = 1;
for (int pas = lg[2 * n]; pas >= 0; pas--)
{
if (minl[bl1[pas][x]] > y)
ans += (1 << pas), x = bl[pas][x];
}
if (minl[x] > y)
cout << "impossible\n";
else
{
if (s[yy] == minl[xs])
cout << ans << '\n';
else
{
///maybe ans + 1?
x = query(1, 1, 2 * n, s[yy], xs - 1).second;
int ans2 = ans;
ans = 2;
for (int pas = lg[2 * n]; pas >= 0; pas--)
{
if (minl[bl1[pas][x]] > y)
ans += (1 << pas), x = bl[pas][x];
}
if (minl[x] > y)
cout << ans2 + 1 << '\n';
else
cout << min(ans2 + 1, ans) << '\n';
}
}
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:99:25: warning: unused variable 'ys' [-Wunused-variable]
99 | int xs = x, ys = y;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14672 KB |
Output is correct |
2 |
Correct |
139 ms |
46384 KB |
Output is correct |
3 |
Correct |
171 ms |
46428 KB |
Output is correct |
4 |
Correct |
149 ms |
46256 KB |
Output is correct |
5 |
Correct |
140 ms |
46768 KB |
Output is correct |
6 |
Correct |
159 ms |
47280 KB |
Output is correct |
7 |
Correct |
163 ms |
47536 KB |
Output is correct |
8 |
Correct |
180 ms |
51552 KB |
Output is correct |
9 |
Correct |
181 ms |
51444 KB |
Output is correct |
10 |
Correct |
170 ms |
46688 KB |
Output is correct |
11 |
Correct |
171 ms |
48560 KB |
Output is correct |
12 |
Correct |
111 ms |
49144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
23120 KB |
Output is correct |
4 |
Correct |
4 ms |
23120 KB |
Output is correct |
5 |
Correct |
4 ms |
23288 KB |
Output is correct |
6 |
Correct |
5 ms |
23120 KB |
Output is correct |
7 |
Correct |
3 ms |
23124 KB |
Output is correct |
8 |
Correct |
4 ms |
23124 KB |
Output is correct |
9 |
Correct |
3 ms |
23032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
23120 KB |
Output is correct |
4 |
Correct |
4 ms |
23120 KB |
Output is correct |
5 |
Correct |
4 ms |
23288 KB |
Output is correct |
6 |
Correct |
5 ms |
23120 KB |
Output is correct |
7 |
Correct |
3 ms |
23124 KB |
Output is correct |
8 |
Correct |
4 ms |
23124 KB |
Output is correct |
9 |
Correct |
3 ms |
23032 KB |
Output is correct |
10 |
Correct |
2 ms |
12764 KB |
Output is correct |
11 |
Correct |
2 ms |
14768 KB |
Output is correct |
12 |
Correct |
3 ms |
23124 KB |
Output is correct |
13 |
Correct |
4 ms |
23124 KB |
Output is correct |
14 |
Correct |
4 ms |
23124 KB |
Output is correct |
15 |
Correct |
4 ms |
23124 KB |
Output is correct |
16 |
Correct |
4 ms |
23124 KB |
Output is correct |
17 |
Correct |
4 ms |
23124 KB |
Output is correct |
18 |
Correct |
3 ms |
22868 KB |
Output is correct |
19 |
Correct |
27 ms |
28996 KB |
Output is correct |
20 |
Correct |
24 ms |
29012 KB |
Output is correct |
21 |
Correct |
28 ms |
29260 KB |
Output is correct |
22 |
Correct |
25 ms |
29280 KB |
Output is correct |
23 |
Correct |
21 ms |
29372 KB |
Output is correct |
24 |
Correct |
20 ms |
29280 KB |
Output is correct |
25 |
Correct |
24 ms |
29004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
23120 KB |
Output is correct |
4 |
Correct |
4 ms |
23120 KB |
Output is correct |
5 |
Correct |
4 ms |
23288 KB |
Output is correct |
6 |
Correct |
5 ms |
23120 KB |
Output is correct |
7 |
Correct |
3 ms |
23124 KB |
Output is correct |
8 |
Correct |
4 ms |
23124 KB |
Output is correct |
9 |
Correct |
3 ms |
23032 KB |
Output is correct |
10 |
Correct |
2 ms |
12764 KB |
Output is correct |
11 |
Correct |
2 ms |
14676 KB |
Output is correct |
12 |
Correct |
4 ms |
23124 KB |
Output is correct |
13 |
Correct |
4 ms |
23124 KB |
Output is correct |
14 |
Correct |
4 ms |
23124 KB |
Output is correct |
15 |
Correct |
5 ms |
23124 KB |
Output is correct |
16 |
Correct |
4 ms |
23124 KB |
Output is correct |
17 |
Correct |
4 ms |
23288 KB |
Output is correct |
18 |
Correct |
3 ms |
22868 KB |
Output is correct |
19 |
Correct |
126 ms |
47660 KB |
Output is correct |
20 |
Correct |
120 ms |
46772 KB |
Output is correct |
21 |
Correct |
125 ms |
47540 KB |
Output is correct |
22 |
Correct |
132 ms |
49340 KB |
Output is correct |
23 |
Correct |
161 ms |
52284 KB |
Output is correct |
24 |
Correct |
161 ms |
52404 KB |
Output is correct |
25 |
Correct |
48 ms |
42344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
46264 KB |
Output is correct |
2 |
Correct |
137 ms |
46436 KB |
Output is correct |
3 |
Correct |
144 ms |
46256 KB |
Output is correct |
4 |
Correct |
160 ms |
51560 KB |
Output is correct |
5 |
Correct |
145 ms |
46768 KB |
Output is correct |
6 |
Correct |
206 ms |
51120 KB |
Output is correct |
7 |
Correct |
199 ms |
51040 KB |
Output is correct |
8 |
Correct |
205 ms |
51380 KB |
Output is correct |
9 |
Correct |
173 ms |
50348 KB |
Output is correct |
10 |
Correct |
210 ms |
50720 KB |
Output is correct |
11 |
Correct |
205 ms |
50608 KB |
Output is correct |
12 |
Correct |
189 ms |
50872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14672 KB |
Output is correct |
2 |
Correct |
139 ms |
46384 KB |
Output is correct |
3 |
Correct |
171 ms |
46428 KB |
Output is correct |
4 |
Correct |
149 ms |
46256 KB |
Output is correct |
5 |
Correct |
140 ms |
46768 KB |
Output is correct |
6 |
Correct |
159 ms |
47280 KB |
Output is correct |
7 |
Correct |
163 ms |
47536 KB |
Output is correct |
8 |
Correct |
180 ms |
51552 KB |
Output is correct |
9 |
Correct |
181 ms |
51444 KB |
Output is correct |
10 |
Correct |
170 ms |
46688 KB |
Output is correct |
11 |
Correct |
171 ms |
48560 KB |
Output is correct |
12 |
Correct |
111 ms |
49144 KB |
Output is correct |
13 |
Correct |
2 ms |
12624 KB |
Output is correct |
14 |
Correct |
2 ms |
14672 KB |
Output is correct |
15 |
Correct |
3 ms |
23120 KB |
Output is correct |
16 |
Correct |
4 ms |
23120 KB |
Output is correct |
17 |
Correct |
4 ms |
23288 KB |
Output is correct |
18 |
Correct |
5 ms |
23120 KB |
Output is correct |
19 |
Correct |
3 ms |
23124 KB |
Output is correct |
20 |
Correct |
4 ms |
23124 KB |
Output is correct |
21 |
Correct |
3 ms |
23032 KB |
Output is correct |
22 |
Correct |
2 ms |
12764 KB |
Output is correct |
23 |
Correct |
2 ms |
14768 KB |
Output is correct |
24 |
Correct |
3 ms |
23124 KB |
Output is correct |
25 |
Correct |
4 ms |
23124 KB |
Output is correct |
26 |
Correct |
4 ms |
23124 KB |
Output is correct |
27 |
Correct |
4 ms |
23124 KB |
Output is correct |
28 |
Correct |
4 ms |
23124 KB |
Output is correct |
29 |
Correct |
4 ms |
23124 KB |
Output is correct |
30 |
Correct |
3 ms |
22868 KB |
Output is correct |
31 |
Correct |
27 ms |
28996 KB |
Output is correct |
32 |
Correct |
24 ms |
29012 KB |
Output is correct |
33 |
Correct |
28 ms |
29260 KB |
Output is correct |
34 |
Correct |
25 ms |
29280 KB |
Output is correct |
35 |
Correct |
21 ms |
29372 KB |
Output is correct |
36 |
Correct |
20 ms |
29280 KB |
Output is correct |
37 |
Correct |
24 ms |
29004 KB |
Output is correct |
38 |
Correct |
2 ms |
12764 KB |
Output is correct |
39 |
Correct |
2 ms |
14676 KB |
Output is correct |
40 |
Correct |
4 ms |
23124 KB |
Output is correct |
41 |
Correct |
4 ms |
23124 KB |
Output is correct |
42 |
Correct |
4 ms |
23124 KB |
Output is correct |
43 |
Correct |
5 ms |
23124 KB |
Output is correct |
44 |
Correct |
4 ms |
23124 KB |
Output is correct |
45 |
Correct |
4 ms |
23288 KB |
Output is correct |
46 |
Correct |
3 ms |
22868 KB |
Output is correct |
47 |
Correct |
126 ms |
47660 KB |
Output is correct |
48 |
Correct |
120 ms |
46772 KB |
Output is correct |
49 |
Correct |
125 ms |
47540 KB |
Output is correct |
50 |
Correct |
132 ms |
49340 KB |
Output is correct |
51 |
Correct |
161 ms |
52284 KB |
Output is correct |
52 |
Correct |
161 ms |
52404 KB |
Output is correct |
53 |
Correct |
48 ms |
42344 KB |
Output is correct |
54 |
Correct |
134 ms |
46264 KB |
Output is correct |
55 |
Correct |
137 ms |
46436 KB |
Output is correct |
56 |
Correct |
144 ms |
46256 KB |
Output is correct |
57 |
Correct |
160 ms |
51560 KB |
Output is correct |
58 |
Correct |
145 ms |
46768 KB |
Output is correct |
59 |
Correct |
206 ms |
51120 KB |
Output is correct |
60 |
Correct |
199 ms |
51040 KB |
Output is correct |
61 |
Correct |
205 ms |
51380 KB |
Output is correct |
62 |
Correct |
173 ms |
50348 KB |
Output is correct |
63 |
Correct |
210 ms |
50720 KB |
Output is correct |
64 |
Correct |
205 ms |
50608 KB |
Output is correct |
65 |
Correct |
189 ms |
50872 KB |
Output is correct |
66 |
Correct |
2 ms |
14812 KB |
Output is correct |
67 |
Correct |
145 ms |
49472 KB |
Output is correct |
68 |
Correct |
141 ms |
49348 KB |
Output is correct |
69 |
Correct |
156 ms |
49332 KB |
Output is correct |
70 |
Correct |
151 ms |
49844 KB |
Output is correct |
71 |
Correct |
157 ms |
50540 KB |
Output is correct |
72 |
Correct |
149 ms |
50624 KB |
Output is correct |
73 |
Correct |
178 ms |
54736 KB |
Output is correct |
74 |
Correct |
166 ms |
54708 KB |
Output is correct |
75 |
Correct |
159 ms |
49732 KB |
Output is correct |
76 |
Correct |
186 ms |
51640 KB |
Output is correct |
77 |
Correct |
108 ms |
49236 KB |
Output is correct |
78 |
Correct |
2 ms |
12792 KB |
Output is correct |
79 |
Correct |
4 ms |
23036 KB |
Output is correct |
80 |
Correct |
4 ms |
23124 KB |
Output is correct |
81 |
Correct |
3 ms |
23140 KB |
Output is correct |
82 |
Correct |
4 ms |
23124 KB |
Output is correct |
83 |
Correct |
4 ms |
23288 KB |
Output is correct |
84 |
Correct |
5 ms |
23008 KB |
Output is correct |
85 |
Correct |
3 ms |
22868 KB |
Output is correct |
86 |
Correct |
29 ms |
29012 KB |
Output is correct |
87 |
Correct |
26 ms |
29004 KB |
Output is correct |
88 |
Correct |
32 ms |
29260 KB |
Output is correct |
89 |
Correct |
27 ms |
29436 KB |
Output is correct |
90 |
Correct |
25 ms |
29268 KB |
Output is correct |
91 |
Correct |
22 ms |
29268 KB |
Output is correct |
92 |
Correct |
23 ms |
29096 KB |
Output is correct |
93 |
Correct |
123 ms |
47620 KB |
Output is correct |
94 |
Correct |
123 ms |
46772 KB |
Output is correct |
95 |
Correct |
131 ms |
47540 KB |
Output is correct |
96 |
Correct |
136 ms |
49336 KB |
Output is correct |
97 |
Correct |
165 ms |
52408 KB |
Output is correct |
98 |
Correct |
163 ms |
52420 KB |
Output is correct |
99 |
Correct |
49 ms |
42372 KB |
Output is correct |
100 |
Correct |
189 ms |
54332 KB |
Output is correct |
101 |
Correct |
197 ms |
54228 KB |
Output is correct |
102 |
Correct |
183 ms |
54492 KB |
Output is correct |
103 |
Correct |
180 ms |
53940 KB |
Output is correct |
104 |
Correct |
200 ms |
53792 KB |
Output is correct |
105 |
Correct |
187 ms |
53944 KB |
Output is correct |
106 |
Correct |
140 ms |
47740 KB |
Output is correct |
107 |
Correct |
154 ms |
48820 KB |
Output is correct |
108 |
Correct |
149 ms |
49600 KB |
Output is correct |
109 |
Correct |
153 ms |
49588 KB |
Output is correct |
110 |
Correct |
192 ms |
54196 KB |
Output is correct |
111 |
Correct |
195 ms |
54136 KB |
Output is correct |