#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <array>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define ar array
const int N = 1e5, N_ = 1 << 17, L = 17;
ar<int, 2> operator+(const ar<int, 2>& u, const ar<int, 2>& v) {
return {min(u[0], v[0]), max(u[1], v[1])};
}
int n_;
void build(ar<int, 2> *t, ar<int, 2> *p, int n) {
for (int i = 0; i < n_; i++)
t[n_ + i] = i < n ? p[i] : ar{0, 0};
for (int i = n_ - 1; i > 0; i--)
t[i] = t[i << 1 | 0] + t[i << 1 | 1];
}
ar<int, 2> query(ar<int, 2> *t, int l, int r) {
ar<int, 2> x = {N, 0};
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = x + t[l++];
if ((r & 1) == 0)
x = x + t[r--];
}
return x;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
int m;
scanf("%d", &m);
static vector<pair<int, int>> el[N + 1], er[N + 1];
while (m--) {
int i, j;
scanf("%d%d", &i, &j), i--, j--;
if (i < j) {
er[i].push_back({j, +1});
er[min(i + k, j)].push_back({j, -1});
} else {
el[max(i - k, j) + 1].push_back({j, +1});
el[i + 1].push_back({j, -1});
}
}
multiset<int> ls, rs;
static ar<int, 2> p[N];
for (int i = 0; i < n; i++) {
for (auto [x, t] : el[i])
if (t == +1)
ls.insert(x);
else
ls.erase(ls.find(x));
for (auto [x, t] : er[i])
if (t == +1)
rs.insert(x);
else
rs.erase(rs.find(x));
p[i][0] = ls.empty() ? i : *ls.begin();
p[i][1] = rs.empty() ? i : *rs.rbegin();
}
n_ = 1;
while (n_ < n)
n_ <<= 1;
static ar<int, 2> tb[L][N_ * 2];
build(tb[0], p, n);
for (int l = 1; l < L; l++) {
for (int i = 0; i < n; i++)
p[i] = query(tb[l - 1], p[i][0], p[i][1]);
build(tb[l], p, n);
}
int q;
scanf("%d", &q);
while (q--) {
int i, j;
scanf("%d%d", &i, &j), i--, j--;
if (i == j) {
printf("0\n");
continue;
}
int x = 1;
ar<int, 2> p = {i, i};
for (int l = L - 1; l >= 0; l--) {
ar<int, 2> q = query(tb[l], p[0], p[1]);
if (q[0] > j || q[1] < j)
p = q, x += 1 << l;
}
printf("%d\n", x == (1 << L) ? -1 : x);
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
Main.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d", &i, &j), i--, j--;
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d%d", &i, &j), i--, j--;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5276 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5276 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
7 ms |
5716 KB |
Output is correct |
14 |
Correct |
6 ms |
5760 KB |
Output is correct |
15 |
Correct |
4 ms |
5716 KB |
Output is correct |
16 |
Correct |
6 ms |
5716 KB |
Output is correct |
17 |
Correct |
5 ms |
5716 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5716 KB |
Output is correct |
20 |
Correct |
6 ms |
5716 KB |
Output is correct |
21 |
Correct |
5 ms |
5800 KB |
Output is correct |
22 |
Correct |
7 ms |
5748 KB |
Output is correct |
23 |
Correct |
5 ms |
5716 KB |
Output is correct |
24 |
Correct |
6 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
43556 KB |
Output is correct |
2 |
Correct |
132 ms |
44592 KB |
Output is correct |
3 |
Correct |
199 ms |
44844 KB |
Output is correct |
4 |
Correct |
118 ms |
44360 KB |
Output is correct |
5 |
Correct |
216 ms |
52276 KB |
Output is correct |
6 |
Correct |
168 ms |
49220 KB |
Output is correct |
7 |
Correct |
200 ms |
55196 KB |
Output is correct |
8 |
Correct |
147 ms |
47948 KB |
Output is correct |
9 |
Correct |
141 ms |
48344 KB |
Output is correct |
10 |
Correct |
212 ms |
50040 KB |
Output is correct |
11 |
Correct |
198 ms |
49892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
47692 KB |
Output is correct |
2 |
Correct |
325 ms |
53040 KB |
Output is correct |
3 |
Correct |
259 ms |
45640 KB |
Output is correct |
4 |
Correct |
193 ms |
55728 KB |
Output is correct |
5 |
Correct |
263 ms |
55244 KB |
Output is correct |
6 |
Correct |
279 ms |
55080 KB |
Output is correct |
7 |
Correct |
237 ms |
50640 KB |
Output is correct |
8 |
Correct |
310 ms |
50676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
49088 KB |
Output is correct |
2 |
Correct |
284 ms |
45696 KB |
Output is correct |
3 |
Correct |
212 ms |
43596 KB |
Output is correct |
4 |
Correct |
207 ms |
42192 KB |
Output is correct |
5 |
Correct |
136 ms |
41704 KB |
Output is correct |
6 |
Correct |
173 ms |
41396 KB |
Output is correct |
7 |
Correct |
184 ms |
51048 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
7 ms |
5844 KB |
Output is correct |
10 |
Correct |
293 ms |
52304 KB |
Output is correct |
11 |
Correct |
284 ms |
54220 KB |
Output is correct |
12 |
Correct |
309 ms |
52384 KB |
Output is correct |
13 |
Correct |
327 ms |
54148 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
7 ms |
5716 KB |
Output is correct |
16 |
Correct |
202 ms |
49300 KB |
Output is correct |
17 |
Correct |
342 ms |
49912 KB |
Output is correct |
18 |
Correct |
303 ms |
50064 KB |
Output is correct |
19 |
Correct |
304 ms |
49812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5276 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
7 ms |
5716 KB |
Output is correct |
14 |
Correct |
6 ms |
5760 KB |
Output is correct |
15 |
Correct |
4 ms |
5716 KB |
Output is correct |
16 |
Correct |
6 ms |
5716 KB |
Output is correct |
17 |
Correct |
5 ms |
5716 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5716 KB |
Output is correct |
20 |
Correct |
6 ms |
5716 KB |
Output is correct |
21 |
Correct |
5 ms |
5800 KB |
Output is correct |
22 |
Correct |
7 ms |
5748 KB |
Output is correct |
23 |
Correct |
5 ms |
5716 KB |
Output is correct |
24 |
Correct |
6 ms |
5716 KB |
Output is correct |
25 |
Correct |
136 ms |
43556 KB |
Output is correct |
26 |
Correct |
132 ms |
44592 KB |
Output is correct |
27 |
Correct |
199 ms |
44844 KB |
Output is correct |
28 |
Correct |
118 ms |
44360 KB |
Output is correct |
29 |
Correct |
216 ms |
52276 KB |
Output is correct |
30 |
Correct |
168 ms |
49220 KB |
Output is correct |
31 |
Correct |
200 ms |
55196 KB |
Output is correct |
32 |
Correct |
147 ms |
47948 KB |
Output is correct |
33 |
Correct |
141 ms |
48344 KB |
Output is correct |
34 |
Correct |
212 ms |
50040 KB |
Output is correct |
35 |
Correct |
198 ms |
49892 KB |
Output is correct |
36 |
Correct |
173 ms |
47692 KB |
Output is correct |
37 |
Correct |
325 ms |
53040 KB |
Output is correct |
38 |
Correct |
259 ms |
45640 KB |
Output is correct |
39 |
Correct |
193 ms |
55728 KB |
Output is correct |
40 |
Correct |
263 ms |
55244 KB |
Output is correct |
41 |
Correct |
279 ms |
55080 KB |
Output is correct |
42 |
Correct |
237 ms |
50640 KB |
Output is correct |
43 |
Correct |
310 ms |
50676 KB |
Output is correct |
44 |
Correct |
385 ms |
49088 KB |
Output is correct |
45 |
Correct |
284 ms |
45696 KB |
Output is correct |
46 |
Correct |
212 ms |
43596 KB |
Output is correct |
47 |
Correct |
207 ms |
42192 KB |
Output is correct |
48 |
Correct |
136 ms |
41704 KB |
Output is correct |
49 |
Correct |
173 ms |
41396 KB |
Output is correct |
50 |
Correct |
184 ms |
51048 KB |
Output is correct |
51 |
Correct |
5 ms |
5204 KB |
Output is correct |
52 |
Correct |
7 ms |
5844 KB |
Output is correct |
53 |
Correct |
293 ms |
52304 KB |
Output is correct |
54 |
Correct |
284 ms |
54220 KB |
Output is correct |
55 |
Correct |
309 ms |
52384 KB |
Output is correct |
56 |
Correct |
327 ms |
54148 KB |
Output is correct |
57 |
Correct |
3 ms |
5204 KB |
Output is correct |
58 |
Correct |
7 ms |
5716 KB |
Output is correct |
59 |
Correct |
202 ms |
49300 KB |
Output is correct |
60 |
Correct |
342 ms |
49912 KB |
Output is correct |
61 |
Correct |
303 ms |
50064 KB |
Output is correct |
62 |
Correct |
304 ms |
49812 KB |
Output is correct |
63 |
Correct |
225 ms |
45004 KB |
Output is correct |
64 |
Correct |
317 ms |
45648 KB |
Output is correct |
65 |
Correct |
236 ms |
45124 KB |
Output is correct |
66 |
Correct |
293 ms |
53036 KB |
Output is correct |
67 |
Correct |
266 ms |
49876 KB |
Output is correct |
68 |
Correct |
244 ms |
50880 KB |
Output is correct |
69 |
Correct |
282 ms |
55116 KB |
Output is correct |
70 |
Correct |
269 ms |
50680 KB |
Output is correct |
71 |
Correct |
323 ms |
50728 KB |
Output is correct |