#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) x.begin(), x.end()
#define vf first
#define vs second
const int mod = 998244353;
const int MX = 1000000005;
const int L = 20;
struct Seg
{
int siz;
vector<pair<int, int>> v;
void init(int n)
{
siz = 1;
while (siz < n)
siz *= 2;
v.assign(siz * 2, mp(MX, -1));
}
pair<int, int> merge(pair<int, int> x, pair<int, int> y)
{
pair<int, int> res;
res.vf = min(x.vf, y.vf);
if (x.vf == y.vf)
res.vs = min(x.vs, y.vs);
else if (res.vf == x.vf)
res.vs = x.vs;
else
res.vs = y.vs;
return res;
}
void set(int i, pair<int, int> p, int x, int lx, int rx)
{
if (rx - lx == 1)
{
v[x] = merge(p, v[x]);
return;
}
int m = (lx + rx) / 2;
if (i < m)
set(i, p, 2 * x + 1, lx, m);
else
set(i, p, 2 * x + 2, m, rx);
v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
}
void set(int i, pair<int, int> p)
{
set(i, p, 0, 0, siz);
}
pair<int, int> range_mx(int l, int r, int x, int lx, int rx)
{
if (l >= rx || lx >= r)
return mp(MX, -1);
if (lx >= l && rx <= r)
return v[x];
int m = (lx + rx) / 2;
return merge(range_mx(l, r, 2 * x + 1, lx, m), range_mx(l, r, 2 * x + 2, m, rx));
}
pair<int, int> range_mx(int l, int r)
{
return range_mx(l, r, 0, 0, siz);
}
};
void solve()
{
int n, q;
cin >> n >> q;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i].vf >> v[i].vs;
}
// positions acocrding to sorted e
vector<pair<int, pair<int, int>>> e_sort;
// vector<pair<int,pair<int,int>>> rev;
for (int i = 0; i < n; i++)
{
e_sort.pb(mp(v[i].vs, mp(v[i].vf, i)));
}
sort(all(e_sort));
// rev = e_sort;
// reverse(all(rev));
int pos[n], rev[n];
for (int i = 0; i < n; i++)
{
pos[e_sort[i].vs.vs] = i;
rev[i] = e_sort[i].vs.vs;
}
// segtree
Seg st;
st.init(n + 1);
for (int i = n - 1; i >= 0; i--)
{
st.set(i, mp(e_sort[i].vs.vf, i));
}
// binary lifting
int up[n][L];
int p[n];
for (int i = 0; i < n; i++)
{
pair<int, pair<int, int>> tp = mp(e_sort[i].vs.vf, mp(-1, -1));
int left = (lower_bound(all(e_sort), tp) - e_sort.begin());
if (left == i)
up[i][0] = p[i] = i;
else
{
p[i] = st.range_mx(left, i).vs;
// cout << left << ' ' << st.range_mx(left,i).vf << ' ' << p[i] << '\n';
up[i][0] = p[i];
}
}
for (int j = 1; j < L; j++)
{
for (int i = 0; i < n; i++)
{
// if (j == 1)
// up[i][j] = up[p[i]][j - 1];
// else
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
// answering the queries
while (q--)
{
int s, e;
cin >> s >> e;
s--;
e--;
if (s == e)
{
cout << 0 << '\n';
continue;
}
int ogs = pos[s], oge = pos[e];
if (ogs > oge)
{
if (v[s].vs >= v[e].vf && v[s].vs <= v[e].vs)
{
cout << 1 << '\n';
continue;
}
else
{
cout << "impossible\n";
continue;
}
}
int i = oge;
int mn = v[s].vs;
int ans = 0;
for (int j = L - 1; j >= 0; j--)
{
if (v[rev[up[i][j]]].vf > mn)
{
ans += (1 << j);
i = up[i][j];
}
}
if (v[rev[i]].vf > mn)
{
i = up[i][0];
ans++;
}
ans++;
i = up[i][0];
if (v[rev[i]].vf > mn)
cout << "impossible\n";
else
cout << ans << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// int t;cin >> t;
// while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
456 KB |
Output is correct |
2 |
Correct |
66 ms |
17300 KB |
Output is correct |
3 |
Correct |
72 ms |
17292 KB |
Output is correct |
4 |
Correct |
75 ms |
17092 KB |
Output is correct |
5 |
Correct |
74 ms |
17348 KB |
Output is correct |
6 |
Correct |
69 ms |
17352 KB |
Output is correct |
7 |
Correct |
73 ms |
17348 KB |
Output is correct |
8 |
Correct |
57 ms |
17596 KB |
Output is correct |
9 |
Correct |
59 ms |
17608 KB |
Output is correct |
10 |
Correct |
84 ms |
17456 KB |
Output is correct |
11 |
Correct |
75 ms |
17608 KB |
Output is correct |
12 |
Correct |
52 ms |
16844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
23 ms |
2588 KB |
Output is correct |
20 |
Correct |
19 ms |
2648 KB |
Output is correct |
21 |
Correct |
26 ms |
2900 KB |
Output is correct |
22 |
Correct |
18 ms |
2908 KB |
Output is correct |
23 |
Correct |
17 ms |
2708 KB |
Output is correct |
24 |
Correct |
19 ms |
2900 KB |
Output is correct |
25 |
Correct |
18 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
496 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
52 ms |
15428 KB |
Output is correct |
20 |
Correct |
45 ms |
14536 KB |
Output is correct |
21 |
Correct |
56 ms |
15300 KB |
Output is correct |
22 |
Correct |
47 ms |
15304 KB |
Output is correct |
23 |
Correct |
46 ms |
15296 KB |
Output is correct |
24 |
Correct |
53 ms |
15300 KB |
Output is correct |
25 |
Correct |
39 ms |
15044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
17096 KB |
Output is correct |
2 |
Correct |
71 ms |
17280 KB |
Output is correct |
3 |
Correct |
76 ms |
17096 KB |
Output is correct |
4 |
Correct |
59 ms |
17604 KB |
Output is correct |
5 |
Correct |
78 ms |
17600 KB |
Output is correct |
6 |
Correct |
85 ms |
17352 KB |
Output is correct |
7 |
Correct |
84 ms |
17352 KB |
Output is correct |
8 |
Correct |
74 ms |
17512 KB |
Output is correct |
9 |
Correct |
45 ms |
15512 KB |
Output is correct |
10 |
Correct |
73 ms |
16884 KB |
Output is correct |
11 |
Correct |
74 ms |
16840 KB |
Output is correct |
12 |
Correct |
72 ms |
16840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
456 KB |
Output is correct |
2 |
Correct |
66 ms |
17300 KB |
Output is correct |
3 |
Correct |
72 ms |
17292 KB |
Output is correct |
4 |
Correct |
75 ms |
17092 KB |
Output is correct |
5 |
Correct |
74 ms |
17348 KB |
Output is correct |
6 |
Correct |
69 ms |
17352 KB |
Output is correct |
7 |
Correct |
73 ms |
17348 KB |
Output is correct |
8 |
Correct |
57 ms |
17596 KB |
Output is correct |
9 |
Correct |
59 ms |
17608 KB |
Output is correct |
10 |
Correct |
84 ms |
17456 KB |
Output is correct |
11 |
Correct |
75 ms |
17608 KB |
Output is correct |
12 |
Correct |
52 ms |
16844 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
23 ms |
2588 KB |
Output is correct |
32 |
Correct |
19 ms |
2648 KB |
Output is correct |
33 |
Correct |
26 ms |
2900 KB |
Output is correct |
34 |
Correct |
18 ms |
2908 KB |
Output is correct |
35 |
Correct |
17 ms |
2708 KB |
Output is correct |
36 |
Correct |
19 ms |
2900 KB |
Output is correct |
37 |
Correct |
18 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
496 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
52 ms |
15428 KB |
Output is correct |
48 |
Correct |
45 ms |
14536 KB |
Output is correct |
49 |
Correct |
56 ms |
15300 KB |
Output is correct |
50 |
Correct |
47 ms |
15304 KB |
Output is correct |
51 |
Correct |
46 ms |
15296 KB |
Output is correct |
52 |
Correct |
53 ms |
15300 KB |
Output is correct |
53 |
Correct |
39 ms |
15044 KB |
Output is correct |
54 |
Correct |
65 ms |
17096 KB |
Output is correct |
55 |
Correct |
71 ms |
17280 KB |
Output is correct |
56 |
Correct |
76 ms |
17096 KB |
Output is correct |
57 |
Correct |
59 ms |
17604 KB |
Output is correct |
58 |
Correct |
78 ms |
17600 KB |
Output is correct |
59 |
Correct |
85 ms |
17352 KB |
Output is correct |
60 |
Correct |
84 ms |
17352 KB |
Output is correct |
61 |
Correct |
74 ms |
17512 KB |
Output is correct |
62 |
Correct |
45 ms |
15512 KB |
Output is correct |
63 |
Correct |
73 ms |
16884 KB |
Output is correct |
64 |
Correct |
74 ms |
16840 KB |
Output is correct |
65 |
Correct |
72 ms |
16840 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
70 ms |
17156 KB |
Output is correct |
68 |
Correct |
72 ms |
17096 KB |
Output is correct |
69 |
Correct |
76 ms |
17220 KB |
Output is correct |
70 |
Correct |
70 ms |
17352 KB |
Output is correct |
71 |
Correct |
70 ms |
17352 KB |
Output is correct |
72 |
Correct |
72 ms |
17296 KB |
Output is correct |
73 |
Correct |
57 ms |
17604 KB |
Output is correct |
74 |
Correct |
57 ms |
17728 KB |
Output is correct |
75 |
Correct |
79 ms |
17608 KB |
Output is correct |
76 |
Correct |
72 ms |
17612 KB |
Output is correct |
77 |
Correct |
56 ms |
16836 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
608 KB |
Output is correct |
80 |
Correct |
1 ms |
488 KB |
Output is correct |
81 |
Correct |
1 ms |
600 KB |
Output is correct |
82 |
Correct |
1 ms |
464 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
472 KB |
Output is correct |
85 |
Correct |
1 ms |
608 KB |
Output is correct |
86 |
Correct |
22 ms |
2568 KB |
Output is correct |
87 |
Correct |
20 ms |
2652 KB |
Output is correct |
88 |
Correct |
24 ms |
2916 KB |
Output is correct |
89 |
Correct |
19 ms |
2916 KB |
Output is correct |
90 |
Correct |
18 ms |
2816 KB |
Output is correct |
91 |
Correct |
17 ms |
2916 KB |
Output is correct |
92 |
Correct |
18 ms |
2404 KB |
Output is correct |
93 |
Correct |
47 ms |
15524 KB |
Output is correct |
94 |
Correct |
44 ms |
14536 KB |
Output is correct |
95 |
Correct |
58 ms |
15300 KB |
Output is correct |
96 |
Correct |
57 ms |
15300 KB |
Output is correct |
97 |
Correct |
44 ms |
15304 KB |
Output is correct |
98 |
Correct |
55 ms |
15304 KB |
Output is correct |
99 |
Correct |
38 ms |
14800 KB |
Output is correct |
100 |
Correct |
86 ms |
17348 KB |
Output is correct |
101 |
Correct |
83 ms |
17348 KB |
Output is correct |
102 |
Correct |
73 ms |
17456 KB |
Output is correct |
103 |
Correct |
70 ms |
16844 KB |
Output is correct |
104 |
Correct |
74 ms |
16840 KB |
Output is correct |
105 |
Correct |
70 ms |
16836 KB |
Output is correct |
106 |
Correct |
77 ms |
16576 KB |
Output is correct |
107 |
Correct |
87 ms |
16584 KB |
Output is correct |
108 |
Correct |
73 ms |
17352 KB |
Output is correct |
109 |
Correct |
79 ms |
17348 KB |
Output is correct |
110 |
Correct |
90 ms |
17092 KB |
Output is correct |
111 |
Correct |
89 ms |
17096 KB |
Output is correct |