#include<bits/stdc++.h>
using namespace std;
const int base = 131072;
const int max_long = 20;
pair<int, int> jump_tree[max_long][base*2];
int train[base];
int train_reverse[base];
pair<int, int> get_array(int a, int b, int log, int v = 1, int l = 0, int r = base - 1){
if(a <= l and b >= r){
return jump_tree[log][v];
}
if(a > r or b < l){
return {1e9, 0};
}
int mid = (l+r)/2;
pair<int, int> first = get_array(a, b, log, v * 2, l, mid);
pair<int, int> second = get_array(a, b, log, v * 2 + 1, mid + 1, r);
pair<int, int> result;
result.first = min(first.first, second.first);
result.second = max(first.second, second.second);
return result;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, m, q;
cin >> n >> k >> m;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
if(a < b){
train[a] = max(train[a], b);
}else{
train_reverse[a] = min(train_reverse[a], b);
if(train_reverse[a] == 0){
train_reverse[a] = b;
}
}
}
vector<pair<int, int>> actual_trains;
int wsk = 0;
for(int i = 0; i < 2 * base; i++){
jump_tree[0][i] = {1e9, 0};
}
for(int i = 0; i <= n; i++){
int c = train[i];
while(!actual_trains.empty()){
if(actual_trains[actual_trains.size()-1].first < c){
actual_trains.pop_back();
}else{
break;
}
}
if(c != 0){
actual_trains.push_back({c, min(i + k - 1, c)});
}
if(actual_trains.empty()){
jump_tree[0][base + i].second = i;
}else{
if(wsk >= actual_trains.size()){
wsk = actual_trains.size()-1;
}
while(actual_trains[wsk].second < i){
wsk++;
if(wsk == actual_trains.size()){
wsk = 0;
actual_trains.clear();
break;
}
}
if(!actual_trains.empty()){
jump_tree[0][base + i].second = actual_trains[wsk].first;
}else{
jump_tree[0][base + i].second = i;
}
}
}
actual_trains.clear();
for(int i = n; i > 0; i--){
int c = train_reverse[i];
while(!actual_trains.empty()){
if(actual_trains[actual_trains.size()-1].first > c and c != 0){
actual_trains.pop_back();
}else{
break;
}
}
if(c != 0){
actual_trains.push_back({c, max(i - k + 1, c)});
}
if(actual_trains.empty()){
jump_tree[0][base + i].first = i;
}else{
if(wsk >= actual_trains.size()){
wsk = actual_trains.size()-1;
}
while(actual_trains[wsk].second > i){
wsk++;
if(wsk == actual_trains.size()){
wsk = 0;
actual_trains.clear();
break;
}
}
if(!actual_trains.empty()){
jump_tree[0][base + i].first = actual_trains[wsk].first;
}else{
jump_tree[0][base + i].first = i;
}
}
}
for(int i = base - 1; i > 0; i--){
jump_tree[0][i].first = min(jump_tree[0][2*i].first, jump_tree[0][2*i+1].first);
jump_tree[0][i].second = max(jump_tree[0][2*i].second, jump_tree[0][2*i+1].second);
}
for(int i = 1; i < max_long; i++){
for(int j = 1; j <= base + n; j++){
jump_tree[i][j] = get_array(jump_tree[i-1][j].first, jump_tree[i-1][j].second, i - 1);
}
}
cin >> q;
for(int i = 0; i < q; i++){
int begin, end;
cin >> begin >> end;
int result = 0;
int number = (1<<(max_long-1));
pair<int, int> range = {begin, begin};
for(int j = max_long - 1; j >= 0; j--){
pair <int, int> new_range = get_array(range.first, range.second, j);
if(new_range.first > end or new_range.second < end){
result += number;
range = new_range;
}
number /= 2;
}
pair <int, int> new_range_2 = get_array(range.first, range.second, 0);
if(new_range_2.first > end or new_range_2.second < end){
result = -1;
}else{
result++;
}
cout << result << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:66:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(wsk >= actual_trains.size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if(wsk == actual_trains.size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:101:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | if(wsk >= actual_trains.size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:106:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | if(wsk == actual_trains.size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
40536 KB |
Output is correct |
2 |
Correct |
14 ms |
40788 KB |
Output is correct |
3 |
Correct |
14 ms |
40616 KB |
Output is correct |
4 |
Correct |
17 ms |
40536 KB |
Output is correct |
5 |
Correct |
15 ms |
40540 KB |
Output is correct |
6 |
Correct |
15 ms |
40540 KB |
Output is correct |
7 |
Correct |
16 ms |
40540 KB |
Output is correct |
8 |
Correct |
14 ms |
40600 KB |
Output is correct |
9 |
Correct |
14 ms |
40540 KB |
Output is correct |
10 |
Correct |
12 ms |
41480 KB |
Output is correct |
11 |
Correct |
14 ms |
40540 KB |
Output is correct |
12 |
Correct |
15 ms |
40540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
40536 KB |
Output is correct |
2 |
Correct |
14 ms |
40788 KB |
Output is correct |
3 |
Correct |
14 ms |
40616 KB |
Output is correct |
4 |
Correct |
17 ms |
40536 KB |
Output is correct |
5 |
Correct |
15 ms |
40540 KB |
Output is correct |
6 |
Correct |
15 ms |
40540 KB |
Output is correct |
7 |
Correct |
16 ms |
40540 KB |
Output is correct |
8 |
Correct |
14 ms |
40600 KB |
Output is correct |
9 |
Correct |
14 ms |
40540 KB |
Output is correct |
10 |
Correct |
12 ms |
41480 KB |
Output is correct |
11 |
Correct |
14 ms |
40540 KB |
Output is correct |
12 |
Correct |
15 ms |
40540 KB |
Output is correct |
13 |
Correct |
33 ms |
40528 KB |
Output is correct |
14 |
Correct |
31 ms |
40544 KB |
Output is correct |
15 |
Correct |
26 ms |
40432 KB |
Output is correct |
16 |
Correct |
33 ms |
40536 KB |
Output is correct |
17 |
Correct |
31 ms |
40532 KB |
Output is correct |
18 |
Correct |
33 ms |
40792 KB |
Output is correct |
19 |
Correct |
30 ms |
40552 KB |
Output is correct |
20 |
Correct |
31 ms |
40540 KB |
Output is correct |
21 |
Correct |
26 ms |
40548 KB |
Output is correct |
22 |
Correct |
30 ms |
40548 KB |
Output is correct |
23 |
Correct |
32 ms |
40688 KB |
Output is correct |
24 |
Correct |
32 ms |
40544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
800 ms |
42904 KB |
Output is correct |
2 |
Correct |
807 ms |
42852 KB |
Output is correct |
3 |
Correct |
872 ms |
43176 KB |
Output is correct |
4 |
Correct |
762 ms |
42732 KB |
Output is correct |
5 |
Correct |
856 ms |
44372 KB |
Output is correct |
6 |
Correct |
725 ms |
44468 KB |
Output is correct |
7 |
Correct |
883 ms |
45100 KB |
Output is correct |
8 |
Correct |
837 ms |
43048 KB |
Output is correct |
9 |
Correct |
868 ms |
43052 KB |
Output is correct |
10 |
Correct |
800 ms |
44400 KB |
Output is correct |
11 |
Correct |
787 ms |
44360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1074 ms |
43736 KB |
Output is correct |
2 |
Correct |
1133 ms |
44940 KB |
Output is correct |
3 |
Correct |
922 ms |
44040 KB |
Output is correct |
4 |
Correct |
1035 ms |
45716 KB |
Output is correct |
5 |
Correct |
1028 ms |
44732 KB |
Output is correct |
6 |
Correct |
1012 ms |
44704 KB |
Output is correct |
7 |
Correct |
930 ms |
45140 KB |
Output is correct |
8 |
Correct |
1104 ms |
45132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1180 ms |
44720 KB |
Output is correct |
2 |
Correct |
1135 ms |
43524 KB |
Output is correct |
3 |
Correct |
1123 ms |
43000 KB |
Output is correct |
4 |
Correct |
1051 ms |
42860 KB |
Output is correct |
5 |
Correct |
914 ms |
42580 KB |
Output is correct |
6 |
Correct |
983 ms |
42096 KB |
Output is correct |
7 |
Correct |
990 ms |
44592 KB |
Output is correct |
8 |
Correct |
14 ms |
40656 KB |
Output is correct |
9 |
Correct |
32 ms |
40540 KB |
Output is correct |
10 |
Correct |
838 ms |
43860 KB |
Output is correct |
11 |
Correct |
990 ms |
44724 KB |
Output is correct |
12 |
Correct |
991 ms |
44552 KB |
Output is correct |
13 |
Correct |
994 ms |
44344 KB |
Output is correct |
14 |
Correct |
15 ms |
40536 KB |
Output is correct |
15 |
Correct |
33 ms |
40528 KB |
Output is correct |
16 |
Correct |
751 ms |
44104 KB |
Output is correct |
17 |
Correct |
1089 ms |
44660 KB |
Output is correct |
18 |
Correct |
1101 ms |
44612 KB |
Output is correct |
19 |
Correct |
1028 ms |
44884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
40536 KB |
Output is correct |
2 |
Correct |
14 ms |
40788 KB |
Output is correct |
3 |
Correct |
14 ms |
40616 KB |
Output is correct |
4 |
Correct |
17 ms |
40536 KB |
Output is correct |
5 |
Correct |
15 ms |
40540 KB |
Output is correct |
6 |
Correct |
15 ms |
40540 KB |
Output is correct |
7 |
Correct |
16 ms |
40540 KB |
Output is correct |
8 |
Correct |
14 ms |
40600 KB |
Output is correct |
9 |
Correct |
14 ms |
40540 KB |
Output is correct |
10 |
Correct |
12 ms |
41480 KB |
Output is correct |
11 |
Correct |
14 ms |
40540 KB |
Output is correct |
12 |
Correct |
15 ms |
40540 KB |
Output is correct |
13 |
Correct |
33 ms |
40528 KB |
Output is correct |
14 |
Correct |
31 ms |
40544 KB |
Output is correct |
15 |
Correct |
26 ms |
40432 KB |
Output is correct |
16 |
Correct |
33 ms |
40536 KB |
Output is correct |
17 |
Correct |
31 ms |
40532 KB |
Output is correct |
18 |
Correct |
33 ms |
40792 KB |
Output is correct |
19 |
Correct |
30 ms |
40552 KB |
Output is correct |
20 |
Correct |
31 ms |
40540 KB |
Output is correct |
21 |
Correct |
26 ms |
40548 KB |
Output is correct |
22 |
Correct |
30 ms |
40548 KB |
Output is correct |
23 |
Correct |
32 ms |
40688 KB |
Output is correct |
24 |
Correct |
32 ms |
40544 KB |
Output is correct |
25 |
Correct |
800 ms |
42904 KB |
Output is correct |
26 |
Correct |
807 ms |
42852 KB |
Output is correct |
27 |
Correct |
872 ms |
43176 KB |
Output is correct |
28 |
Correct |
762 ms |
42732 KB |
Output is correct |
29 |
Correct |
856 ms |
44372 KB |
Output is correct |
30 |
Correct |
725 ms |
44468 KB |
Output is correct |
31 |
Correct |
883 ms |
45100 KB |
Output is correct |
32 |
Correct |
837 ms |
43048 KB |
Output is correct |
33 |
Correct |
868 ms |
43052 KB |
Output is correct |
34 |
Correct |
800 ms |
44400 KB |
Output is correct |
35 |
Correct |
787 ms |
44360 KB |
Output is correct |
36 |
Correct |
1074 ms |
43736 KB |
Output is correct |
37 |
Correct |
1133 ms |
44940 KB |
Output is correct |
38 |
Correct |
922 ms |
44040 KB |
Output is correct |
39 |
Correct |
1035 ms |
45716 KB |
Output is correct |
40 |
Correct |
1028 ms |
44732 KB |
Output is correct |
41 |
Correct |
1012 ms |
44704 KB |
Output is correct |
42 |
Correct |
930 ms |
45140 KB |
Output is correct |
43 |
Correct |
1104 ms |
45132 KB |
Output is correct |
44 |
Correct |
1180 ms |
44720 KB |
Output is correct |
45 |
Correct |
1135 ms |
43524 KB |
Output is correct |
46 |
Correct |
1123 ms |
43000 KB |
Output is correct |
47 |
Correct |
1051 ms |
42860 KB |
Output is correct |
48 |
Correct |
914 ms |
42580 KB |
Output is correct |
49 |
Correct |
983 ms |
42096 KB |
Output is correct |
50 |
Correct |
990 ms |
44592 KB |
Output is correct |
51 |
Correct |
14 ms |
40656 KB |
Output is correct |
52 |
Correct |
32 ms |
40540 KB |
Output is correct |
53 |
Correct |
838 ms |
43860 KB |
Output is correct |
54 |
Correct |
990 ms |
44724 KB |
Output is correct |
55 |
Correct |
991 ms |
44552 KB |
Output is correct |
56 |
Correct |
994 ms |
44344 KB |
Output is correct |
57 |
Correct |
15 ms |
40536 KB |
Output is correct |
58 |
Correct |
33 ms |
40528 KB |
Output is correct |
59 |
Correct |
751 ms |
44104 KB |
Output is correct |
60 |
Correct |
1089 ms |
44660 KB |
Output is correct |
61 |
Correct |
1101 ms |
44612 KB |
Output is correct |
62 |
Correct |
1028 ms |
44884 KB |
Output is correct |
63 |
Correct |
1106 ms |
43608 KB |
Output is correct |
64 |
Correct |
1203 ms |
44136 KB |
Output is correct |
65 |
Correct |
1095 ms |
43908 KB |
Output is correct |
66 |
Correct |
1228 ms |
44988 KB |
Output is correct |
67 |
Correct |
1120 ms |
45200 KB |
Output is correct |
68 |
Correct |
950 ms |
44884 KB |
Output is correct |
69 |
Correct |
1010 ms |
44888 KB |
Output is correct |
70 |
Correct |
986 ms |
45132 KB |
Output is correct |
71 |
Correct |
1080 ms |
45336 KB |
Output is correct |