Submission #91263

#TimeUsernameProblemLanguageResultExecution timeMemory
91263inomUFO (IZhO14_ufo)C++14
60 / 100
2076 ms218308 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

#define fi first
#define se second
#define new new228
#define pb push_back
#define rank rank228
#define int long long
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
 
using namespace std;
using namespace __gnu_pbds;
 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)
 
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // st.oreder_of_key();

const int N = 1000100;
const int INF = 1e9 + 7;
const int MAXN = 4 * N;
const int MOD = 998244353;
 
int TN = 1;

int n, m, r, k, p;
vector<vector<int>> a;
set<int> column[N], row[N];
pair<char, pair<int, int>> Q[N];

void subtask1() {
    for (int i = 1; i <= k; i++) {
        char c; int x, h;
        c = Q[i].fi, x = Q[i].se.fi, h = Q[i].se.se;
        if (c == 'W' || c == 'E') {
            if (c == 'W') {
                int cnt = r, j = 1;
                while (j <= m && cnt > 0) {
                    if (a[x][j] >= h) {
                        a[x][j]--; cnt--;
                    }
                    j++;
                }    
            }
            else {
                int cnt = r, j = m;
                while (j >= 1 && cnt > 0) {
                    if (a[x][j] >= h) {
                        a[x][j]--; cnt--;
                    }
                    j--;
                }
            }
        }
        else {
            if (c == 'N') {
                int cnt = r, j = 1;
                while (j <= n && cnt > 0) {
                    if (a[j][x] >= h) {
                        a[j][x]--; cnt--;
                    }
                    j++;
                }
            }
            else {
                int cnt = r, j = n;
                while (j >= 1 && cnt > 0) {
                    if (a[j][x] >= h) {
                        a[j][x]--; cnt--;
                    }
                    j--;
                }
            }
        }
    }
    int ans = 0; p--;
    for (int i = 1; i + p <= n; i++) {
        for (int j = 1; j + p <= m; j++) {
            int sum = 0;
            for (int u = i; u <= i + p; u++) {
                for (int v = j; v <= j + p; v++) {
                    sum += a[u][v];
                }
            }
            ans = max(ans, sum);
        }
    }
    printf("%lld\n", ans);
    exit(false);
}

void subtask2() {
    /*for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;*/
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j]) {
                column[i].insert(j);
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[j][i]) {
                row[i].insert(j);
            }
        }
    }
    /*for (int i = 1; i <= n; i++) {
        cout << i << " ";
        for (auto j: column[i]) {
            cout << j << " ";
        }
        cout << endl;
    }
    cout << endl;*/
    for (int qs = 1; qs <= k; qs++) {
        char c; int x, h;
        c = Q[qs].fi; x = Q[qs].se.fi; h = Q[qs].se.se;
        // cout << c << " " << x << " " << h << "\n";
        if (c == 'W') {
            int cnt = 0;
            vector<int> tmp;
            while (cnt < r && sz(column[x]) > 0) {
                int cur = *column[x].begin();
                column[x].erase(column[x].begin());
                if (a[x][cur] > 0) {
                    a[x][cur]--; cnt++;
                    if (a[x][cur] > 0) 
                        tmp.pb(cur);
                }
            }
            for (auto i: tmp) {
                column[x].insert(i);
            }
        }
        if (c == 'E') {
            int cnt = 0;
            vector<int> tmp;
            while (cnt < r && sz(column[x]) > 0) {
                int cur = *column[x].rbegin();
                column[x].erase(--column[x].end());
                if (a[x][cur] > 0) {
                    a[x][cur]--; cnt++;
                    if (a[x][cur] > 0)
                        tmp.pb(cur);
                }
            }
            for (auto i: tmp) {
                column[x].insert(i);
            }
        }
        if (c == 'N') {
            int cnt = 0;
            vector<int> tmp;
            while (cnt < r && sz(row[x]) > 0) {
                int cur = *row[x].begin();
                // cout << x << " " << cur << " " << a[x][cur] << "\n";
                row[x].erase(row[x].begin());
                if (a[cur][x] > 0) {
                    a[cur][x]--; cnt++;
                    if (a[cur][x] > 0) 
                        tmp.pb(cur);
                }
            }
            for (auto i: tmp) {
                row[x].insert(i);
            }
        }
        if (c == 'S') {
            int cnt = 0;
            vector<int> tmp;
            while (cnt < r && sz(row[x]) > 0) {
                int cur = *row[x].rbegin();
                row[x].erase(--row[x].end());
                if (a[cur][x] > 0) {
                    a[cur][x]--; cnt++;
                    if (a[cur][x] > 0) {
                        tmp.pb(cur);
                    }
                }
            }
            for (auto i: tmp) {
                row[x].insert(i);
            }
        }
        /*
        for (int l = 1; l <= n; l++) {
            for (int j = 1; j <= m; j++) {
                cout << a[l][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
        */
    }
    /*for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;*/
    int ans = 0; p--;
    for (int i = 1; i + p <= n; i++) {
        for (int j = 1; j + p <= m; j++) {
            int sum = 0;
            for (int u = i; u <= i + p; u++) {
                for (int v = j; v <= j + p; v++) {
                    sum += a[u][v];
                }
            }
            ans = max(ans, sum);
        }
    }
    printf("%lld\n", ans);
    exit(false);
}

signed main() {
    // ios_base::sync_with_stdio(0);
    // in; out;  // cin >> TN;
    scanf("%lld %lld %lld %lld %lld", &n, &m, &r, &k, &p);
    a.resize(n + 1, vector<int>(m + 1, false));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%lld", &a[i][j]);
        }
    }
    bool ok = true;
    for (int i = 1; i <= k; i++) {
        // scanf("%c %lld %lld\n", &Q[i].fi, &Q[i].se.fi, &Q[i].se.se);
        cin >> Q[i].fi >> Q[i].se.fi >> Q[i].se.se;
        if (Q[i].se.se > 1) {
            ok = false;
        }
    }
    if (!ok) {
        subtask1();
    } else {
        subtask2();
    }
    return 0;
 }

Compilation message (stderr)

ufo.cpp:23:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable : 4996)
 
ufo.cpp: In function 'void subtask2()':
ufo.cpp:131:24: warning: variable 'h' set but not used [-Wunused-but-set-variable]
         char c; int x, h;
                        ^
ufo.cpp: In function 'int main()':
ufo.cpp:236:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld %lld %lld %lld %lld", &n, &m, &r, &k, &p);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ufo.cpp:240:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%lld", &a[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...