#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1e6 + 1;
const int MAX_OBSTACLES = 4e5 + 1;
int n, m, b;
int num_obstacles;
pair<int, int> obstacle_x[MAX_OBSTACLES];
pair<int, int> obstacle_y[MAX_OBSTACLES];
int cost[MAX_OBSTACLES];
struct event{
int x, type, y_start, y_end;
};
bool comp(event x, event y){
return x.x < y.x;
}
event events[2 * MAX_OBSTACLES];
struct node{
int left_bound, right_bound;
int left_value, left_zeros;
int right_value, right_zeros;
int min_value, all_zeros;
int lazy;
};
node merge(node left, node right){
node actual_left = {left.left_bound, left.right_bound, left.left_value + left.lazy, left.left_zeros, left.right_value + left.lazy, left.right_zeros, left.min_value + left.lazy, left.all_zeros, 0};
node actual_right = {right.left_bound, right.right_bound, right.left_value + right.lazy, right.left_zeros, right.right_value + right.lazy, right.right_zeros, right.min_value + right.lazy, right.all_zeros, 0};
node x = {left.left_bound, right.right_bound, actual_left.left_value, 0, actual_right.right_value, 0, min(actual_left.min_value, actual_right.min_value), 0, 0};
if (actual_left.left_zeros == (left.right_bound - left.left_bound + 1) and actual_left.left_value == actual_right.left_value){
x.left_zeros = actual_left.left_zeros + actual_right.left_zeros;
}
else{
x.left_zeros = actual_left.left_zeros;
}
if (actual_right.right_zeros == (right.right_bound - right.left_bound + 1) and actual_left.right_value == actual_right.right_value){
x.right_zeros = actual_right.right_zeros + actual_left.right_zeros;
}
else{
x.right_zeros = actual_right.right_zeros;
}
if (actual_left.min_value == x.min_value){
x.all_zeros = max(x.all_zeros, actual_left.all_zeros);
}
if (actual_right.min_value == x.min_value){
x.all_zeros = max(x.all_zeros, actual_right.all_zeros);
}
if (actual_left.right_value == actual_right.left_value and actual_left.right_value == x.min_value){
x.all_zeros = max(x.all_zeros, actual_left.right_zeros + actual_right.left_zeros);
}
//cout << actual_left.min_value << ' ' << actual_left.all_zeros << ' ' << actual_right.min_value << ' ' << actual_right.all_zeros << ' ' << x.all_zeros << '\n';
return x;
}
int size_of_tree;
node tree[4 * MAX_N];
void update(int left, int right, int tree_node, int delta){
int node_left = tree[tree_node].left_bound;
int node_right = tree[tree_node].right_bound;
if (node_right < left or node_left > right){
return;
}
if (node_left >= left and node_right <= right){
tree[tree_node].lazy += delta;
return;
}
tree[2 * tree_node].lazy += tree[tree_node].lazy;
tree[2 * tree_node + 1].lazy += tree[tree_node].lazy;
update(left, right, 2 * tree_node, delta);
update(left, right, 2 * tree_node + 1, delta);
tree[tree_node] = merge(tree[2 * tree_node], tree[2 * tree_node + 1]);
//cout << tree_node << ' ' << tree[tree_node].all_zeros << '\n';
}
node longest_consecutive;
void find_longest_consecutive(int left, int right, int tree_node){
int node_left = tree[tree_node].left_bound;
int node_right = tree[tree_node].right_bound;
if (node_right < left or node_left > right){
return;
}
if (node_left >= left and node_right <= right){
longest_consecutive = merge(longest_consecutive, tree[tree_node]);
return;
}
tree[2 * tree_node].lazy += tree[tree_node].lazy;
tree[2 * tree_node + 1].lazy += tree[tree_node].lazy;
find_longest_consecutive(left, right, 2 * tree_node);
find_longest_consecutive(left, right, 2 * tree_node + 1);
tree[tree_node] = merge(tree[2 * tree_node], tree[2 * tree_node + 1]);
}
int main(){
cin >> m >> n >> b >> num_obstacles;
for (int i = 1;i <= num_obstacles;i++){
cin >> obstacle_x[i].first >> obstacle_y[i].first >> obstacle_x[i].second >> obstacle_y[i].second >> cost[i];
events[2 * i - 1] = {obstacle_x[i].first, 1, obstacle_y[i].first, obstacle_y[i].second};
events[2 * i] = {obstacle_x[i].second + 1, -1, obstacle_y[i].first, obstacle_y[i].second};
}
sort(events + 1, events + 2 * num_obstacles + 1, comp);
size_of_tree = 1;
while (size_of_tree < n){
size_of_tree *= 2;
}
for (int i = 1;i <= size_of_tree;i++){
tree[size_of_tree + i - 1] = {i, i, 0, 1, 0, 1, 0, 1, 0};
}
for (int i = size_of_tree - 1;i >= 1;i--){
tree[i] = merge(tree[2 * i], tree[2 * i + 1]);
}
int ans = 0;
int ctr = 1;
int ctr_2 = 1;
int curr = 0;
for (int i = 1;i <= m;i++){
while (ctr_2 <= 2 * num_obstacles and events[ctr_2].x == i){
if (events[ctr_2].type == -1){
update(events[ctr_2].y_start, events[ctr_2].y_end, 1, events[ctr_2].type);
}
ctr_2++;
}
longest_consecutive = {0, 0, 0, 0, 0, 0};
find_longest_consecutive(1, n, 1);
if (longest_consecutive.all_zeros >= (curr - i + 1)){
ans = max(ans, curr - i + 1);
}
else{
continue;
}
while ((curr + 1) <= m){
curr++;
while (ctr <= 2 * num_obstacles and events[ctr].x == curr){
if (events[ctr].type == 1){
update(events[ctr].y_start, events[ctr].y_end, 1, events[ctr].type);
}
ctr++;
}
longest_consecutive = {0, 0, 0, 0, 0, 0};
find_longest_consecutive(1, n, 1);
if (longest_consecutive.min_value != 0 or longest_consecutive.all_zeros < (curr - i + 1)){
break;
}
ans = max(ans, curr - i + 1);
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
784 ms |
74400 KB |
Output is correct |
2 |
Correct |
761 ms |
74320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
797 ms |
74324 KB |
Output is correct |
2 |
Correct |
783 ms |
74324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
1884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
10752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
844 ms |
75936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
894 ms |
76116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
912 ms |
76624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1416 ms |
90276 KB |
Output is correct |
2 |
Correct |
999 ms |
24916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1657 ms |
98132 KB |
Output is correct |
2 |
Correct |
1643 ms |
97592 KB |
Output is correct |
3 |
Correct |
731 ms |
97416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1798 ms |
106016 KB |
Output is correct |
2 |
Correct |
2045 ms |
105556 KB |
Output is correct |
3 |
Correct |
2093 ms |
105560 KB |
Output is correct |
4 |
Correct |
1936 ms |
105296 KB |
Output is correct |
5 |
Correct |
1605 ms |
105556 KB |
Output is correct |