답안 #171764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
171764 2019-12-30T10:40:27 Z VEGAnn UFO (IZhO14_ufo) C++14
100 / 100
1085 ms 123512 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
#define pi3 pair<pii, int>
#define MP3(a,b,c) MP(MP(a, b), c)
using namespace std;
const int oo = 1e9;
const int N = 1000100;
const int K = 110;
const int PW = 20;
vector<vector<int> > a, pref;
int n, m, R, k, p, pos;
bool fd;
int ans = 0;

struct seg_tree{
    vector<int> mx;
    int tp, nm;

    seg_tree(): mx(0), tp(-1), nm(-1) {}

    seg_tree(int _tp, int _nm): mx(0), tp(_tp), nm(_nm) {
        if (tp == 0){
            mx.resize(4 * m);
            build(1, 0, m - 1);
        } else {
            mx.resize(4 * n);
            build(1, 0, n - 1);
        }
    }

    void build(int v, int l, int r){
        if (l == r){
            if (tp == 0)
                mx[v] = a[nm][l];
            else mx[v] = a[l][nm];
            return;
        }
        int md = (l + r) >> 1;
        build(v + v, l, md);
        build(v + v + 1, md + 1, r);
        mx[v] = max(mx[v + v], mx[v + v + 1]);
    }

    void real_search_rgt(int v, int l, int r, int vl){
        if (l == r){
            fd = 1;
            pos = l;
            return;
        }
        int md = (l + r) >> 1;
        if (mx[v + v + 1] >= vl)
            real_search_rgt(v + v + 1, md + 1, r, vl);
        else real_search_rgt(v + v, l, md, vl);
    }

    void search_rgt(int v, int tl, int tr, int l, int r, int vl){
        if (l > r || fd) return;
        if (tl == l && tr == r){
            if (mx[v] >= vl)
                real_search_rgt(v, l, r, vl);
            return;
        }
        int md = (tl + tr) >> 1;
        search_rgt(v + v + 1, md + 1, tr, max(md + 1, l), r, vl);
        search_rgt(v + v, tl, md, l, min(r, md), vl);
    }

    void real_search_lft(int v, int l, int r, int vl){
        if (l == r){
            fd = 1;
            pos = l;
            return;
        }
        int md = (l + r) >> 1;
        if (mx[v + v] >= vl)
            real_search_lft(v + v, l, md, vl);
        else real_search_lft(v + v + 1, md + 1, r, vl);
    }

    void search_lft(int v, int tl, int tr, int l, int r, int vl){
        if (l > r || fd) return;
        if (tl == l && tr == r){
            if (mx[v] >= vl)
                real_search_lft(v, l, r, vl);
            return;
        }
        int md = (tl + tr) >> 1;
        search_lft(v + v, tl, md, l, min(r, md), vl);
        search_lft(v + v + 1, md + 1, tr, max(md + 1, l), r, vl);
    }

    void update(int v, int l, int r, int ps){
        if (l == r){
            if (tp == 0)
                mx[v] = a[nm][l];
            else mx[v] = a[l][nm];
            return;
        }
        int md = (l + r) >> 1;
        if (ps <= md)
            update(v + v, l, md, ps);
        else update(v + v + 1, md + 1, r, ps);
        mx[v] = max(mx[v + v], mx[v + v + 1]);
    }

};
seg_tree row[N], col[N];

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m >> R >> k >> p;

    a.resize(n);
    for (int i = 0; i < n; i++){
        a[i].resize(m);
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int i = 0; i < n; i++)
        row[i] = seg_tree(0, i);

    for (int i = 0; i < m; i++)
        col[i] = seg_tree(1, i);

    for (; k; k--){
        char tp; int nm, ht;
        cin >> tp >> nm >> ht;
        nm--;
        if (tp == 'N'){
            int pr = -1;
            for (int it = 0; it < R; it++){
                fd = 0;
                pos = -1;
                col[nm].search_lft(1, 0, n - 1, pr + 1, n - 1, ht);
                if (!fd) break;
                pr = pos;
                a[pos][nm]--;
                col[nm].update(1, 0, n - 1, pos);
                row[pos].update(1, 0, m - 1, nm);
            }
        } else if (tp == 'S'){
            int pr = n;
            for (int it = 0; it < R; it++){
                fd = 0;
                pos = -1;
                col[nm].search_rgt(1, 0, n - 1, 0, pr - 1, ht);
                if (!fd) break;
                pr = pos;
                a[pos][nm]--;
                col[nm].update(1, 0, n - 1, pos);
                row[pos].update(1, 0, m - 1, nm);
            }
        } else if (tp == 'W'){
            int pr = -1;
            for (int it = 0; it < R; it++){
                fd = 0;
                pos = -1;
                row[nm].search_lft(1, 0, m - 1, pr + 1, m - 1, ht);
                if (!fd) break;
                pr = pos;
                a[nm][pos]--;
                row[nm].update(1, 0, m - 1, pos);
                col[pos].update(1, 0, n - 1, nm);
            }
        } else {
            int pr = m;
            for (int it = 0; it < R; it++){
                fd = 0;
                pos = -1;
                row[nm].search_rgt(1, 0, m - 1, 0, pr - 1, ht);
                if (!fd) break;
                pr = pos;
                a[nm][pos]--;
                row[nm].update(1, 0, m - 1, pos);
                col[pos].update(1, 0, n - 1, nm);
            }
        }
    }

    pref.resize(n + 1);
    for (int i = 0; i <= n; i++)
        pref[i].resize(m + 1);

    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++){
        pref[i][j] = a[i - 1][j - 1] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
    }

    for (int i = p; i <= n; i++)
    for (int j = p; j <= m; j++){
        int res = pref[i][j] - pref[i - p][j] - pref[i][j - p] + pref[i - p][j - p];
        ans = max(ans, res);
    }

    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 62968 KB Output is correct
2 Correct 62 ms 63016 KB Output is correct
3 Correct 64 ms 63096 KB Output is correct
4 Correct 79 ms 63548 KB Output is correct
5 Correct 167 ms 65912 KB Output is correct
6 Correct 268 ms 84720 KB Output is correct
7 Correct 326 ms 105980 KB Output is correct
8 Correct 254 ms 107744 KB Output is correct
9 Correct 544 ms 106744 KB Output is correct
10 Correct 663 ms 107512 KB Output is correct
11 Correct 469 ms 105976 KB Output is correct
12 Correct 689 ms 107500 KB Output is correct
13 Correct 842 ms 114680 KB Output is correct
14 Correct 615 ms 106232 KB Output is correct
15 Correct 698 ms 106096 KB Output is correct
16 Correct 271 ms 105308 KB Output is correct
17 Correct 1085 ms 113580 KB Output is correct
18 Correct 277 ms 110328 KB Output is correct
19 Correct 433 ms 108024 KB Output is correct
20 Correct 950 ms 123512 KB Output is correct