#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vi vector<long long>
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pr pair<ll, ll>
#define ff first
#define ss second
void solution();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
ll r, c, d, k;
struct SegTree {
vi lzy, tree;
ll n;
SegTree () {
n = c;
lzy.resize(4*n);
tree.resize(4*n);
}
void push(int id, int l, int r) {
if (lzy[id] == 0) return;
tree[id] += lzy[id]*(r-l+1);
if (l != r) {
lzy[2*id] += lzy[id];
lzy[2*id+1] += lzy[id];
}
lzy[id] = 0;
}
void update(int id, int l, int r, int L, int R, int val = 1) {
push(id, l, r);
if (L > r || l > R) return;
if (L <= l && R >= r) {
lzy[id] += val;
push(id, l, r);
return;
}
int mid = (l + r)/2;
update(2*id, l, mid, L, R, val);
update(2*id+1, mid+1, r, L, R, val);
tree[id] = tree[2*id] + tree[2*id+1];
}
ll query(int id, int l, int r, int pos) {
push(id, l, r);
if (pos < l || pos > r) return 0;
if (l == r) return tree[id];
int mid = (l + r)/2;
return query(2*id, l, mid, pos) + query(2*id+1, mid+1, r, pos);
}
void update(int l, int r, int val = 1) {
l = max(0, l);
r = min(r, int(c-1));
update(1, 0, n-1, l, r, val);
}
ll query(int pos) {return query(1, 0, n-1, pos);}
};
void ad(pr p, vector<SegTree> &m) {
int t = max(0ll, p.ff-d);
int b = min(r-1, p.ff+d)+1;
int lf = max(0ll, p.ss - d);
int ri = min(c-1, p.ss+d);
m[t].update(lf, ri);
m[b].update(lf, ri, -1);
}
void solution() {
cin >> r >> c >> d >> k;
vector<string> a(r);
for (string &z : a) cin >> z;
vector<SegTree> m(r+1);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (a[i][j] == 'S') {
ad({i, j}, m);
}
}
}
vector<vi> arr(r, vi(c));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = m[i].query(j);
}
}
for (int j = 0; j < c; j++) {
for (int i = 1; i < r; i++) {
arr[i][j] += arr[i-1][j];
}
}
ll ans = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (a[i][j] == 'M' && arr[i][j] >= k) ans++;
}
}
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |