#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <iomanip>
#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int Z = (int)1e3 + 228;
const int N = (int)1e5 + 228;
const int INF = (int)1e9 + 228;
const int MOD = (int)1e9 + 7;
vector<vector<int> > a;
vector<vector<ll> > pref;
vector<vector<int> > t_row, t_col;
void build_row(int ind, int v, int tl, int tr)
{
if (tl == tr)
{
t_row[ind][v] = a[ind][tl];
return;
}
int mid = (tl + tr) / 2;
build_row(ind, 2 * v, tl, mid);
build_row(ind, 2 * v + 1, mid + 1, tr);
t_row[ind][v] = max(t_row[ind][2 * v], t_row[ind][2 * v + 1]);
}
void build_col(int ind, int v, int tl, int tr)
{
if (tl == tr)
{
t_col[ind][v] = a[tl][ind];
return;
}
int mid = (tl + tr) / 2;
build_col(ind, 2 * v, tl, mid);
build_col(ind, 2 * v + 1, mid + 1, tr);
t_col[ind][v] = max(t_col[ind][2 * v], t_col[ind][2 * v + 1]);
}
void update_row(int ind, int v, int tl, int tr, int pos)
{
if (tl == tr)
{
t_row[ind][v] = max(t_row[ind][v] - 1, 0);
return;
}
int mid = (tl + tr) / 2;
if (pos <= mid) update_row(ind, 2 * v, tl, mid, pos);
else update_row(ind, 2 * v + 1, mid + 1, tr, pos);
t_row[ind][v] = max(t_row[ind][2 * v], t_row[ind][2 * v + 1]);
}
void update_col(int ind, int v, int tl, int tr, int pos)
{
if (tl == tr)
{
t_col[ind][v] = max(t_col[ind][v] - 1, 0);
return;
}
int mid = (tl + tr) / 2;
if (pos <= mid) update_col(ind, 2 * v, tl, mid, pos);
else update_col(ind, 2 * v + 1, mid + 1, tr, pos);
t_col[ind][v] = max(t_col[ind][2 * v], t_col[ind][2 * v + 1]);
}
int get_up(int ind, int v, int tl, int tr, int h)
{
if (tl == tr)
return tl;
int mid = (tl + tr) / 2;
if (t_col[ind][2 * v] >= h) return get_up(ind, 2 * v, tl, mid, h);
else return get_up(ind, 2 * v + 1, mid + 1, tr, h);
}
int get_down(int ind, int v, int tl, int tr, int h)
{
if (tl == tr)
return tl;
int mid = (tl + tr) / 2;
if (t_col[ind][2 * v + 1] >= h) return get_down(ind, 2 * v + 1, mid + 1, tr, h);
else return get_down(ind, 2 * v, tl, mid, h);
}
int get_left(int ind, int v, int tl, int tr, int h)
{
if (tl == tr)
return tl;
int mid = (tl + tr) / 2;
if (t_row[ind][2 * v] >= h) return get_left(ind, 2 * v, tl, mid, h);
else return get_left(ind, 2 * v + 1, mid + 1, tr, h);
}
int get_right(int ind, int v, int tl, int tr, int h)
{
if (tl == tr)
return tl;
int mid = (tl + tr) / 2;
if (t_row[ind][2 * v + 1] >= h) return get_right(ind, 2 * v + 1, mid + 1, tr, h);
else return get_right(ind, 2 * v, tl, mid, h);
}
ll get_pref(vector<vector<ll> >& pref, int x1, int y1, int x2, int y2)
{
return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
}
int main()
{
SWS;
//files;
int n, m, r, k, p;
cin >> n >> m >> r >> k >> p;
a.resize(n + 1, vector<int> (m + 1));
pref.resize(n + 1, vector<ll> (m + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
t_row.resize(n + 1);
t_col.resize(m + 1);
for (int i = 1; i <= n; i++)
t_row[i].resize(4 * m);
for (int i = 1; i <= m; i++)
t_col[i].resize(4 * n);
for (int i = 1; i <= n; i++)
build_row(i, 1, 1, m);
for (int i = 1; i <= m; i++)
build_col(i, 1, 1, n);
while (k--)
{
char c;
int num, h;
cin >> c >> num >> h;
if (c == 'N')
{
for (int i = 1; i <= r; i++)
{
if (t_col[num][1] < h) break;
int pos = get_up(num, 1, 1, n, h);
a[pos][num] = max(a[pos][num] - 1, 0);
update_col(num, 1, 1, n, pos);
}
}
else if (c == 'S')
{
for (int i = 1; i <= r; i++)
{
if (t_col[num][1] < h) break;
int pos = get_down(num, 1, 1, n, h);
a[pos][num] = max(a[pos][num] - 1, 0);
update_col(num, 1, 1, n, pos);
}
}
else if (c == 'W')
{
for (int i = 1; i <= r; i++)
{
if (t_row[num][1] < h) break;
int pos = get_left(num, 1, 1, m, h);
a[num][pos] = max(a[num][pos] - 1, 0);
update_row(num, 1, 1, m, pos);
}
}
else
{
for (int i = 1; i <= r; i++)
{
if (t_row[num][1] < h) break;
int pos = get_right(num, 1, 1, m, h);
a[num][pos] = max(a[num][pos] - 1, 0);
update_row(num, 1, 1, m, pos);
}
}
}
ll ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a[i][j];
for (int i = p; i <= n; i++)
for (int j = p; j <= m; j++)
ans = max(ans, get_pref(pref, i - p + 1, j - p + 1, i, j));
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
4 |
Incorrect |
12 ms |
888 KB |
Output isn't correct |
5 |
Incorrect |
72 ms |
3192 KB |
Output isn't correct |
6 |
Incorrect |
159 ms |
22316 KB |
Output isn't correct |
7 |
Incorrect |
265 ms |
49500 KB |
Output isn't correct |
8 |
Correct |
214 ms |
49248 KB |
Output is correct |
9 |
Correct |
451 ms |
46508 KB |
Output is correct |
10 |
Correct |
636 ms |
49332 KB |
Output is correct |
11 |
Correct |
450 ms |
48416 KB |
Output is correct |
12 |
Correct |
647 ms |
49340 KB |
Output is correct |
13 |
Correct |
789 ms |
56660 KB |
Output is correct |
14 |
Correct |
541 ms |
48608 KB |
Output is correct |
15 |
Incorrect |
590 ms |
49228 KB |
Output isn't correct |
16 |
Correct |
237 ms |
48620 KB |
Output is correct |
17 |
Incorrect |
834 ms |
56828 KB |
Output isn't correct |
18 |
Correct |
233 ms |
52728 KB |
Output is correct |
19 |
Incorrect |
380 ms |
54348 KB |
Output isn't correct |
20 |
Correct |
1101 ms |
95656 KB |
Output is correct |