#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 1e5 + 7;
int l[N], r[N], who[N], p[N][20], n;
V<pi> aux;
int t[N * 4];
int add(int x, int y) {
if(l[x] < l[y]) return x;
else return y;
}
void build(int v = 1, int tl = 0, int tr = n) {
if(tr - tl == 1) {
t[v] = aux[tl].S;
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
t[v] = add(t[v * 2], t[v * 2 + 1]);
}
int qry(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) / 2;
if(r <= tm) return qry(l, r, v * 2, tl, tm);
else if(l >= tm) return qry(l, r, v * 2 + 1, tm, tr);
else return add(qry(l, r, v * 2, tl, tm), qry(l, r, v * 2 + 1, tm, tr));
}
signed main()
{
IO_OP;
int q;
cin >> n >> q;
for(int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
aux.EB(r[i], i);
}
sort(ALL(aux));
build();
for(int i = 0; i < n; i++) {
// who[i] = i;
// for(int j = 0; j < n; j++) if(l[i] <= r[j] && r[j] <= r[i]) {
// if(l[j] < l[who[i]])
// who[i] = j;
// }
int lb = lower_bound(ALL(aux), pi(l[i], -INF)) - aux.begin();
int rb = upper_bound(ALL(aux), pi(r[i], INF)) - aux.begin();
who[i] = qry(lb, rb);
p[i][0] = who[i];
}
for(int j = 1; j < 20; j++)
for(int i = 0; i < n; i++)
p[i][j] = p[p[i][j - 1]][j - 1];
while(q--) {
int s, e;
cin >> s >> e, s--, e--;
if(s == e) {
cout << "0\n";
continue;
}
if(r[s] > r[e]) {
cout << "impossible\n";
continue;
}
if(l[e] <= r[s] && r[s] <= r[e]) {
cout << 1 << '\n';
continue;
}
int ans = 0;
for(int j = 19; j >= 0; j--) {
if(l[p[e][j]] > r[s]) {
ans += 1 << j;
e = p[e][j];
}
}
if(l[who[e]] <= r[s]) {
ans += 2;
cout << ans << '\n';
} else {
cout << "impossible\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
122 ms |
11864 KB |
Output is correct |
3 |
Correct |
110 ms |
11772 KB |
Output is correct |
4 |
Correct |
152 ms |
11772 KB |
Output is correct |
5 |
Correct |
130 ms |
11912 KB |
Output is correct |
6 |
Correct |
129 ms |
11932 KB |
Output is correct |
7 |
Correct |
122 ms |
12024 KB |
Output is correct |
8 |
Correct |
99 ms |
12292 KB |
Output is correct |
9 |
Correct |
102 ms |
12400 KB |
Output is correct |
10 |
Correct |
131 ms |
12232 KB |
Output is correct |
11 |
Correct |
119 ms |
12356 KB |
Output is correct |
12 |
Correct |
72 ms |
12356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
34 ms |
1352 KB |
Output is correct |
20 |
Correct |
29 ms |
1424 KB |
Output is correct |
21 |
Correct |
32 ms |
1612 KB |
Output is correct |
22 |
Correct |
27 ms |
1752 KB |
Output is correct |
23 |
Correct |
24 ms |
1524 KB |
Output is correct |
24 |
Correct |
33 ms |
1480 KB |
Output is correct |
25 |
Correct |
28 ms |
1240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
372 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
75 ms |
11256 KB |
Output is correct |
20 |
Correct |
94 ms |
11252 KB |
Output is correct |
21 |
Correct |
86 ms |
11204 KB |
Output is correct |
22 |
Correct |
75 ms |
11192 KB |
Output is correct |
23 |
Correct |
73 ms |
11248 KB |
Output is correct |
24 |
Correct |
86 ms |
11228 KB |
Output is correct |
25 |
Correct |
41 ms |
11272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
11868 KB |
Output is correct |
2 |
Correct |
116 ms |
11872 KB |
Output is correct |
3 |
Correct |
147 ms |
11872 KB |
Output is correct |
4 |
Correct |
95 ms |
12272 KB |
Output is correct |
5 |
Correct |
115 ms |
12168 KB |
Output is correct |
6 |
Correct |
169 ms |
11908 KB |
Output is correct |
7 |
Correct |
126 ms |
11924 KB |
Output is correct |
8 |
Correct |
118 ms |
12108 KB |
Output is correct |
9 |
Correct |
89 ms |
11180 KB |
Output is correct |
10 |
Correct |
122 ms |
11672 KB |
Output is correct |
11 |
Correct |
116 ms |
11460 KB |
Output is correct |
12 |
Correct |
127 ms |
11648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
122 ms |
11864 KB |
Output is correct |
3 |
Correct |
110 ms |
11772 KB |
Output is correct |
4 |
Correct |
152 ms |
11772 KB |
Output is correct |
5 |
Correct |
130 ms |
11912 KB |
Output is correct |
6 |
Correct |
129 ms |
11932 KB |
Output is correct |
7 |
Correct |
122 ms |
12024 KB |
Output is correct |
8 |
Correct |
99 ms |
12292 KB |
Output is correct |
9 |
Correct |
102 ms |
12400 KB |
Output is correct |
10 |
Correct |
131 ms |
12232 KB |
Output is correct |
11 |
Correct |
119 ms |
12356 KB |
Output is correct |
12 |
Correct |
72 ms |
12356 KB |
Output is correct |
13 |
Correct |
1 ms |
264 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
34 ms |
1352 KB |
Output is correct |
32 |
Correct |
29 ms |
1424 KB |
Output is correct |
33 |
Correct |
32 ms |
1612 KB |
Output is correct |
34 |
Correct |
27 ms |
1752 KB |
Output is correct |
35 |
Correct |
24 ms |
1524 KB |
Output is correct |
36 |
Correct |
33 ms |
1480 KB |
Output is correct |
37 |
Correct |
28 ms |
1240 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
372 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
75 ms |
11256 KB |
Output is correct |
48 |
Correct |
94 ms |
11252 KB |
Output is correct |
49 |
Correct |
86 ms |
11204 KB |
Output is correct |
50 |
Correct |
75 ms |
11192 KB |
Output is correct |
51 |
Correct |
73 ms |
11248 KB |
Output is correct |
52 |
Correct |
86 ms |
11228 KB |
Output is correct |
53 |
Correct |
41 ms |
11272 KB |
Output is correct |
54 |
Correct |
103 ms |
11868 KB |
Output is correct |
55 |
Correct |
116 ms |
11872 KB |
Output is correct |
56 |
Correct |
147 ms |
11872 KB |
Output is correct |
57 |
Correct |
95 ms |
12272 KB |
Output is correct |
58 |
Correct |
115 ms |
12168 KB |
Output is correct |
59 |
Correct |
169 ms |
11908 KB |
Output is correct |
60 |
Correct |
126 ms |
11924 KB |
Output is correct |
61 |
Correct |
118 ms |
12108 KB |
Output is correct |
62 |
Correct |
89 ms |
11180 KB |
Output is correct |
63 |
Correct |
122 ms |
11672 KB |
Output is correct |
64 |
Correct |
116 ms |
11460 KB |
Output is correct |
65 |
Correct |
127 ms |
11648 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
119 ms |
11844 KB |
Output is correct |
68 |
Correct |
112 ms |
11868 KB |
Output is correct |
69 |
Correct |
174 ms |
11788 KB |
Output is correct |
70 |
Correct |
124 ms |
11856 KB |
Output is correct |
71 |
Correct |
110 ms |
11920 KB |
Output is correct |
72 |
Correct |
137 ms |
11912 KB |
Output is correct |
73 |
Correct |
107 ms |
12308 KB |
Output is correct |
74 |
Correct |
104 ms |
12356 KB |
Output is correct |
75 |
Correct |
123 ms |
12160 KB |
Output is correct |
76 |
Correct |
115 ms |
12268 KB |
Output is correct |
77 |
Correct |
79 ms |
12300 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
33 ms |
1428 KB |
Output is correct |
87 |
Correct |
30 ms |
1396 KB |
Output is correct |
88 |
Correct |
32 ms |
1640 KB |
Output is correct |
89 |
Correct |
24 ms |
1740 KB |
Output is correct |
90 |
Correct |
26 ms |
1556 KB |
Output is correct |
91 |
Correct |
23 ms |
1572 KB |
Output is correct |
92 |
Correct |
26 ms |
1228 KB |
Output is correct |
93 |
Correct |
78 ms |
11256 KB |
Output is correct |
94 |
Correct |
76 ms |
11208 KB |
Output is correct |
95 |
Correct |
82 ms |
11144 KB |
Output is correct |
96 |
Correct |
81 ms |
11212 KB |
Output is correct |
97 |
Correct |
73 ms |
11240 KB |
Output is correct |
98 |
Correct |
90 ms |
11268 KB |
Output is correct |
99 |
Correct |
43 ms |
11144 KB |
Output is correct |
100 |
Correct |
132 ms |
11916 KB |
Output is correct |
101 |
Correct |
128 ms |
11996 KB |
Output is correct |
102 |
Correct |
116 ms |
12100 KB |
Output is correct |
103 |
Correct |
133 ms |
11636 KB |
Output is correct |
104 |
Correct |
154 ms |
11376 KB |
Output is correct |
105 |
Correct |
122 ms |
11616 KB |
Output is correct |
106 |
Correct |
124 ms |
11932 KB |
Output is correct |
107 |
Correct |
148 ms |
12040 KB |
Output is correct |
108 |
Correct |
115 ms |
12052 KB |
Output is correct |
109 |
Correct |
117 ms |
12032 KB |
Output is correct |
110 |
Correct |
153 ms |
11900 KB |
Output is correct |
111 |
Correct |
121 ms |
11816 KB |
Output is correct |