#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define int long long
vector<vector<int>> v;
vector<set<int>> l,c;
set<int> aux;
signed main()
{
    int n,m,poz,niv,r,k,p,i,j,max1=0;
    char ch;
    cin>>n>>m>>r>>k>>p;
    v.resize(n+1);
    l.resize(n+1);c.resize(m+1);
    for(i=1;i<=n;i++){
        v[i].resize(m+1);
        for(j=1;j<=m;j++){
            cin>>v[i][j];
            if(v[i][j]){
                l[i].insert(j);
                c[j].insert(i);
            }
        }
    }
    for(i=1;i<=k;i++){
        cin>>ch>>poz>>niv;
        if(ch=='N'){
            aux.clear();
            for(j=0;j<r && !c[poz].empty();j++){
                auto it=c[poz].begin();
                c[poz].erase(it);
                v[*it][poz]--;
                if(v[*it][poz]>0){
                    aux.insert(*it);
                }
            }
            c[poz].insert(aux.begin(),aux.end());
        }else if(ch=='S'){
            aux.clear();
            for(j=0;j<r&& !c[poz].empty();j++){
                auto it=c[poz].end();it--;
                c[poz].erase(it);
                v[*it][poz]--;
                if(v[*it][poz]>0){
                    aux.insert(*it);
                }
            }
            c[poz].insert(aux.begin(),aux.end());
        }else if(ch=='W'){
            aux.clear();
            for(j=0;j<r && !l[poz].empty();j++){
                auto it=l[poz].begin();
                l[poz].erase(it);
                v[poz][*it]--;
                if(v[poz][*it]>0){
                    aux.insert(*it);
                }
            }
            l[poz].insert(aux.begin(),aux.end());
        }else{
            aux.clear();
            for(j=0;j<r&& !l[poz].empty() ;j++){
                auto it=l[poz].end();it--;
                l[poz].erase(it);
                v[poz][*it]--;
                if(v[poz][*it]>0){
                    aux.insert(*it);
                }
            }
            l[poz].insert(aux.begin(),aux.end());
        }
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            v[i][j]+=v[i-1][j]+v[i][j-1]-v[i-1][j-1];
        }
    }
    for(i=1;i+p-1<=n;i++){
        for(j=1;j+p-1<=m;j++){
            max1=max(max1,v[i+p-1][j+p-1]-v[i-1][j+p-1]-v[i+p-1][j-1]+v[i-1][j-1]);
        }
    }
    cout<<max1;
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |