#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef BE_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
int n,m,d,e;
string s[500005];
int l,r;
int ans;
int type;
int level,zereg[30];
vector<int> v[30];
void build_tree(){
zereg[0] = 1;
for(int i = 0; i < 30; i++){
if(zereg[i] >= m){
level = i;
debug(level);
break;
}
zereg[i+1] = zereg[i] * 2;
}
for(int i = 0; i <= level; i++){
for(int j = 0; j < zereg[i]; j++){
v[i].pb(0);
}
}
}
void dfs(int k, int x){
int y = zereg[level-k] * x;
int z = zereg[level-k] * (x + 1) - 1;
if(l <= y && z <= r){
v[k][x] += type;
return;
}
if(z<l || y>r || k==level) return;
v[k+1][x*2] += v[k][x];
v[k+1][x*2+1] += v[k][x];
v[k][x] = 0;
dfs(k+1,x*2);
dfs(k+1,x*2+1);
}
int go(int pos){
int ret = 0;
for(int i = level; i >= 0; i--){
ret += v[i][pos];
pos /= 2;
}
return ret;
}
int main(){
IOS
cin >> n >> m >> d >> e; // distance / minimum number
for(int i=0; i<n; i++){
cin >> s[i];
}
build_tree();
for(int i = 0; i <= min(d,n-1); i++){
for(int j = 0; j < m; j++){
if(s[i][j] == 'S'){
l = max(0,j - d);
r = min(m-1, j + d);
type = 1;
dfs(0,0);
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(s[i][j] == 'M'){
int cnt = go(j);
if(cnt >= e){
ans++;
}
}
}
if(i-d >= 0){
for(int j = 0; j < m; j++){
if(s[i-d][j] == 'S'){
l = max(0,j - d);
r = min(m-1, j + d);
type = -1;
dfs(0,0);
}
}
}
if(i + d + 1 < n){
for(int j = 0; j < m; j++){
if(s[i + d + 1][j] == 'S'){
l = max(0,j - d);
r = min(m-1, j + d);
type = 1;
dfs(0,0);
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15856 KB |
Output is correct |
2 |
Correct |
9 ms |
15980 KB |
Output is correct |
3 |
Correct |
9 ms |
15964 KB |
Output is correct |
4 |
Correct |
11 ms |
15920 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15856 KB |
Output is correct |
2 |
Correct |
9 ms |
15980 KB |
Output is correct |
3 |
Correct |
9 ms |
15964 KB |
Output is correct |
4 |
Correct |
11 ms |
15920 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
9 ms |
15956 KB |
Output is correct |
7 |
Correct |
9 ms |
16112 KB |
Output is correct |
8 |
Correct |
8 ms |
15988 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15944 KB |
Output is correct |
2 |
Correct |
10 ms |
15984 KB |
Output is correct |
3 |
Correct |
10 ms |
15956 KB |
Output is correct |
4 |
Correct |
9 ms |
15980 KB |
Output is correct |
5 |
Correct |
9 ms |
15984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16340 KB |
Output is correct |
2 |
Correct |
10 ms |
16280 KB |
Output is correct |
3 |
Correct |
10 ms |
16468 KB |
Output is correct |
4 |
Correct |
11 ms |
16360 KB |
Output is correct |
5 |
Correct |
10 ms |
16352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
22112 KB |
Output is correct |
2 |
Correct |
31 ms |
22112 KB |
Output is correct |
3 |
Correct |
26 ms |
22240 KB |
Output is correct |
4 |
Correct |
96 ms |
22088 KB |
Output is correct |
5 |
Correct |
47 ms |
22080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15856 KB |
Output is correct |
2 |
Correct |
9 ms |
15980 KB |
Output is correct |
3 |
Correct |
9 ms |
15964 KB |
Output is correct |
4 |
Correct |
11 ms |
15920 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
9 ms |
15956 KB |
Output is correct |
7 |
Correct |
9 ms |
16112 KB |
Output is correct |
8 |
Correct |
8 ms |
15988 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15944 KB |
Output is correct |
12 |
Correct |
10 ms |
15984 KB |
Output is correct |
13 |
Correct |
10 ms |
15956 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
9 ms |
15984 KB |
Output is correct |
16 |
Correct |
10 ms |
16340 KB |
Output is correct |
17 |
Correct |
10 ms |
16280 KB |
Output is correct |
18 |
Correct |
10 ms |
16468 KB |
Output is correct |
19 |
Correct |
11 ms |
16360 KB |
Output is correct |
20 |
Correct |
10 ms |
16352 KB |
Output is correct |
21 |
Correct |
61 ms |
22112 KB |
Output is correct |
22 |
Correct |
31 ms |
22112 KB |
Output is correct |
23 |
Correct |
26 ms |
22240 KB |
Output is correct |
24 |
Correct |
96 ms |
22088 KB |
Output is correct |
25 |
Correct |
47 ms |
22080 KB |
Output is correct |
26 |
Correct |
24 ms |
17484 KB |
Output is correct |
27 |
Correct |
22 ms |
17012 KB |
Output is correct |
28 |
Correct |
52 ms |
17164 KB |
Output is correct |
29 |
Correct |
30 ms |
17192 KB |
Output is correct |
30 |
Correct |
19 ms |
17132 KB |
Output is correct |