Submission #171968

#TimeUsernameProblemLanguageResultExecution timeMemory
171968NightmarUFO (IZhO14_ufo)C++17
10 / 100
629 ms262148 KiB
#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(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, 0)); pref.resize(n + 1, vector<ll> (m + 1, 0)); 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 + 228)); for (int i = 1; i <= m; i++) t_col[i].resize(4 * (n + 228)); 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(i - p + 1, j - p + 1, i, j)); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...