Submission #586315

#TimeUsernameProblemLanguageResultExecution timeMemory
586315JomnoiCollecting Mushrooms (NOI18_collectmushrooms)C++17
100 / 100
40 ms29728 KiB
#include <bits/stdc++.h>
using namespace std;

int R, C;
vector <vector <char>> s;
vector <vector <int>> fenwick;

void update(int x, int y, int v) {
    for(int i = x; i <= R; i += (i & -i)) {
        for(int j = y; j <= C; j += (j & -j)) {
            fenwick[i][j] += v;
        }
    }
}

int query(int x, int y) {
    int res = 0;
    for(int i = x; i >= 1; i -= (i & -i)) {
        for(int j = y; j >= 1; j -= (j & -j)) {
            res += fenwick[i][j];
        }
    }
    return res;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int D, K;
    cin >> R >> C >> D >> K;

    s.resize(R + 10, vector <char> (C + 10));
    for(int i = 1; i <= R; i++) {
        for(int j = 1; j <= C; j++) {
            cin >> s[i][j];
        }
    }

    fenwick.resize(R + 10, vector <int> (C + 10, 0));
    for(int i = 1; i <= R; i++) {
        for(int j = 1; j <= C; j++) {
            if(s[i][j] == 'S') {
                int minx = max(1, i - D), miny = max(1, j - D);
                int maxx = min(R, i + D), maxy = min(C, j + D);

                update(minx, miny, 1);
                update(minx, maxy + 1, -1);
                update(maxx + 1, miny, -1);
                update(maxx + 1, maxy + 1, 1);
            }
        }
    }

    int ans = 0;
    for(int i = 1; i <= R; i++) {
        for(int j = 1; j <= C; j++) {
            if(s[i][j] == 'M' and query(i, j) >= K) {
                ans++;
            }
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...