#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, m);
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, m, p[ind[i]].second, {p[ind[i]].first, ind[i]});
dp[0][ind[i]] = query(1, 1, m, 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 = 1;
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];
if (p[s].second > p[e].second || p[s].second < p[e].first)
cout << "impossible\n";
else
cout << ans + (p[s].second >= p[e].first) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
120 ms |
11180 KB |
Output is correct |
3 |
Correct |
112 ms |
11132 KB |
Output is correct |
4 |
Correct |
152 ms |
11168 KB |
Output is correct |
5 |
Correct |
125 ms |
11252 KB |
Output is correct |
6 |
Correct |
110 ms |
11212 KB |
Output is correct |
7 |
Correct |
119 ms |
11364 KB |
Output is correct |
8 |
Correct |
118 ms |
14796 KB |
Output is correct |
9 |
Correct |
133 ms |
14784 KB |
Output is correct |
10 |
Correct |
120 ms |
14568 KB |
Output is correct |
11 |
Correct |
160 ms |
14660 KB |
Output is correct |
12 |
Correct |
75 ms |
12992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
340 KB |
Output is correct |
19 |
Correct |
34 ms |
1368 KB |
Output is correct |
20 |
Correct |
27 ms |
1364 KB |
Output is correct |
21 |
Correct |
30 ms |
1608 KB |
Output is correct |
22 |
Correct |
28 ms |
2576 KB |
Output is correct |
23 |
Correct |
25 ms |
2516 KB |
Output is correct |
24 |
Correct |
23 ms |
2516 KB |
Output is correct |
25 |
Correct |
26 ms |
2148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
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 |
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 |
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 |
69 ms |
10480 KB |
Output is correct |
20 |
Correct |
66 ms |
10588 KB |
Output is correct |
21 |
Correct |
79 ms |
11428 KB |
Output is correct |
22 |
Correct |
73 ms |
12444 KB |
Output is correct |
23 |
Correct |
76 ms |
12488 KB |
Output is correct |
24 |
Correct |
76 ms |
12492 KB |
Output is correct |
25 |
Correct |
31 ms |
9752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
11132 KB |
Output is correct |
2 |
Correct |
111 ms |
11088 KB |
Output is correct |
3 |
Correct |
161 ms |
11128 KB |
Output is correct |
4 |
Correct |
144 ms |
11592 KB |
Output is correct |
5 |
Correct |
114 ms |
11660 KB |
Output is correct |
6 |
Correct |
119 ms |
11344 KB |
Output is correct |
7 |
Correct |
128 ms |
11224 KB |
Output is correct |
8 |
Correct |
116 ms |
11364 KB |
Output is correct |
9 |
Correct |
70 ms |
10580 KB |
Output is correct |
10 |
Correct |
110 ms |
10996 KB |
Output is correct |
11 |
Correct |
106 ms |
10756 KB |
Output is correct |
12 |
Correct |
110 ms |
10972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
120 ms |
11180 KB |
Output is correct |
3 |
Correct |
112 ms |
11132 KB |
Output is correct |
4 |
Correct |
152 ms |
11168 KB |
Output is correct |
5 |
Correct |
125 ms |
11252 KB |
Output is correct |
6 |
Correct |
110 ms |
11212 KB |
Output is correct |
7 |
Correct |
119 ms |
11364 KB |
Output is correct |
8 |
Correct |
118 ms |
14796 KB |
Output is correct |
9 |
Correct |
133 ms |
14784 KB |
Output is correct |
10 |
Correct |
120 ms |
14568 KB |
Output is correct |
11 |
Correct |
160 ms |
14660 KB |
Output is correct |
12 |
Correct |
75 ms |
12992 KB |
Output is correct |
13 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 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 |
340 KB |
Output is correct |
31 |
Correct |
34 ms |
1368 KB |
Output is correct |
32 |
Correct |
27 ms |
1364 KB |
Output is correct |
33 |
Correct |
30 ms |
1608 KB |
Output is correct |
34 |
Correct |
28 ms |
2576 KB |
Output is correct |
35 |
Correct |
25 ms |
2516 KB |
Output is correct |
36 |
Correct |
23 ms |
2516 KB |
Output is correct |
37 |
Correct |
26 ms |
2148 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 |
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 |
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 |
69 ms |
10480 KB |
Output is correct |
48 |
Correct |
66 ms |
10588 KB |
Output is correct |
49 |
Correct |
79 ms |
11428 KB |
Output is correct |
50 |
Correct |
73 ms |
12444 KB |
Output is correct |
51 |
Correct |
76 ms |
12488 KB |
Output is correct |
52 |
Correct |
76 ms |
12492 KB |
Output is correct |
53 |
Correct |
31 ms |
9752 KB |
Output is correct |
54 |
Correct |
100 ms |
11132 KB |
Output is correct |
55 |
Correct |
111 ms |
11088 KB |
Output is correct |
56 |
Correct |
161 ms |
11128 KB |
Output is correct |
57 |
Correct |
144 ms |
11592 KB |
Output is correct |
58 |
Correct |
114 ms |
11660 KB |
Output is correct |
59 |
Correct |
119 ms |
11344 KB |
Output is correct |
60 |
Correct |
128 ms |
11224 KB |
Output is correct |
61 |
Correct |
116 ms |
11364 KB |
Output is correct |
62 |
Correct |
70 ms |
10580 KB |
Output is correct |
63 |
Correct |
110 ms |
10996 KB |
Output is correct |
64 |
Correct |
106 ms |
10756 KB |
Output is correct |
65 |
Correct |
110 ms |
10972 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
103 ms |
14280 KB |
Output is correct |
68 |
Correct |
108 ms |
14340 KB |
Output is correct |
69 |
Correct |
156 ms |
14248 KB |
Output is correct |
70 |
Correct |
131 ms |
14336 KB |
Output is correct |
71 |
Correct |
116 ms |
14264 KB |
Output is correct |
72 |
Correct |
117 ms |
14304 KB |
Output is correct |
73 |
Correct |
130 ms |
14700 KB |
Output is correct |
74 |
Correct |
121 ms |
14740 KB |
Output is correct |
75 |
Correct |
124 ms |
14732 KB |
Output is correct |
76 |
Correct |
143 ms |
14684 KB |
Output is correct |
77 |
Correct |
71 ms |
13016 KB |
Output is correct |
78 |
Correct |
1 ms |
340 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 |
460 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 |
29 ms |
2260 KB |
Output is correct |
87 |
Correct |
32 ms |
2408 KB |
Output is correct |
88 |
Correct |
37 ms |
2520 KB |
Output is correct |
89 |
Correct |
25 ms |
2636 KB |
Output is correct |
90 |
Correct |
25 ms |
2488 KB |
Output is correct |
91 |
Correct |
25 ms |
2464 KB |
Output is correct |
92 |
Correct |
26 ms |
2088 KB |
Output is correct |
93 |
Correct |
82 ms |
12564 KB |
Output is correct |
94 |
Correct |
83 ms |
11628 KB |
Output is correct |
95 |
Correct |
86 ms |
11356 KB |
Output is correct |
96 |
Correct |
72 ms |
12544 KB |
Output is correct |
97 |
Correct |
70 ms |
12464 KB |
Output is correct |
98 |
Correct |
102 ms |
12412 KB |
Output is correct |
99 |
Correct |
28 ms |
9692 KB |
Output is correct |
100 |
Correct |
147 ms |
14388 KB |
Output is correct |
101 |
Correct |
121 ms |
14412 KB |
Output is correct |
102 |
Correct |
158 ms |
14528 KB |
Output is correct |
103 |
Correct |
118 ms |
14028 KB |
Output is correct |
104 |
Correct |
103 ms |
13760 KB |
Output is correct |
105 |
Correct |
112 ms |
14060 KB |
Output is correct |
106 |
Correct |
141 ms |
13704 KB |
Output is correct |
107 |
Correct |
157 ms |
13700 KB |
Output is correct |
108 |
Correct |
117 ms |
13376 KB |
Output is correct |
109 |
Correct |
114 ms |
13264 KB |
Output is correct |
110 |
Correct |
147 ms |
14308 KB |
Output is correct |
111 |
Correct |
157 ms |
14324 KB |
Output is correct |