# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
int n, q;
pair <pair <int, int>, int> p[maxn];
int t[maxn * 8], up[maxn][20];
int combine (int x, int y)
{
if (x == 0) return y;
if (y == 0) return x;
return (p[x].ff.ff < p[y].ff.ff ? x : y);
}
void update (int pos, int x, int v = 1, int tl = 1, int tr = n + n)
{
if (tl == tr)
{
t[v] = combine(t[v], x);
return;
}
else
{
int tm = (tl + tr) >> 1;
if (pos <= tm)
{
update(pos, x, v * 2, tl, tm);
}
else
{
update(pos, x, v * 2 + 1, tm + 1, tr);
}
t[v] = combine(t[v * 2], t[v * 2 + 1]);
}
}
int get (int l, int r, int v = 1, int tl = 1, int tr = n + n)
{
if (tr < l || r < tl) return 0;
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
return combine(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}
void ma1n (/* SABR */)
{
cin >> n >> q;
vector <int> v;
for (int i = 1; i <= n; ++i)
{
cin >> p[i].ff.ff >> p[i].ff.ss;
p[i].ss = i;
v.pb(p[i].ff.ff);
v.pb(p[i].ff.ss);
}
sort(v.begin(), v.end());
auto ittttt = unique(v.begin(), v.end());
v.resize(distance(v.begin(), ittttt));
for (int i = 1; i <= n; ++i)
{
p[i].ff.ff = lower_bound(v.begin(), v.end(), p[i].ff.ff) - v.begin() + 1;
p[i].ff.ss = lower_bound(v.begin(), v.end(), p[i].ff.ss) - v.begin() + 1;
}
for (int i = 1; i <= n; ++i)
{
update(p[i].ff.ss, i);
}
for (int i = 1; i <= n; ++i)
{
up[i][0] = get(p[i].ff.ff, p[i].ff.ss);
}
for (int j = 1; j <= 19; ++j)
{
for (int i = 1; i <= n; ++i)
{
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
while (q--)
{
int id1, id2;
cin >> id1 >> id2;
if (id1 == id2)
{
cout << 0 << nl;
}
else if (p[id1].ff.ss > p[id2].ff.ss)
{
cout << "impossible" << nl;
}
else
{
int x = id2, ans = 0;
for (int j = 19; j >= 0; --j)
{
int nxt = up[x][j];
if (p[id1].ff.ss < p[nxt].ff.ff)
{
ans += (1LL << j);
x = nxt;
}
}
if (p[x].ff.ff <= p[id1].ff.ss && p[id1].ff.ss <= p[x].ff.ss)
{
ans++;
}
else
{
ans += 2;
x = up[x][0];
}
if (p[x].ff.ff <= p[id1].ff.ss && p[id1].ff.ss <= p[x].ff.ss)
{
cout << ans << nl;
}
else
{
cout << "impossible" << nl;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("angry.in", "r", stdin);
// freopen("angry.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
137 ms |
11880 KB |
Output is correct |
3 |
Correct |
132 ms |
11864 KB |
Output is correct |
4 |
Correct |
157 ms |
11848 KB |
Output is correct |
5 |
Correct |
147 ms |
12004 KB |
Output is correct |
6 |
Correct |
145 ms |
15204 KB |
Output is correct |
7 |
Correct |
141 ms |
15272 KB |
Output is correct |
8 |
Correct |
141 ms |
16432 KB |
Output is correct |
9 |
Correct |
135 ms |
16444 KB |
Output is correct |
10 |
Correct |
148 ms |
15364 KB |
Output is correct |
11 |
Correct |
161 ms |
15972 KB |
Output is correct |
12 |
Correct |
96 ms |
14812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
41 ms |
2444 KB |
Output is correct |
20 |
Correct |
41 ms |
2408 KB |
Output is correct |
21 |
Correct |
34 ms |
2632 KB |
Output is correct |
22 |
Correct |
28 ms |
2740 KB |
Output is correct |
23 |
Correct |
28 ms |
2628 KB |
Output is correct |
24 |
Correct |
27 ms |
2600 KB |
Output is correct |
25 |
Correct |
30 ms |
2336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
109 ms |
13136 KB |
Output is correct |
20 |
Correct |
101 ms |
12436 KB |
Output is correct |
21 |
Correct |
121 ms |
13108 KB |
Output is correct |
22 |
Correct |
109 ms |
13584 KB |
Output is correct |
23 |
Correct |
125 ms |
13152 KB |
Output is correct |
24 |
Correct |
122 ms |
14208 KB |
Output is correct |
25 |
Correct |
56 ms |
11480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
11848 KB |
Output is correct |
2 |
Correct |
132 ms |
11812 KB |
Output is correct |
3 |
Correct |
174 ms |
11900 KB |
Output is correct |
4 |
Correct |
144 ms |
13332 KB |
Output is correct |
5 |
Correct |
149 ms |
12148 KB |
Output is correct |
6 |
Correct |
160 ms |
13044 KB |
Output is correct |
7 |
Correct |
162 ms |
16072 KB |
Output is correct |
8 |
Correct |
172 ms |
16328 KB |
Output is correct |
9 |
Correct |
126 ms |
13176 KB |
Output is correct |
10 |
Correct |
179 ms |
15688 KB |
Output is correct |
11 |
Correct |
153 ms |
15392 KB |
Output is correct |
12 |
Correct |
154 ms |
15652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
137 ms |
11880 KB |
Output is correct |
3 |
Correct |
132 ms |
11864 KB |
Output is correct |
4 |
Correct |
157 ms |
11848 KB |
Output is correct |
5 |
Correct |
147 ms |
12004 KB |
Output is correct |
6 |
Correct |
145 ms |
15204 KB |
Output is correct |
7 |
Correct |
141 ms |
15272 KB |
Output is correct |
8 |
Correct |
141 ms |
16432 KB |
Output is correct |
9 |
Correct |
135 ms |
16444 KB |
Output is correct |
10 |
Correct |
148 ms |
15364 KB |
Output is correct |
11 |
Correct |
161 ms |
15972 KB |
Output is correct |
12 |
Correct |
96 ms |
14812 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
41 ms |
2444 KB |
Output is correct |
32 |
Correct |
41 ms |
2408 KB |
Output is correct |
33 |
Correct |
34 ms |
2632 KB |
Output is correct |
34 |
Correct |
28 ms |
2740 KB |
Output is correct |
35 |
Correct |
28 ms |
2628 KB |
Output is correct |
36 |
Correct |
27 ms |
2600 KB |
Output is correct |
37 |
Correct |
30 ms |
2336 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
109 ms |
13136 KB |
Output is correct |
48 |
Correct |
101 ms |
12436 KB |
Output is correct |
49 |
Correct |
121 ms |
13108 KB |
Output is correct |
50 |
Correct |
109 ms |
13584 KB |
Output is correct |
51 |
Correct |
125 ms |
13152 KB |
Output is correct |
52 |
Correct |
122 ms |
14208 KB |
Output is correct |
53 |
Correct |
56 ms |
11480 KB |
Output is correct |
54 |
Correct |
134 ms |
11848 KB |
Output is correct |
55 |
Correct |
132 ms |
11812 KB |
Output is correct |
56 |
Correct |
174 ms |
11900 KB |
Output is correct |
57 |
Correct |
144 ms |
13332 KB |
Output is correct |
58 |
Correct |
149 ms |
12148 KB |
Output is correct |
59 |
Correct |
160 ms |
13044 KB |
Output is correct |
60 |
Correct |
162 ms |
16072 KB |
Output is correct |
61 |
Correct |
172 ms |
16328 KB |
Output is correct |
62 |
Correct |
126 ms |
13176 KB |
Output is correct |
63 |
Correct |
179 ms |
15688 KB |
Output is correct |
64 |
Correct |
153 ms |
15392 KB |
Output is correct |
65 |
Correct |
154 ms |
15652 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
144 ms |
15116 KB |
Output is correct |
68 |
Correct |
133 ms |
14908 KB |
Output is correct |
69 |
Correct |
158 ms |
14936 KB |
Output is correct |
70 |
Correct |
158 ms |
15136 KB |
Output is correct |
71 |
Correct |
141 ms |
15268 KB |
Output is correct |
72 |
Correct |
147 ms |
15352 KB |
Output is correct |
73 |
Correct |
145 ms |
16384 KB |
Output is correct |
74 |
Correct |
136 ms |
16364 KB |
Output is correct |
75 |
Correct |
152 ms |
15436 KB |
Output is correct |
76 |
Correct |
147 ms |
15852 KB |
Output is correct |
77 |
Correct |
94 ms |
14664 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
2 ms |
380 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 |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
34 ms |
2380 KB |
Output is correct |
87 |
Correct |
31 ms |
2528 KB |
Output is correct |
88 |
Correct |
37 ms |
2640 KB |
Output is correct |
89 |
Correct |
29 ms |
2816 KB |
Output is correct |
90 |
Correct |
28 ms |
2652 KB |
Output is correct |
91 |
Correct |
28 ms |
2636 KB |
Output is correct |
92 |
Correct |
30 ms |
2280 KB |
Output is correct |
93 |
Correct |
126 ms |
13236 KB |
Output is correct |
94 |
Correct |
97 ms |
12360 KB |
Output is correct |
95 |
Correct |
114 ms |
13132 KB |
Output is correct |
96 |
Correct |
99 ms |
13632 KB |
Output is correct |
97 |
Correct |
123 ms |
13240 KB |
Output is correct |
98 |
Correct |
134 ms |
14332 KB |
Output is correct |
99 |
Correct |
52 ms |
11460 KB |
Output is correct |
100 |
Correct |
217 ms |
16028 KB |
Output is correct |
101 |
Correct |
168 ms |
16040 KB |
Output is correct |
102 |
Correct |
191 ms |
16216 KB |
Output is correct |
103 |
Correct |
195 ms |
15752 KB |
Output is correct |
104 |
Correct |
151 ms |
15316 KB |
Output is correct |
105 |
Correct |
158 ms |
15700 KB |
Output is correct |
106 |
Correct |
139 ms |
14160 KB |
Output is correct |
107 |
Correct |
194 ms |
14424 KB |
Output is correct |
108 |
Correct |
141 ms |
15048 KB |
Output is correct |
109 |
Correct |
138 ms |
15056 KB |
Output is correct |
110 |
Correct |
182 ms |
15928 KB |
Output is correct |
111 |
Correct |
150 ms |
15916 KB |
Output is correct |