#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
vector <vector <char>> a;
class Fenwick {
protected :
int R, C;
vector <vector <int>> fenwick;
public :
Fenwick(int r, int c) : R(r), C(c) {
fenwick.resize(r + 10, vector <int> (c + 10, 0));
}
int lsb(int x) {
return x & -x;
}
void update(int x, int y, int val) {
for(; x <= R; x += lsb(x)) {
for(int idx = y; idx <= C; idx += lsb(idx)) {
fenwick[x][idx] += val;
}
}
}
int query(int x, int y) {
int res = 0;
for(; x > 0; x -= lsb(x)) {
for(int idx = y; idx > 0; idx -= lsb(idx)) {
res += fenwick[x][idx];
}
}
return res;
}
};
int main() {
int R, C, D, K;
scanf(" %d %d %d %d", &R, &C, &D, &K);
a.resize(R + 10, vector <char> (C + 10));
for(int i = 1; i <= R; i++) {
for(int j = 1; j <= C; j++) {
scanf(" %c", &a[i][j]);
}
}
Fenwick fw(R, C);
for(int i = 1; i <= R; i++) {
for(int j = 1; j <= C; j++) {
if(a[i][j] == 'S') {
int min_x = max(1, i - D), max_x = min(R + 1, i + D + 1);
int min_y = max(1, j - D), max_y = min(C + 1, j + D + 1);
fw.update(min_x, min_y, 1);
fw.update(max_x, min_y, -1);
fw.update(min_x, max_y, -1);
fw.update(max_x, max_y, 1);
}
}
}
if(DEBUG) {
cout << "Debugging :\n";
for(int i = 1; i <= R; i++) {
for(int j = 1; j <= C; j++) {
cout << fw.query(i, j) << " ";
}
cout << endl;
}
}
int ans = 0;
for(int i = 1; i <= R; i++) {
for(int j = 1; j <= C; j++) {
if(a[i][j] == 'M' and fw.query(i, j) >= K) {
ans++;
}
}
}
cout << ans;
return 0;
}
Compilation message
mushrooms.cpp: In function 'int main()':
mushrooms.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf(" %d %d %d %d", &R, &C, &D, &K);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mushrooms.cpp:45:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf(" %c", &a[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1452 KB |
Output is correct |
2 |
Correct |
16 ms |
1476 KB |
Output is correct |
3 |
Correct |
12 ms |
1836 KB |
Output is correct |
4 |
Correct |
10 ms |
1484 KB |
Output is correct |
5 |
Correct |
11 ms |
1580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
29624 KB |
Output is correct |
2 |
Correct |
37 ms |
29628 KB |
Output is correct |
3 |
Correct |
42 ms |
29636 KB |
Output is correct |
4 |
Correct |
51 ms |
29624 KB |
Output is correct |
5 |
Correct |
39 ms |
29616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
1 ms |
280 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
288 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
12 ms |
1452 KB |
Output is correct |
17 |
Correct |
16 ms |
1476 KB |
Output is correct |
18 |
Correct |
12 ms |
1836 KB |
Output is correct |
19 |
Correct |
10 ms |
1484 KB |
Output is correct |
20 |
Correct |
11 ms |
1580 KB |
Output is correct |
21 |
Correct |
43 ms |
29624 KB |
Output is correct |
22 |
Correct |
37 ms |
29628 KB |
Output is correct |
23 |
Correct |
42 ms |
29636 KB |
Output is correct |
24 |
Correct |
51 ms |
29624 KB |
Output is correct |
25 |
Correct |
39 ms |
29616 KB |
Output is correct |
26 |
Correct |
42 ms |
5936 KB |
Output is correct |
27 |
Correct |
35 ms |
3236 KB |
Output is correct |
28 |
Correct |
68 ms |
3928 KB |
Output is correct |
29 |
Correct |
53 ms |
3780 KB |
Output is correct |
30 |
Correct |
33 ms |
5220 KB |
Output is correct |