#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
using ll = long long;
const int MAX_N = 3e4;
struct BestSegTree {
vector <int> seg;
int n;
void init (int n) {
this->n = n;
seg.resize (1 + 4 * n);
}
void update (int node, int lb, int rb, int pos, int to_add) {
if (lb == rb) {
seg[node] += to_add;
return;
}
int mid = (lb + rb) / 2, left_node = node * 2, right_node = node * 2 + 1;
if (pos <= mid)
update (left_node, lb, mid, pos, to_add);
else
update (right_node, mid + 1, rb, pos, to_add);
seg[node] = max (seg[left_node], seg[right_node]);
}
int query (int node, int lb, int rb, int bound) {
if (lb == rb)
return lb;
int mid = (lb + rb) / 2, left_node = node * 2, right_node = node * 2 + 1;
if (seg[left_node] >= bound)
return query (left_node, lb, mid, bound);
if (seg[right_node] >= bound)
return query (left_node, mid + 1, rb, bound);
return -1;
}
int queryPos (int node, int lb, int rb, int pos) {
if (lb == rb)
return seg[node];
int mid = (lb + rb) / 2, left_node = node * 2, right_node = node * 2 + 1;
if (pos <= mid)
return queryPos (left_node, lb, mid, pos);
else
return queryPos (right_node, mid + 1, rb, pos);
}
};
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n, m, r, k, p;
cin >> n >> m >> r >> k >> p;
vector <BestSegTree> segW (n + 1), segE (n + 1);
for (int i = 1; i <= n; i++)
segW[i].init (m), segE[i].init (m);
vector <BestSegTree> segS (m + 1), segN (m + 1);
for (int i = 1; i <= m; i++)
segS[i].init (n), segN[i].init (n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x;
cin >> x;
segN[j].update (1, 1, n, i, x);
segS[j].update (1, 1, n, n - i + 1, x);
segW[i].update (1, 1, m, j, x);
segE[i].update (1, 1, m, m - j + 1, x);
}
}
while (k--) {
char dir;
cin >> dir;
if (dir == 'W') {
int row, height;
cin >> row >> height;
row = n - row + 1;
int lft = r;
bool good = true;
while (lft && good) {
int col = segW[row].query (1, 1, m, height);
if (col == -1)
good = false;
else {
segN[col].update (1, 1, n, row, -1);
segS[col].update (1, 1, n, n - row + 1, -1);
segW[row].update (1, 1, m, col, -1);
segE[row].update (1, 1, m, m - col + 1, -1);
}
lft--;
}
}
if (dir == 'E') {
int row, height;
cin >> row >> height;
row = n - row + 1;
int lft = r;
bool good = true;
while (lft && good) {
int col = segE[row].query (1, 1, m, height);
if (col == -1)
good = false;
else {
col = m - col + 1;
segN[col].update (1, 1, n, row, -1);
segS[col].update (1, 1, n, n - row + 1, -1);
segW[row].update (1, 1, m, col, -1);
segE[row].update (1, 1, m, m - col + 1, -1);
}
lft--;
}
}
if (dir == 'S') {
int col, height;
cin >> col >> height;
int lft = r;
bool good = true;
while (lft && good) {
int row = segS[col].query (1, 1, n, height);
if (row == -1)
good = false;
else {
row = n - row + 1;
segN[col].update (1, 1, n, row, -1);
segS[col].update (1, 1, n, n - row + 1, -1);
segW[row].update (1, 1, m, col, -1);
segE[row].update (1, 1, m, m - col + 1, -1);
}
lft--;
}
}
if (dir == 'N') {
int col, height;
cin >> col >> height;
int lft = r;
bool good = true;
while (lft && good) {
int row = segN[col].query (1, 1, n, height);
if (row == -1)
good = false;
else {
segN[col].update (1, 1, n, row, -1);
segS[col].update (1, 1, n, n - row + 1, -1);
segW[row].update (1, 1, m, col, -1);
segE[row].update (1, 1, m, m - col + 1, -1);
}
lft--;
}
}
}
vector <vector <int>> a (n + 1);
for (int i = 1; i <= n; i++) {
a[i].resize (m + 1);
for (int j = 1; j <= m; j++) {
a[i][j] = segW[i].queryPos (1, 1, m, j);
// cout << a[i][j] << " ";
}
// cout << "\n";
}
int mx = 0;
for (int i = 1; i <= n - p + 1; i++)
for (int j = 1; j <= m - p + 1; j++) {
int cnt = 0;
for (int x = i; x < i + p; x++)
for (int y = j; y < j + p; y++)
cnt += a[x][y];
mx = max (mx, cnt);
}
cout << mx << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
492 KB |
Output isn't correct |
4 |
Incorrect |
16 ms |
1004 KB |
Output isn't correct |
5 |
Incorrect |
151 ms |
4204 KB |
Output isn't correct |
6 |
Incorrect |
400 ms |
33132 KB |
Output isn't correct |
7 |
Incorrect |
738 ms |
76268 KB |
Output isn't correct |
8 |
Correct |
605 ms |
76396 KB |
Output is correct |
9 |
Incorrect |
685 ms |
71796 KB |
Output isn't correct |
10 |
Correct |
695 ms |
76268 KB |
Output is correct |
11 |
Incorrect |
595 ms |
75628 KB |
Output isn't correct |
12 |
Correct |
667 ms |
76396 KB |
Output is correct |
13 |
Correct |
621 ms |
81004 KB |
Output is correct |
14 |
Correct |
904 ms |
75628 KB |
Output is correct |
15 |
Incorrect |
754 ms |
76268 KB |
Output isn't correct |
16 |
Correct |
613 ms |
75756 KB |
Output is correct |
17 |
Incorrect |
971 ms |
81132 KB |
Output isn't correct |
18 |
Correct |
469 ms |
72812 KB |
Output is correct |
19 |
Incorrect |
615 ms |
85740 KB |
Output isn't correct |
20 |
Correct |
745 ms |
160908 KB |
Output is correct |