#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:33:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
Main.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &i, &j), i--, j--;
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d", &i, &j), i--, j--;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
40028 KB |
Output is correct |
2 |
Correct |
7 ms |
40024 KB |
Output is correct |
3 |
Correct |
7 ms |
40256 KB |
Output is correct |
4 |
Correct |
7 ms |
40028 KB |
Output is correct |
5 |
Correct |
8 ms |
40028 KB |
Output is correct |
6 |
Correct |
7 ms |
40028 KB |
Output is correct |
7 |
Correct |
7 ms |
40028 KB |
Output is correct |
8 |
Correct |
6 ms |
40024 KB |
Output is correct |
9 |
Correct |
7 ms |
40004 KB |
Output is correct |
10 |
Correct |
6 ms |
39892 KB |
Output is correct |
11 |
Correct |
7 ms |
40028 KB |
Output is correct |
12 |
Correct |
8 ms |
39864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
40028 KB |
Output is correct |
2 |
Correct |
7 ms |
40024 KB |
Output is correct |
3 |
Correct |
7 ms |
40256 KB |
Output is correct |
4 |
Correct |
7 ms |
40028 KB |
Output is correct |
5 |
Correct |
8 ms |
40028 KB |
Output is correct |
6 |
Correct |
7 ms |
40028 KB |
Output is correct |
7 |
Correct |
7 ms |
40028 KB |
Output is correct |
8 |
Correct |
6 ms |
40024 KB |
Output is correct |
9 |
Correct |
7 ms |
40004 KB |
Output is correct |
10 |
Correct |
6 ms |
39892 KB |
Output is correct |
11 |
Correct |
7 ms |
40028 KB |
Output is correct |
12 |
Correct |
8 ms |
39864 KB |
Output is correct |
13 |
Correct |
10 ms |
40028 KB |
Output is correct |
14 |
Correct |
9 ms |
40152 KB |
Output is correct |
15 |
Correct |
8 ms |
40108 KB |
Output is correct |
16 |
Correct |
10 ms |
40028 KB |
Output is correct |
17 |
Correct |
10 ms |
40212 KB |
Output is correct |
18 |
Correct |
9 ms |
40220 KB |
Output is correct |
19 |
Correct |
10 ms |
40028 KB |
Output is correct |
20 |
Correct |
8 ms |
40164 KB |
Output is correct |
21 |
Correct |
8 ms |
40052 KB |
Output is correct |
22 |
Correct |
10 ms |
40140 KB |
Output is correct |
23 |
Correct |
9 ms |
40068 KB |
Output is correct |
24 |
Correct |
9 ms |
40024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
44632 KB |
Output is correct |
2 |
Correct |
123 ms |
44572 KB |
Output is correct |
3 |
Correct |
154 ms |
44960 KB |
Output is correct |
4 |
Correct |
116 ms |
44372 KB |
Output is correct |
5 |
Correct |
207 ms |
52416 KB |
Output is correct |
6 |
Correct |
169 ms |
49352 KB |
Output is correct |
7 |
Correct |
145 ms |
55324 KB |
Output is correct |
8 |
Correct |
152 ms |
48204 KB |
Output is correct |
9 |
Correct |
119 ms |
48476 KB |
Output is correct |
10 |
Correct |
175 ms |
50132 KB |
Output is correct |
11 |
Correct |
174 ms |
50104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
49400 KB |
Output is correct |
2 |
Correct |
226 ms |
52920 KB |
Output is correct |
3 |
Correct |
207 ms |
45648 KB |
Output is correct |
4 |
Correct |
164 ms |
55948 KB |
Output is correct |
5 |
Correct |
217 ms |
55280 KB |
Output is correct |
6 |
Correct |
207 ms |
55280 KB |
Output is correct |
7 |
Correct |
217 ms |
50940 KB |
Output is correct |
8 |
Correct |
227 ms |
50756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
51980 KB |
Output is correct |
2 |
Correct |
236 ms |
45892 KB |
Output is correct |
3 |
Correct |
201 ms |
43556 KB |
Output is correct |
4 |
Correct |
218 ms |
42272 KB |
Output is correct |
5 |
Correct |
130 ms |
41968 KB |
Output is correct |
6 |
Correct |
175 ms |
41356 KB |
Output is correct |
7 |
Correct |
182 ms |
51016 KB |
Output is correct |
8 |
Correct |
10 ms |
40028 KB |
Output is correct |
9 |
Correct |
10 ms |
40028 KB |
Output is correct |
10 |
Correct |
254 ms |
52404 KB |
Output is correct |
11 |
Correct |
266 ms |
54356 KB |
Output is correct |
12 |
Correct |
285 ms |
52568 KB |
Output is correct |
13 |
Correct |
281 ms |
54256 KB |
Output is correct |
14 |
Correct |
7 ms |
39868 KB |
Output is correct |
15 |
Correct |
10 ms |
40168 KB |
Output is correct |
16 |
Correct |
185 ms |
49352 KB |
Output is correct |
17 |
Correct |
283 ms |
49972 KB |
Output is correct |
18 |
Correct |
298 ms |
50332 KB |
Output is correct |
19 |
Correct |
272 ms |
50084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
40028 KB |
Output is correct |
2 |
Correct |
7 ms |
40024 KB |
Output is correct |
3 |
Correct |
7 ms |
40256 KB |
Output is correct |
4 |
Correct |
7 ms |
40028 KB |
Output is correct |
5 |
Correct |
8 ms |
40028 KB |
Output is correct |
6 |
Correct |
7 ms |
40028 KB |
Output is correct |
7 |
Correct |
7 ms |
40028 KB |
Output is correct |
8 |
Correct |
6 ms |
40024 KB |
Output is correct |
9 |
Correct |
7 ms |
40004 KB |
Output is correct |
10 |
Correct |
6 ms |
39892 KB |
Output is correct |
11 |
Correct |
7 ms |
40028 KB |
Output is correct |
12 |
Correct |
8 ms |
39864 KB |
Output is correct |
13 |
Correct |
10 ms |
40028 KB |
Output is correct |
14 |
Correct |
9 ms |
40152 KB |
Output is correct |
15 |
Correct |
8 ms |
40108 KB |
Output is correct |
16 |
Correct |
10 ms |
40028 KB |
Output is correct |
17 |
Correct |
10 ms |
40212 KB |
Output is correct |
18 |
Correct |
9 ms |
40220 KB |
Output is correct |
19 |
Correct |
10 ms |
40028 KB |
Output is correct |
20 |
Correct |
8 ms |
40164 KB |
Output is correct |
21 |
Correct |
8 ms |
40052 KB |
Output is correct |
22 |
Correct |
10 ms |
40140 KB |
Output is correct |
23 |
Correct |
9 ms |
40068 KB |
Output is correct |
24 |
Correct |
9 ms |
40024 KB |
Output is correct |
25 |
Correct |
132 ms |
44632 KB |
Output is correct |
26 |
Correct |
123 ms |
44572 KB |
Output is correct |
27 |
Correct |
154 ms |
44960 KB |
Output is correct |
28 |
Correct |
116 ms |
44372 KB |
Output is correct |
29 |
Correct |
207 ms |
52416 KB |
Output is correct |
30 |
Correct |
169 ms |
49352 KB |
Output is correct |
31 |
Correct |
145 ms |
55324 KB |
Output is correct |
32 |
Correct |
152 ms |
48204 KB |
Output is correct |
33 |
Correct |
119 ms |
48476 KB |
Output is correct |
34 |
Correct |
175 ms |
50132 KB |
Output is correct |
35 |
Correct |
174 ms |
50104 KB |
Output is correct |
36 |
Correct |
148 ms |
49400 KB |
Output is correct |
37 |
Correct |
226 ms |
52920 KB |
Output is correct |
38 |
Correct |
207 ms |
45648 KB |
Output is correct |
39 |
Correct |
164 ms |
55948 KB |
Output is correct |
40 |
Correct |
217 ms |
55280 KB |
Output is correct |
41 |
Correct |
207 ms |
55280 KB |
Output is correct |
42 |
Correct |
217 ms |
50940 KB |
Output is correct |
43 |
Correct |
227 ms |
50756 KB |
Output is correct |
44 |
Correct |
338 ms |
51980 KB |
Output is correct |
45 |
Correct |
236 ms |
45892 KB |
Output is correct |
46 |
Correct |
201 ms |
43556 KB |
Output is correct |
47 |
Correct |
218 ms |
42272 KB |
Output is correct |
48 |
Correct |
130 ms |
41968 KB |
Output is correct |
49 |
Correct |
175 ms |
41356 KB |
Output is correct |
50 |
Correct |
182 ms |
51016 KB |
Output is correct |
51 |
Correct |
10 ms |
40028 KB |
Output is correct |
52 |
Correct |
10 ms |
40028 KB |
Output is correct |
53 |
Correct |
254 ms |
52404 KB |
Output is correct |
54 |
Correct |
266 ms |
54356 KB |
Output is correct |
55 |
Correct |
285 ms |
52568 KB |
Output is correct |
56 |
Correct |
281 ms |
54256 KB |
Output is correct |
57 |
Correct |
7 ms |
39868 KB |
Output is correct |
58 |
Correct |
10 ms |
40168 KB |
Output is correct |
59 |
Correct |
185 ms |
49352 KB |
Output is correct |
60 |
Correct |
283 ms |
49972 KB |
Output is correct |
61 |
Correct |
298 ms |
50332 KB |
Output is correct |
62 |
Correct |
272 ms |
50084 KB |
Output is correct |
63 |
Correct |
224 ms |
45144 KB |
Output is correct |
64 |
Correct |
278 ms |
45520 KB |
Output is correct |
65 |
Correct |
244 ms |
45312 KB |
Output is correct |
66 |
Correct |
255 ms |
53076 KB |
Output is correct |
67 |
Correct |
256 ms |
50000 KB |
Output is correct |
68 |
Correct |
221 ms |
51004 KB |
Output is correct |
69 |
Correct |
230 ms |
55092 KB |
Output is correct |
70 |
Correct |
235 ms |
50644 KB |
Output is correct |
71 |
Correct |
301 ms |
50864 KB |
Output is correct |