#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
const int B = 1 << 17;
int n, m, k, q, s, t, a, b;
pair<int, int> seg[17][B * 2];
vector<int> v[2][NMAX];
multiset<int> ms;
pair<int, int> operator /(const pair<int, int>& a, const pair<int, int>& b){
return {min(a.first, b.first), max(a.second, b.second)};
}
void pull(int t){
for(int i = B - 1; i; i--) seg[t][i] = seg[t][i * 2] / seg[t][i * 2 + 1];
}
pair<int,int> qry(int t, int l, int r){
l += B; r += B;
pair<int, int> p = {n + 1, 0};
while(l <= r){
if(l & 1) p = p / seg[t][l++];
if(!(r & 1)) p = p / seg[t][r--];
l /= 2; r /= 2;
}
return p;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> m;
while(m--){
cin >> a >> b;
if(a < b){
v[0][a].emplace_back(b);
v[0][min(a + k, b)].emplace_back(-b);
}
else{
v[1][a].emplace_back(b);
v[1][max(a - k, b)].emplace_back(-b);
}
}
for(int i = 0; i < 17; i++)
for(int j = 1; j < B * 2; j++) seg[i][j] = {n + 1, 0};
for(int t = 0; t < 2; t++){
multiset<int> ms;
if(!t){
for(int i = 1; i <= n; i++){
for(int& x : v[t][i]){
if(x > 0) ms.emplace(x);
else ms.erase(ms.find(-x));
}
seg[0][B + i].second = (ms.empty() ? i : *ms.rbegin());
}
}
else{
for(int i = n; i; i--){
for(int& x : v[t][i]){
if(x > 0) ms.emplace(x);
else ms.erase(ms.find(-x));
}
seg[0][B + i].first = (ms.empty() ? i : *ms.begin());
}
}
}
pull(0);
for(int i = 1; i < 17; i++){
for(int j = 1; j <= n; j++)
seg[i][B + j] = qry(i - 1, seg[i - 1][B + j].first, seg[i - 1][B + j].second);
pull(i);
}
cin >> q;
while(q--){
cin >> s >> t;
int l = s, r = s, cur = 0;
for(int i = 16; i >= 0; i--){
auto [mn, mx] = qry(i, l, r);
if(t < mn || t > mx){
cur += 1 << i;
l = mn, r = mx;
}
}
if(++cur == (1 << 17)) cout << "-1\n";
else cout << cur << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:84:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | auto [mn, mx] = qry(i, l, r);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
39844 KB |
Output is correct |
2 |
Correct |
24 ms |
39884 KB |
Output is correct |
3 |
Correct |
24 ms |
39876 KB |
Output is correct |
4 |
Correct |
24 ms |
39852 KB |
Output is correct |
5 |
Correct |
24 ms |
39896 KB |
Output is correct |
6 |
Correct |
24 ms |
39856 KB |
Output is correct |
7 |
Correct |
23 ms |
39892 KB |
Output is correct |
8 |
Correct |
24 ms |
39884 KB |
Output is correct |
9 |
Correct |
24 ms |
39892 KB |
Output is correct |
10 |
Correct |
23 ms |
39892 KB |
Output is correct |
11 |
Correct |
24 ms |
39892 KB |
Output is correct |
12 |
Correct |
24 ms |
39784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
39844 KB |
Output is correct |
2 |
Correct |
24 ms |
39884 KB |
Output is correct |
3 |
Correct |
24 ms |
39876 KB |
Output is correct |
4 |
Correct |
24 ms |
39852 KB |
Output is correct |
5 |
Correct |
24 ms |
39896 KB |
Output is correct |
6 |
Correct |
24 ms |
39856 KB |
Output is correct |
7 |
Correct |
23 ms |
39892 KB |
Output is correct |
8 |
Correct |
24 ms |
39884 KB |
Output is correct |
9 |
Correct |
24 ms |
39892 KB |
Output is correct |
10 |
Correct |
23 ms |
39892 KB |
Output is correct |
11 |
Correct |
24 ms |
39892 KB |
Output is correct |
12 |
Correct |
24 ms |
39784 KB |
Output is correct |
13 |
Correct |
28 ms |
40004 KB |
Output is correct |
14 |
Correct |
26 ms |
39900 KB |
Output is correct |
15 |
Correct |
25 ms |
39904 KB |
Output is correct |
16 |
Correct |
28 ms |
39964 KB |
Output is correct |
17 |
Correct |
27 ms |
39964 KB |
Output is correct |
18 |
Correct |
25 ms |
39984 KB |
Output is correct |
19 |
Correct |
26 ms |
39976 KB |
Output is correct |
20 |
Correct |
26 ms |
40020 KB |
Output is correct |
21 |
Correct |
27 ms |
39944 KB |
Output is correct |
22 |
Correct |
26 ms |
40012 KB |
Output is correct |
23 |
Correct |
26 ms |
40016 KB |
Output is correct |
24 |
Correct |
33 ms |
39928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
43764 KB |
Output is correct |
2 |
Correct |
152 ms |
43712 KB |
Output is correct |
3 |
Correct |
200 ms |
44096 KB |
Output is correct |
4 |
Correct |
140 ms |
43496 KB |
Output is correct |
5 |
Correct |
185 ms |
48880 KB |
Output is correct |
6 |
Correct |
160 ms |
48456 KB |
Output is correct |
7 |
Correct |
197 ms |
53584 KB |
Output is correct |
8 |
Correct |
149 ms |
46032 KB |
Output is correct |
9 |
Correct |
151 ms |
46164 KB |
Output is correct |
10 |
Correct |
175 ms |
47792 KB |
Output is correct |
11 |
Correct |
179 ms |
47692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
47112 KB |
Output is correct |
2 |
Correct |
245 ms |
49856 KB |
Output is correct |
3 |
Correct |
226 ms |
44920 KB |
Output is correct |
4 |
Correct |
206 ms |
54276 KB |
Output is correct |
5 |
Correct |
211 ms |
50676 KB |
Output is correct |
6 |
Correct |
205 ms |
50596 KB |
Output is correct |
7 |
Correct |
215 ms |
48416 KB |
Output is correct |
8 |
Correct |
267 ms |
48584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
49320 KB |
Output is correct |
2 |
Correct |
252 ms |
44924 KB |
Output is correct |
3 |
Correct |
254 ms |
42756 KB |
Output is correct |
4 |
Correct |
225 ms |
41424 KB |
Output is correct |
5 |
Correct |
181 ms |
40948 KB |
Output is correct |
6 |
Correct |
203 ms |
40604 KB |
Output is correct |
7 |
Correct |
225 ms |
49952 KB |
Output is correct |
8 |
Correct |
24 ms |
39892 KB |
Output is correct |
9 |
Correct |
27 ms |
39904 KB |
Output is correct |
10 |
Correct |
286 ms |
49484 KB |
Output is correct |
11 |
Correct |
263 ms |
51640 KB |
Output is correct |
12 |
Correct |
280 ms |
49776 KB |
Output is correct |
13 |
Correct |
278 ms |
51516 KB |
Output is correct |
14 |
Correct |
23 ms |
39844 KB |
Output is correct |
15 |
Correct |
27 ms |
39892 KB |
Output is correct |
16 |
Correct |
203 ms |
46356 KB |
Output is correct |
17 |
Correct |
309 ms |
47140 KB |
Output is correct |
18 |
Correct |
297 ms |
47052 KB |
Output is correct |
19 |
Correct |
283 ms |
47096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
39844 KB |
Output is correct |
2 |
Correct |
24 ms |
39884 KB |
Output is correct |
3 |
Correct |
24 ms |
39876 KB |
Output is correct |
4 |
Correct |
24 ms |
39852 KB |
Output is correct |
5 |
Correct |
24 ms |
39896 KB |
Output is correct |
6 |
Correct |
24 ms |
39856 KB |
Output is correct |
7 |
Correct |
23 ms |
39892 KB |
Output is correct |
8 |
Correct |
24 ms |
39884 KB |
Output is correct |
9 |
Correct |
24 ms |
39892 KB |
Output is correct |
10 |
Correct |
23 ms |
39892 KB |
Output is correct |
11 |
Correct |
24 ms |
39892 KB |
Output is correct |
12 |
Correct |
24 ms |
39784 KB |
Output is correct |
13 |
Correct |
28 ms |
40004 KB |
Output is correct |
14 |
Correct |
26 ms |
39900 KB |
Output is correct |
15 |
Correct |
25 ms |
39904 KB |
Output is correct |
16 |
Correct |
28 ms |
39964 KB |
Output is correct |
17 |
Correct |
27 ms |
39964 KB |
Output is correct |
18 |
Correct |
25 ms |
39984 KB |
Output is correct |
19 |
Correct |
26 ms |
39976 KB |
Output is correct |
20 |
Correct |
26 ms |
40020 KB |
Output is correct |
21 |
Correct |
27 ms |
39944 KB |
Output is correct |
22 |
Correct |
26 ms |
40012 KB |
Output is correct |
23 |
Correct |
26 ms |
40016 KB |
Output is correct |
24 |
Correct |
33 ms |
39928 KB |
Output is correct |
25 |
Correct |
156 ms |
43764 KB |
Output is correct |
26 |
Correct |
152 ms |
43712 KB |
Output is correct |
27 |
Correct |
200 ms |
44096 KB |
Output is correct |
28 |
Correct |
140 ms |
43496 KB |
Output is correct |
29 |
Correct |
185 ms |
48880 KB |
Output is correct |
30 |
Correct |
160 ms |
48456 KB |
Output is correct |
31 |
Correct |
197 ms |
53584 KB |
Output is correct |
32 |
Correct |
149 ms |
46032 KB |
Output is correct |
33 |
Correct |
151 ms |
46164 KB |
Output is correct |
34 |
Correct |
175 ms |
47792 KB |
Output is correct |
35 |
Correct |
179 ms |
47692 KB |
Output is correct |
36 |
Correct |
197 ms |
47112 KB |
Output is correct |
37 |
Correct |
245 ms |
49856 KB |
Output is correct |
38 |
Correct |
226 ms |
44920 KB |
Output is correct |
39 |
Correct |
206 ms |
54276 KB |
Output is correct |
40 |
Correct |
211 ms |
50676 KB |
Output is correct |
41 |
Correct |
205 ms |
50596 KB |
Output is correct |
42 |
Correct |
215 ms |
48416 KB |
Output is correct |
43 |
Correct |
267 ms |
48584 KB |
Output is correct |
44 |
Correct |
365 ms |
49320 KB |
Output is correct |
45 |
Correct |
252 ms |
44924 KB |
Output is correct |
46 |
Correct |
254 ms |
42756 KB |
Output is correct |
47 |
Correct |
225 ms |
41424 KB |
Output is correct |
48 |
Correct |
181 ms |
40948 KB |
Output is correct |
49 |
Correct |
203 ms |
40604 KB |
Output is correct |
50 |
Correct |
225 ms |
49952 KB |
Output is correct |
51 |
Correct |
24 ms |
39892 KB |
Output is correct |
52 |
Correct |
27 ms |
39904 KB |
Output is correct |
53 |
Correct |
286 ms |
49484 KB |
Output is correct |
54 |
Correct |
263 ms |
51640 KB |
Output is correct |
55 |
Correct |
280 ms |
49776 KB |
Output is correct |
56 |
Correct |
278 ms |
51516 KB |
Output is correct |
57 |
Correct |
23 ms |
39844 KB |
Output is correct |
58 |
Correct |
27 ms |
39892 KB |
Output is correct |
59 |
Correct |
203 ms |
46356 KB |
Output is correct |
60 |
Correct |
309 ms |
47140 KB |
Output is correct |
61 |
Correct |
297 ms |
47052 KB |
Output is correct |
62 |
Correct |
283 ms |
47096 KB |
Output is correct |
63 |
Correct |
274 ms |
44328 KB |
Output is correct |
64 |
Correct |
288 ms |
44816 KB |
Output is correct |
65 |
Correct |
238 ms |
44412 KB |
Output is correct |
66 |
Correct |
221 ms |
49676 KB |
Output is correct |
67 |
Correct |
286 ms |
49240 KB |
Output is correct |
68 |
Correct |
206 ms |
48508 KB |
Output is correct |
69 |
Correct |
221 ms |
50636 KB |
Output is correct |
70 |
Correct |
249 ms |
48436 KB |
Output is correct |
71 |
Correct |
279 ms |
48504 KB |
Output is correct |