#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000;
struct segment_tree{
int N;
vector<long long> sum, left;
segment_tree(int N2){
N = 1;
while (N < N2){
N *= 2;
}
sum = vector<long long>(N * 2 - 1, INF);
left = vector<long long>(N * 2 - 1, INF);
for (int i = 0; i < N2; i++){
sum[N - 1 + i] = 0;
left[N - 1 + i] = 0;
}
for (int i = N - 2; i >= 0; i--){
sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2];
left[i] = 0;
}
}
void add(int i, long long x){
i += N - 1;
sum[i] += x;
left[i] = sum[i];
while (i > 0){
i = (i - 1) / 2;
sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2];
left[i] = min(left[i * 2 + 1], sum[i * 2 + 1] + min(left[i * 2 + 2], (long long) 0));
}
}
long long get(){
return left[0];
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int M, N;
cin >> M >> N;
int B;
cin >> B;
int P;
cin >> P;
vector<int> X1(P), Y1(P), X2(P), Y2(P), C(P);
for (int i = 0; i < P; i++){
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i] >> C[i];
X1[i]--;
Y1[i]--;
}
int tv = 0, fv = min(M, N) + 1;
while (fv - tv > 1){
int mid = (tv + fv) / 2;
vector<tuple<int, int, int>> T;
for (int i = 0; i < P; i++){
T.push_back(make_tuple(max(Y1[i] - mid + 1, 0), i, 0));
if (Y2[i] < N - mid + 1){
T.push_back(make_tuple(Y2[i], i, 1));
}
}
sort(T.begin(), T.end());
int cnt = T.size();
segment_tree ST(M - mid + 1);
bool ok = false;
for (int i = 0; i < cnt; i++){
int j = get<1>(T[i]);
int t = get<2>(T[i]);
if (t == 0){
ST.add(max(X1[j] - mid + 1, 0), C[j]);
if (X2[j] < M - mid + 1){
ST.add(X2[j], -C[j]);
}
}
if (t == 1){
ST.add(max(X1[j] - mid + 1, 0), -C[j]);
if (X2[j] < M - mid + 1){
ST.add(X2[j], C[j]);
}
}
if (i < cnt - 1){
if (get<0>(T[i]) != get<0>(T[i + 1])){
if (ST.get() <= B){
ok = true;
}
}
}
}
if (ST.get() <= B){
ok = true;
}
if (ok){
tv = mid;
} else {
fv = mid;
}
}
cout << tv << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
4504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
16724 KB |
Output is correct |
2 |
Correct |
399 ms |
33324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
33264 KB |
Output is correct |
2 |
Correct |
202 ms |
16884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
852 KB |
Output is correct |
2 |
Correct |
34 ms |
1244 KB |
Output is correct |
3 |
Correct |
34 ms |
1256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
5428 KB |
Output is correct |
2 |
Correct |
217 ms |
5436 KB |
Output is correct |
3 |
Correct |
175 ms |
5428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
498 ms |
17996 KB |
Output is correct |
2 |
Correct |
216 ms |
34288 KB |
Output is correct |
3 |
Correct |
37 ms |
1952 KB |
Output is correct |
4 |
Correct |
804 ms |
34796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
610 ms |
34972 KB |
Output is correct |
2 |
Correct |
888 ms |
34964 KB |
Output is correct |
3 |
Correct |
395 ms |
18064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
618 ms |
18672 KB |
Output is correct |
2 |
Correct |
1081 ms |
35332 KB |
Output is correct |
3 |
Correct |
1115 ms |
35268 KB |
Output is correct |
4 |
Correct |
1068 ms |
35228 KB |
Output is correct |
5 |
Correct |
1151 ms |
35200 KB |
Output is correct |
6 |
Correct |
328 ms |
17868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5083 ms |
47908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5087 ms |
58328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5085 ms |
62672 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |