#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
namespace sub2 {
const int maxN = 1e6 + 20;
int tree[maxN << 2];
int lazy[maxN << 2];
int Lx[maxN];
int Ly[maxN];
int Rx[maxN];
int Ry[maxN];
int cost[maxN];
vector<int> queries[maxN];
int N, M, B, K;
void reset(int id, int lt, int rt) {
tree[id] = 0;
lazy[id] = 0;
if (lt == rt) {
return;
}
int mt = (lt + rt) >> 1;
reset(id << 1, lt, mt);
reset(id << 1 | 1, mt + 1, rt);
}
void update(int id, int lt, int rt, int ql, int qr, int add) {
if (lt == ql && rt == qr) {
tree[id] += add;
lazy[id] += add;
return;
}
tree[id << 1] += lazy[id];
lazy[id << 1] += lazy[id];
tree[id << 1 | 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
lazy[id] = 0;
int mt = (lt + rt) >> 1;
if (qr <= mt) {
update(id << 1, lt, mt, ql, qr, add);
}
else if (ql >= mt + 1) {
update(id << 1 | 1, mt + 1, rt, ql, qr, add);
}
else {
update(id << 1, lt, mt, ql, mt, add);
update(id << 1 | 1, mt + 1, rt, mt + 1, qr, add);
}
tree[id] = min(tree[id << 1], tree[id << 1 | 1]);
}
bool check(int sz) {
int n = N - sz + 1;
int m = M - sz + 1;
for (int i = 1; i <= n; i++) {
queries[i].clear();
}
for (int i = 1; i <= K; i++) {
int lx = max(1, Lx[i] - sz + 1);
int rx = min(n, Rx[i]);
int ly = max(1, Ly[i] - sz + 1);
int ry = min(m, Ry[i]);
if (lx <= rx && ly <= ry) {
queries[lx].push_back(i);
if (rx < n) {
queries[rx + 1].push_back(-i);
}
}
}
reset(1, 1, m);
for (int i = 1; i <= n; i++) {
for (auto id: queries[i]) {
if (id > 0) {
int lt = max(1, Ly[id] - sz + 1);
int rt = min(m, Ry[id]);
update(1, 1, m, lt, rt, cost[id]);
}
else {
int lt = max(1, Ly[-id] - sz + 1);
int rt = min(m, Ry[-id]);
update(1, 1, m, lt, rt, -cost[-id]);
}
}
if (tree[1] <= B) {
return true;
}
}
return false;
}
void solve() {
for (int i = 1; i <= K; i++) {
cin >> Lx[i] >> Ly[i] >> Rx[i] >> Ry[i] >> cost[i];
}
int res = 0;
int lt = 1;
int rt = min(N, M);
while (lt <= rt) {
int mt = (lt + rt) >> 1;
if (check(mt)) {
res = mt;
lt = mt + 1;
}
else {
rt = mt - 1;
}
}
cout << res;
}
}
namespace sub13 {
struct node {
int len, min_val, best_all, best_pref, best_suf, lazy;
node() {};
node(int _len): len(_len), min_val(0), best_all(_len), best_pref(_len), best_suf(_len), lazy(0) {};
};
const int maxN = 1e6 + 20;
vector<pair<int, int>> rem[maxN];
vector<pair<int, int>> add[maxN];
node tree[maxN << 2];
int N, M, B, K;
node merge(node L, node R) {
node res;
res.len = L.len + R.len;
res.min_val = min(L.min_val, R.min_val);
res.best_all = 0;
int sum = 0;
if (L.min_val == res.min_val) {
res.best_all = max(res.best_all, L.best_all);
sum += L.best_suf;
if (L.best_pref == L.len && R.min_val == res.min_val) {
res.best_pref = L.len + R.best_pref;
}
else {
res.best_pref = L.best_pref;
}
}
else {
res.best_pref = 0;
}
if (R.min_val == res.min_val) {
res.best_all = max(res.best_all, R.best_all);
sum += R.best_pref;
if (R.best_suf == R.len && L.min_val == res.min_val) {
res.best_suf = R.len + L.best_suf;
}
else {
res.best_suf = R.best_suf;
}
}
else {
res.best_suf = 0;
}
res.best_all = max(res.best_all, sum);
res.lazy = 0;
return res;
}
void build(int id, int lt, int rt) {
tree[id] = node(rt - lt + 1);
if (lt == rt) {
return;
}
int mt = (lt + rt) >> 1;
build(id << 1, lt, mt);
build(id << 1 | 1, mt + 1, rt);
}
void update(int id, int lt, int rt, int ql, int qr, int add) {
if (lt == ql && rt == qr) {
tree[id].lazy += add;
tree[id].min_val += add;
return;
}
tree[id << 1].lazy += tree[id].lazy;
tree[id << 1].min_val += tree[id].lazy;
tree[id << 1 | 1].lazy += tree[id].lazy;
tree[id << 1 | 1].min_val += tree[id].lazy;
tree[id].lazy = 0;
int mt = (lt + rt) >> 1;
if (qr <= mt) {
update(id << 1, lt, mt, ql, qr, add);
}
else if (ql >= mt + 1) {
update(id << 1 | 1, mt + 1, rt, ql, qr, add);
}
else {
update(id << 1, lt, mt, ql, mt, add);
update(id << 1 | 1, mt + 1, rt, mt + 1, qr, add);
}
tree[id] = merge(tree[id << 1], tree[id << 1 | 1]);
}
void solve() {
for (int i = 1; i <= K; i++) {
int lx, ly, rx, ry, cost;
cin >> lx >> ly >> rx >> ry >> cost;
add[lx].emplace_back(ly, ry);
rem[rx].emplace_back(ly, ry);
}
build(1, 1, M);
int rt = 0;
int res = 0;
for (int i = 1; i <= N; i++) {
while (true) {
int best = (tree[1].min_val == 0 ? tree[1].best_all : 0);
res = max(res, min(rt - i + 1, best));
if (rt == N || (best < rt - i + 1 && rt >= i)) {
break;
}
rt++;
for (auto p: add[rt]) {
update(1, 1, M, p.first, p.second, 1);
}
}
for (auto p: rem[i]) {
update(1, 1, M, p.first, p.second, -1);
}
}
cout << res;
}
}
void just_do_it() {
int N, M, B, K;
cin >> N >> M >> B >> K;
if (B > 0) {
sub2::N = N;
sub2::M = M;
sub2::B = B;
sub2::K = K;
sub2::solve();
}
else {
sub13::N = N;
sub13::M = M;
sub13::B = B;
sub13::K = K;
sub13::solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
70704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
70772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
70752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
71584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
76892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
119968 KB |
Output is correct |
2 |
Correct |
72 ms |
120064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
119992 KB |
Output is correct |
2 |
Correct |
72 ms |
120072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
71116 KB |
Output is correct |
2 |
Correct |
75 ms |
71480 KB |
Output is correct |
3 |
Correct |
59 ms |
71500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
75168 KB |
Output is correct |
2 |
Correct |
289 ms |
75716 KB |
Output is correct |
3 |
Correct |
263 ms |
75780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
83816 KB |
Output is correct |
2 |
Correct |
110 ms |
75608 KB |
Output is correct |
3 |
Correct |
158 ms |
87884 KB |
Output is correct |
4 |
Correct |
542 ms |
96080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
408 ms |
94748 KB |
Output is correct |
2 |
Correct |
858 ms |
97432 KB |
Output is correct |
3 |
Correct |
149 ms |
81112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
84488 KB |
Output is correct |
2 |
Correct |
1202 ms |
100128 KB |
Output is correct |
3 |
Correct |
1113 ms |
99744 KB |
Output is correct |
4 |
Correct |
1143 ms |
99848 KB |
Output is correct |
5 |
Correct |
1161 ms |
100116 KB |
Output is correct |
6 |
Correct |
79 ms |
80332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
694 ms |
131512 KB |
Output is correct |
2 |
Correct |
344 ms |
83164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
806 ms |
136360 KB |
Output is correct |
2 |
Correct |
844 ms |
133028 KB |
Output is correct |
3 |
Correct |
623 ms |
127872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1008 ms |
140912 KB |
Output is correct |
2 |
Correct |
1311 ms |
138700 KB |
Output is correct |
3 |
Correct |
1290 ms |
138680 KB |
Output is correct |
4 |
Correct |
1167 ms |
136892 KB |
Output is correct |
5 |
Correct |
814 ms |
142520 KB |
Output is correct |