#include <bits/stdc++.h>
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif
using namespace std;
#define int int64_t
template <class T>
istream &operator>>(istream &in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
struct Range
{
int s, e;
};
struct T
{
int s, i;
bool operator<(T other) const
{
return pair<int, int>(s, i) < pair<int, int>(other.s, other.i);
}
};
int n, q;
vector<Range> ranges;
vector<T> b;
T neutral = {LLONG_MAX / 2, -1};
T update(int l, int r, int i, int k, T x)
{
if (k >= r || k < l)
return b[i];
if (l + 1 == r)
return b[i] = min(b[i], x);
int m = (l + r) / 2;
return b[i] = min(update(l, m, i * 2 + 1, k, x), update(m, r, i * 2 + 2, k, x));
}
T query(int l, int r, int i, int ql, int qr)
{
if (l >= qr || ql >= r)
return neutral;
if (l >= ql && r <= qr)
return b[i];
int m = (l + r) / 2;
return min(query(l, m, i * 2 + 1, ql, qr), query(m, r, i * 2 + 2, ql, qr));
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> q;
ranges = vector<Range>(n);
vector<int> vals;
for (int i = 0; i < n; i++)
{
cin >> ranges[i].s >> ranges[i].e;
vals.push_back(ranges[i].s);
vals.push_back(ranges[i].e);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
b = vector<T>(vals.size() * 4, neutral);
for (int i = 0; i < n; i++)
{
ranges[i].s = lower_bound(vals.begin(), vals.end(), ranges[i].s) - vals.begin();
ranges[i].e = lower_bound(vals.begin(), vals.end(), ranges[i].e) - vals.begin();
}
for (int i = 0; i < n; i++)
update(0, vals.size(), 0, ranges[i].e, {ranges[i].s, i});
vector<vector<int>> prev(20, vector<int>(n, -1));
for (int i = 0; i < n; i++)
{
prev[0][i] = query(0, vals.size(), 0, ranges[i].s, ranges[i].e + 1).i;
if (prev[0][i] == i)
prev[0][i] = -1;
}
for (int k = 1; k < 20; k++)
for (int i = 0; i < n; i++)
{
if (prev[k - 1][i] != -1)
prev[k][i] = prev[k - 1][prev[k - 1][i]];
}
while (q--)
{
int s, e;
cin >> s >> e;
s--, e--;
if (s == e)
{
cout << "0\n";
continue;
}
int cost = 0;
for (int k = 19; k >= 0; k--)
{
if (prev[k][e] != -1 && ranges[prev[k][e]].s > ranges[s].e)
e = prev[k][e], cost += pow(2, k);
}
if (ranges[e].s > ranges[s].e)
{
e = prev[0][e]; // move to reachable one
cost++;
}
if (e != -1 && ranges[e].s <= ranges[s].e && ranges[e].e >= ranges[s].e)
{
cout << cost + (s != e) << "\n";
}
else
{
cout << "impossible\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
185 ms |
26696 KB |
Output is correct |
3 |
Correct |
187 ms |
26748 KB |
Output is correct |
4 |
Correct |
252 ms |
26620 KB |
Output is correct |
5 |
Correct |
211 ms |
27400 KB |
Output is correct |
6 |
Correct |
194 ms |
28076 KB |
Output is correct |
7 |
Correct |
189 ms |
28184 KB |
Output is correct |
8 |
Correct |
186 ms |
33508 KB |
Output is correct |
9 |
Correct |
165 ms |
33508 KB |
Output is correct |
10 |
Correct |
231 ms |
27116 KB |
Output is correct |
11 |
Correct |
215 ms |
29632 KB |
Output is correct |
12 |
Correct |
95 ms |
27256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
46 ms |
2108 KB |
Output is correct |
20 |
Correct |
42 ms |
2092 KB |
Output is correct |
21 |
Correct |
45 ms |
2360 KB |
Output is correct |
22 |
Correct |
39 ms |
2400 KB |
Output is correct |
23 |
Correct |
32 ms |
2516 KB |
Output is correct |
24 |
Correct |
27 ms |
2636 KB |
Output is correct |
25 |
Correct |
32 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
488 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
148 ms |
26868 KB |
Output is correct |
20 |
Correct |
116 ms |
26344 KB |
Output is correct |
21 |
Correct |
137 ms |
26052 KB |
Output is correct |
22 |
Correct |
132 ms |
28400 KB |
Output is correct |
23 |
Correct |
160 ms |
32624 KB |
Output is correct |
24 |
Correct |
153 ms |
32384 KB |
Output is correct |
25 |
Correct |
34 ms |
19932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
26772 KB |
Output is correct |
2 |
Correct |
193 ms |
26692 KB |
Output is correct |
3 |
Correct |
244 ms |
26716 KB |
Output is correct |
4 |
Correct |
176 ms |
33544 KB |
Output is correct |
5 |
Correct |
250 ms |
27052 KB |
Output is correct |
6 |
Correct |
252 ms |
33132 KB |
Output is correct |
7 |
Correct |
278 ms |
33156 KB |
Output is correct |
8 |
Correct |
215 ms |
33220 KB |
Output is correct |
9 |
Correct |
168 ms |
32364 KB |
Output is correct |
10 |
Correct |
260 ms |
32836 KB |
Output is correct |
11 |
Correct |
189 ms |
32580 KB |
Output is correct |
12 |
Correct |
238 ms |
32752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
185 ms |
26696 KB |
Output is correct |
3 |
Correct |
187 ms |
26748 KB |
Output is correct |
4 |
Correct |
252 ms |
26620 KB |
Output is correct |
5 |
Correct |
211 ms |
27400 KB |
Output is correct |
6 |
Correct |
194 ms |
28076 KB |
Output is correct |
7 |
Correct |
189 ms |
28184 KB |
Output is correct |
8 |
Correct |
186 ms |
33508 KB |
Output is correct |
9 |
Correct |
165 ms |
33508 KB |
Output is correct |
10 |
Correct |
231 ms |
27116 KB |
Output is correct |
11 |
Correct |
215 ms |
29632 KB |
Output is correct |
12 |
Correct |
95 ms |
27256 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
46 ms |
2108 KB |
Output is correct |
32 |
Correct |
42 ms |
2092 KB |
Output is correct |
33 |
Correct |
45 ms |
2360 KB |
Output is correct |
34 |
Correct |
39 ms |
2400 KB |
Output is correct |
35 |
Correct |
32 ms |
2516 KB |
Output is correct |
36 |
Correct |
27 ms |
2636 KB |
Output is correct |
37 |
Correct |
32 ms |
2252 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
496 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
488 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
148 ms |
26868 KB |
Output is correct |
48 |
Correct |
116 ms |
26344 KB |
Output is correct |
49 |
Correct |
137 ms |
26052 KB |
Output is correct |
50 |
Correct |
132 ms |
28400 KB |
Output is correct |
51 |
Correct |
160 ms |
32624 KB |
Output is correct |
52 |
Correct |
153 ms |
32384 KB |
Output is correct |
53 |
Correct |
34 ms |
19932 KB |
Output is correct |
54 |
Correct |
168 ms |
26772 KB |
Output is correct |
55 |
Correct |
193 ms |
26692 KB |
Output is correct |
56 |
Correct |
244 ms |
26716 KB |
Output is correct |
57 |
Correct |
176 ms |
33544 KB |
Output is correct |
58 |
Correct |
250 ms |
27052 KB |
Output is correct |
59 |
Correct |
252 ms |
33132 KB |
Output is correct |
60 |
Correct |
278 ms |
33156 KB |
Output is correct |
61 |
Correct |
215 ms |
33220 KB |
Output is correct |
62 |
Correct |
168 ms |
32364 KB |
Output is correct |
63 |
Correct |
260 ms |
32836 KB |
Output is correct |
64 |
Correct |
189 ms |
32580 KB |
Output is correct |
65 |
Correct |
238 ms |
32752 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
180 ms |
26760 KB |
Output is correct |
68 |
Correct |
197 ms |
26844 KB |
Output is correct |
69 |
Correct |
283 ms |
26692 KB |
Output is correct |
70 |
Correct |
185 ms |
27428 KB |
Output is correct |
71 |
Correct |
187 ms |
28032 KB |
Output is correct |
72 |
Correct |
207 ms |
28236 KB |
Output is correct |
73 |
Correct |
165 ms |
33416 KB |
Output is correct |
74 |
Correct |
199 ms |
33756 KB |
Output is correct |
75 |
Correct |
225 ms |
27372 KB |
Output is correct |
76 |
Correct |
223 ms |
29612 KB |
Output is correct |
77 |
Correct |
110 ms |
27288 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
63 ms |
2144 KB |
Output is correct |
87 |
Correct |
45 ms |
2092 KB |
Output is correct |
88 |
Correct |
42 ms |
2268 KB |
Output is correct |
89 |
Correct |
28 ms |
2380 KB |
Output is correct |
90 |
Correct |
28 ms |
2564 KB |
Output is correct |
91 |
Correct |
30 ms |
2476 KB |
Output is correct |
92 |
Correct |
38 ms |
2204 KB |
Output is correct |
93 |
Correct |
127 ms |
26128 KB |
Output is correct |
94 |
Correct |
120 ms |
26156 KB |
Output is correct |
95 |
Correct |
139 ms |
26112 KB |
Output is correct |
96 |
Correct |
150 ms |
28348 KB |
Output is correct |
97 |
Correct |
157 ms |
32500 KB |
Output is correct |
98 |
Correct |
173 ms |
32436 KB |
Output is correct |
99 |
Correct |
32 ms |
19788 KB |
Output is correct |
100 |
Correct |
268 ms |
33164 KB |
Output is correct |
101 |
Correct |
245 ms |
33596 KB |
Output is correct |
102 |
Correct |
245 ms |
33688 KB |
Output is correct |
103 |
Correct |
219 ms |
33080 KB |
Output is correct |
104 |
Correct |
208 ms |
33360 KB |
Output is correct |
105 |
Correct |
207 ms |
33560 KB |
Output is correct |
106 |
Correct |
204 ms |
25900 KB |
Output is correct |
107 |
Correct |
241 ms |
27764 KB |
Output is correct |
108 |
Correct |
189 ms |
27636 KB |
Output is correct |
109 |
Correct |
175 ms |
27472 KB |
Output is correct |
110 |
Correct |
212 ms |
33444 KB |
Output is correct |
111 |
Correct |
215 ms |
33056 KB |
Output is correct |