// I stand with PALESTINE
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
struct segtree {
vector<int> tree;
int size;
void init(int n) {
size = 1;
while (size < n) size *= 2;
tree.assign(2 * size, 0);
}
void build(vector<int> &a) {
init((int) a.size());
for (int p = size; p < size + a.size(); p++) tree[p] = a[p - size];
for (int p = size - 1; p > 0; p--) tree[p] = max(tree[p << 1], tree[p << 1 | 1]);
}
void upd(int p, int v) {
p += size;
tree[p] = v;
for (; p > 1; p >>= 1) tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
int get_next(int i, int v, int x, int lx, int rx) {
if (tree[x] < v or rx - 1 < i) return -1;
if (rx - lx == 1) return (tree[x] >= v ? lx : -1);
int m = (lx + rx) >> 1;
int ans = get_next(i, v, x << 1, lx, m);
if (ans != -1) return ans;
return get_next(i, v, x << 1 | 1, m, rx);
}
int get_next(int i, int v) {
return get_next(i, v, 1, 0, size);
}
int get_prev(int i, int v, int x, int lx, int rx) {
if (tree[x] < v or i < lx) return -1;
if (rx - lx == 1) return (tree[x] >= v ? lx : -1);
int m = (lx + rx) >> 1;
int ans = get_prev(i, v, x << 1 | 1, m, rx);
if (ans != -1) return ans;
return get_prev(i, v, x << 1, lx, m);
}
int get_prev(int i, int v) {
return get_prev(i, v, 1, 0, size);
}
int get(int p) {
return tree[p + size];
}
};
void solve() {
int n, m, r, q, p;
cin >> n >> m >> r >> q >> p;
vector<segtree> col(n), row(m);
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];
col[i].build(a[i]);
}
vector<int> copy_row(n);
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) copy_row[i] = a[i][j];
row[j].build(copy_row);
}
while (q--) {
char op;
cin >> op;
if (op == 'W' or op == 'E') {
int i, v;
cin >> i >> v;
assert(1 <= i and i <= n and v != 0);
i--;
int now, cnt = 0;
if (op == 'W') now = col[i].get_next(0, v);
else now = col[i].get_prev(m, v);
while (cnt < r and now != -1) {
row[now].upd(i, col[i].get(now) - 1);
col[i].upd(now, col[i].get(now) - 1);
cnt++;
if (op == 'W') now = col[i].get_next(now + 1, v);
else now = col[i].get_prev(now - 1, v);
}
} else {
int j, v;
cin >> j >> v;
assert(1 <= j and j <= m and v != 0);
j--;
int now, cnt = 0;
if (op == 'N') now = row[j].get_next(0, v);
else now = row[j].get_prev(n, v);
while (cnt < r and now != -1) {
if (now == 1 and j == 0) {
cout << row[j].get(now) - 1 << '\n';
cout << col[now].get(j) - 1 << '\n';
}
col[now].upd(j, row[j].get(now) - 1);
row[j].upd(now, row[j].get(now) - 1);
cnt++;
if (op == 'N') now = row[j].get_next(now + 1, v);
else now = row[j].get_prev(now - 1, v);
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) cout << col[i].get(j) << ' ';
// cout << endl;
// }
// cout << endl;
}
int ans = 0;
for (int i = 0; i <= n - p; i++) {
for (int j = 0; j <= m - p; j++) {
int sum = 0;
for (int x = i; x < i + p; x++) {
for (int y = j; y < j + p; y++) {
sum += col[x].get(y);
}
}
ans = max(ans, sum);
}
}
cout << ans;
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
Compilation message
ufo.cpp: In member function 'void segtree::build(std::vector<int>&)':
ufo.cpp:25:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int p = size; p < size + a.size(); p++) tree[p] = a[p - size];
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
704 KB |
Output is correct |
5 |
Correct |
34 ms |
2412 KB |
Output is correct |
6 |
Correct |
85 ms |
13648 KB |
Output is correct |
7 |
Correct |
126 ms |
33564 KB |
Output is correct |
8 |
Correct |
114 ms |
33480 KB |
Output is correct |
9 |
Correct |
205 ms |
30092 KB |
Output is correct |
10 |
Correct |
263 ms |
32608 KB |
Output is correct |
11 |
Correct |
181 ms |
26004 KB |
Output is correct |
12 |
Correct |
277 ms |
32336 KB |
Output is correct |
13 |
Correct |
314 ms |
35412 KB |
Output is correct |
14 |
Correct |
232 ms |
25624 KB |
Output is correct |
15 |
Correct |
278 ms |
32892 KB |
Output is correct |
16 |
Correct |
104 ms |
28516 KB |
Output is correct |
17 |
Correct |
413 ms |
35404 KB |
Output is correct |
18 |
Correct |
112 ms |
29780 KB |
Output is correct |
19 |
Correct |
158 ms |
38980 KB |
Output is correct |
20 |
Correct |
471 ms |
77764 KB |
Output is correct |