#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, q, E[100002], dp[20][100002], ind[100002];
pair<int, int>p[100002];
int aint[400002], poz[400002];
void build (int nod, int st, int dr) {
aint[nod] = INF;
poz[nod] = st;
if (st < dr) {
build(2 * nod, st, (st + dr) / 2);
build(2 * nod + 1, (st + dr) / 2 + 1, dr);
}
}
void update (int nod, int st, int dr, int x, pair<int, int> val) {
if (st == dr) {
if (aint[nod] > val.first) {
aint[nod] = val.first;
poz[nod] = val.second;
}
return;
}
int mid = (st + dr) / 2;
if (x <= mid)
update(2 * nod, st, mid, x, val);
else
update(2 * nod + 1, mid + 1, dr, x, val);
aint[nod] = min(aint[2 * nod], aint[2 * nod + 1]);
if (aint[2 * nod] == aint[nod])
poz[nod] = poz[2 * nod];
else
poz[nod] = poz[2 * nod + 1];
}
pair<int, int> query (int nod, int st, int dr, int a, int b) {
if (a <= st && dr <= b)
return {aint[nod], poz[nod]};
int mid = (st + dr) / 2;
if (b <= mid)
return query(2 * nod, st, mid, a, b);
else if (a > mid)
return query(2 * nod + 1, mid + 1, dr, a, b);
else {
auto p1 = query(2 * nod, st, mid, a, b);
auto p2 = query(2 * nod + 1, mid + 1, dr, a, b);
return min(p1, p2);
}
}
bool cmp (int a, int b) {
if (p[a].second == p[b].second)
return p[a].first < p[b].first;
return p[a].second < p[b].second;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> p[i].first >> p[i].second;
E[i] = p[i].second;
}
sort(E + 1, E + n + 1);
int m = 0;
for (int i = 1; i <= n; i++) {
ind[i] = i;
if (i == 1 || E[i] != E[i - 1])
E[++m] = E[i];
}
sort(ind + 1, ind + n + 1, cmp);
build(1, 1, n);
for (int i = 1; i <= n; i++) {
p[ind[i]].second = lower_bound(E + 1, E + m + 1, p[ind[i]].second) - E;
p[ind[i]].first = lower_bound(E + 1, E + m + 1, p[ind[i]].first) - E;
update(1, 1, n, p[ind[i]].second, {p[ind[i]].first, ind[i]});
dp[0][ind[i]] = query(1, 1, n, p[ind[i]].first, p[ind[i]].second).second;
}
int lim = 0;
while ((1 << lim) <= n)
lim++;
lim--;
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n; j++)
dp[i][j] = dp[i - 1][dp[i - 1][j]];
while (q--) {
int s, e;
cin >> s >> e;
if (p[e].second < p[s].second) {
cout << "impossible\n";
continue;
}
if (p[s].second >= p[e].first) {
cout<< (s != e) << "\n";
continue;
}
int ans = 0;
for (int i = lim; i >= 0; i--) {
if (p[s].second < p[dp[i][e]].first) {
e = dp[i][e];
ans += (1 << i);
}
}
e = dp[0][e];
ans++;
if (p[e].first <= p[s].second)
cout << ans + (s != e) << "\n";
else
cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
93 ms |
11084 KB |
Output is correct |
3 |
Correct |
107 ms |
11156 KB |
Output is correct |
4 |
Correct |
173 ms |
11180 KB |
Output is correct |
5 |
Correct |
118 ms |
11340 KB |
Output is correct |
6 |
Correct |
127 ms |
11240 KB |
Output is correct |
7 |
Correct |
141 ms |
11280 KB |
Output is correct |
8 |
Correct |
120 ms |
11632 KB |
Output is correct |
9 |
Correct |
126 ms |
11640 KB |
Output is correct |
10 |
Correct |
129 ms |
11548 KB |
Output is correct |
11 |
Correct |
126 ms |
11596 KB |
Output is correct |
12 |
Correct |
72 ms |
11660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
34 ms |
1300 KB |
Output is correct |
20 |
Correct |
35 ms |
1364 KB |
Output is correct |
21 |
Correct |
31 ms |
1528 KB |
Output is correct |
22 |
Correct |
27 ms |
1596 KB |
Output is correct |
23 |
Correct |
26 ms |
1524 KB |
Output is correct |
24 |
Correct |
23 ms |
1416 KB |
Output is correct |
25 |
Correct |
31 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 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 |
79 ms |
10576 KB |
Output is correct |
20 |
Correct |
73 ms |
10564 KB |
Output is correct |
21 |
Correct |
87 ms |
10596 KB |
Output is correct |
22 |
Correct |
73 ms |
10580 KB |
Output is correct |
23 |
Correct |
74 ms |
10592 KB |
Output is correct |
24 |
Correct |
81 ms |
10476 KB |
Output is correct |
25 |
Correct |
44 ms |
10564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
11200 KB |
Output is correct |
2 |
Correct |
120 ms |
11172 KB |
Output is correct |
3 |
Correct |
168 ms |
11120 KB |
Output is correct |
4 |
Correct |
120 ms |
11708 KB |
Output is correct |
5 |
Correct |
128 ms |
11508 KB |
Output is correct |
6 |
Correct |
129 ms |
11248 KB |
Output is correct |
7 |
Correct |
125 ms |
11304 KB |
Output is correct |
8 |
Correct |
119 ms |
11372 KB |
Output is correct |
9 |
Correct |
72 ms |
10548 KB |
Output is correct |
10 |
Correct |
111 ms |
10964 KB |
Output is correct |
11 |
Correct |
129 ms |
10692 KB |
Output is correct |
12 |
Correct |
122 ms |
10960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
93 ms |
11084 KB |
Output is correct |
3 |
Correct |
107 ms |
11156 KB |
Output is correct |
4 |
Correct |
173 ms |
11180 KB |
Output is correct |
5 |
Correct |
118 ms |
11340 KB |
Output is correct |
6 |
Correct |
127 ms |
11240 KB |
Output is correct |
7 |
Correct |
141 ms |
11280 KB |
Output is correct |
8 |
Correct |
120 ms |
11632 KB |
Output is correct |
9 |
Correct |
126 ms |
11640 KB |
Output is correct |
10 |
Correct |
129 ms |
11548 KB |
Output is correct |
11 |
Correct |
126 ms |
11596 KB |
Output is correct |
12 |
Correct |
72 ms |
11660 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 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 |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 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 |
34 ms |
1300 KB |
Output is correct |
32 |
Correct |
35 ms |
1364 KB |
Output is correct |
33 |
Correct |
31 ms |
1528 KB |
Output is correct |
34 |
Correct |
27 ms |
1596 KB |
Output is correct |
35 |
Correct |
26 ms |
1524 KB |
Output is correct |
36 |
Correct |
23 ms |
1416 KB |
Output is correct |
37 |
Correct |
31 ms |
1212 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
79 ms |
10576 KB |
Output is correct |
48 |
Correct |
73 ms |
10564 KB |
Output is correct |
49 |
Correct |
87 ms |
10596 KB |
Output is correct |
50 |
Correct |
73 ms |
10580 KB |
Output is correct |
51 |
Correct |
74 ms |
10592 KB |
Output is correct |
52 |
Correct |
81 ms |
10476 KB |
Output is correct |
53 |
Correct |
44 ms |
10564 KB |
Output is correct |
54 |
Correct |
104 ms |
11200 KB |
Output is correct |
55 |
Correct |
120 ms |
11172 KB |
Output is correct |
56 |
Correct |
168 ms |
11120 KB |
Output is correct |
57 |
Correct |
120 ms |
11708 KB |
Output is correct |
58 |
Correct |
128 ms |
11508 KB |
Output is correct |
59 |
Correct |
129 ms |
11248 KB |
Output is correct |
60 |
Correct |
125 ms |
11304 KB |
Output is correct |
61 |
Correct |
119 ms |
11372 KB |
Output is correct |
62 |
Correct |
72 ms |
10548 KB |
Output is correct |
63 |
Correct |
111 ms |
10964 KB |
Output is correct |
64 |
Correct |
129 ms |
10692 KB |
Output is correct |
65 |
Correct |
122 ms |
10960 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
110 ms |
11120 KB |
Output is correct |
68 |
Correct |
113 ms |
11104 KB |
Output is correct |
69 |
Correct |
150 ms |
11104 KB |
Output is correct |
70 |
Correct |
117 ms |
11152 KB |
Output is correct |
71 |
Correct |
113 ms |
11224 KB |
Output is correct |
72 |
Correct |
124 ms |
11244 KB |
Output is correct |
73 |
Correct |
124 ms |
11596 KB |
Output is correct |
74 |
Correct |
124 ms |
11572 KB |
Output is correct |
75 |
Correct |
116 ms |
11448 KB |
Output is correct |
76 |
Correct |
115 ms |
11504 KB |
Output is correct |
77 |
Correct |
81 ms |
11676 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
2 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 |
31 ms |
1304 KB |
Output is correct |
87 |
Correct |
31 ms |
1272 KB |
Output is correct |
88 |
Correct |
33 ms |
1612 KB |
Output is correct |
89 |
Correct |
27 ms |
1660 KB |
Output is correct |
90 |
Correct |
25 ms |
1444 KB |
Output is correct |
91 |
Correct |
27 ms |
1528 KB |
Output is correct |
92 |
Correct |
29 ms |
1108 KB |
Output is correct |
93 |
Correct |
70 ms |
10520 KB |
Output is correct |
94 |
Correct |
69 ms |
10592 KB |
Output is correct |
95 |
Correct |
81 ms |
10484 KB |
Output is correct |
96 |
Correct |
72 ms |
10580 KB |
Output is correct |
97 |
Correct |
72 ms |
10484 KB |
Output is correct |
98 |
Correct |
81 ms |
10572 KB |
Output is correct |
99 |
Correct |
45 ms |
10572 KB |
Output is correct |
100 |
Correct |
139 ms |
11272 KB |
Output is correct |
101 |
Correct |
124 ms |
11244 KB |
Output is correct |
102 |
Correct |
119 ms |
11484 KB |
Output is correct |
103 |
Correct |
119 ms |
10988 KB |
Output is correct |
104 |
Correct |
116 ms |
10732 KB |
Output is correct |
105 |
Correct |
117 ms |
10960 KB |
Output is correct |
106 |
Correct |
120 ms |
11264 KB |
Output is correct |
107 |
Correct |
124 ms |
11368 KB |
Output is correct |
108 |
Correct |
132 ms |
11376 KB |
Output is correct |
109 |
Correct |
119 ms |
11340 KB |
Output is correct |
110 |
Correct |
134 ms |
11296 KB |
Output is correct |
111 |
Correct |
154 ms |
11244 KB |
Output is correct |