#include <bits/stdc++.h>
using namespace std;
struct segtree {
vector<int> mx;
segtree() {}
segtree(int n) : mx(4 * n) {}
void modify(int id, int l, int r, int i, int v) {
if (l == r) {
mx[id] += v;
return;
}
int mid = (l + r) >> 1;
if (i <= mid) {
modify(id << 1, l, mid, i, v);
} else {
modify(id << 1 | 1, mid + 1, r, i, v);
}
mx[id] = max(mx[id << 1], mx[id << 1 | 1]);
}
int find_first(int id, int l, int r, int i, int v) {
if (r < i || mx[id] < v) {
return -1;
}
if (l == r) {
return l;
}
int mid = (l + r) / 2;
int f = find_first(id << 1, l, mid, i, v);
if (f != -1) {
return f;
} else {
return find_first(id << 1 | 1, mid + 1, r, i, v);
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, r, k, p;
cin >> n >> m >> r >> k >> p;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
vector<vector<segtree>> st(4);
st[0] = vector<segtree>(n, segtree(m));
st[1] = vector<segtree>(n, segtree(m));
st[2] = vector<segtree>(m, segtree(n));
st[3] = vector<segtree>(m, segtree(n));
vector<vector<int>> b(n, vector<int>(m));
auto Update = [&](int i, int j, int v) {
st[0][i].modify(1, 0, m - 1, j, v);
st[1][i].modify(1, 0, m - 1, m - 1 - j, v);
st[2][j].modify(1, 0, n - 1, i, v);
st[3][j].modify(1, 0, n - 1, n - 1 - i, v);
b[i][j] += v;
};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Update(i, j, a[i][j]);
}
}
while (k--) {
char op;
cin >> op;
if (op == 'W') {
int i, h;
cin >> i >> h;
--i;
int lst = 0;
for (int it = 0; it < r && lst < m; it++) {
int j = st[0][i].find_first(1, 0, m - 1, lst, h);
if (j == -1) {
break;
}
Update(i, j, -1);
lst = j + 1;
}
}
if (op == 'E') {
int i, h;
cin >> i >> h;
--i;
int lst = 0;
for (int it = 0; it < r && lst < m; it++) {
int j = st[1][i].find_first(1, 0, m - 1, lst, h);
if (j == -1) {
break;
}
Update(i, m - 1 - j, -1);
lst = j + 1;
}
}
if (op == 'N') {
int j, h;
cin >> j >> h;
--j;
int lst = 0;
for (int it = 0; it < r && lst < n; it++) {
int i = st[2][j].find_first(1, 0, n - 1, lst, h);
if (i == -1) {
break;
}
Update(i, j, -1);
lst = i + 1;
}
}
if (op == 'S') {
int j, h;
cin >> j >> h;
--j;
int lst = 0;
for (int it = 0; it < r && lst < n; it++) {
int i = st[3][j].find_first(1, 0, n - 1, lst, h);
if (i == -1) {
break;
}
Update(n - 1 - i, j, -1);
lst = i + 1;
}
}
}
vector<vector<long long>> sum(n, vector<long long>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum[i][j] = b[i][j];
if (i > 0) {
sum[i][j] += sum[i - 1][j];
}
if (j > 0) {
sum[i][j] += sum[i][j - 1];
}
if (i > 0 && j > 0) {
sum[i][j] -= sum[i - 1][j - 1];
}
}
}
auto Query = [&](int x1, int y1, int x2, int y2) {
long long res = sum[x2][y2];
if (x1 > 0) {
res -= sum[x1 - 1][y2];
}
if (y1 > 0) {
res -= sum[x2][y1 - 1];
}
if (x1 > 0 && y1 > 0) {
res += sum[x1 - 1][y1 - 1];
}
return res;
};
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans = max(ans, Query(max(0, i - p + 1), max(0, j - p + 1), i, j));
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
13 ms |
1316 KB |
Output is correct |
5 |
Correct |
90 ms |
5460 KB |
Output is correct |
6 |
Correct |
256 ms |
42156 KB |
Output is correct |
7 |
Correct |
341 ms |
95600 KB |
Output is correct |
8 |
Correct |
290 ms |
92096 KB |
Output is correct |
9 |
Correct |
648 ms |
86780 KB |
Output is correct |
10 |
Correct |
672 ms |
91048 KB |
Output is correct |
11 |
Correct |
497 ms |
89380 KB |
Output is correct |
12 |
Correct |
664 ms |
91304 KB |
Output is correct |
13 |
Correct |
757 ms |
101000 KB |
Output is correct |
14 |
Correct |
573 ms |
89640 KB |
Output is correct |
15 |
Correct |
638 ms |
92724 KB |
Output is correct |
16 |
Correct |
296 ms |
92004 KB |
Output is correct |
17 |
Correct |
905 ms |
105764 KB |
Output is correct |
18 |
Correct |
269 ms |
94872 KB |
Output is correct |
19 |
Correct |
379 ms |
101776 KB |
Output is correct |
20 |
Correct |
883 ms |
173296 KB |
Output is correct |