This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 4e6 + 10;
const int MOD = 1e9 + 7;
#define int int
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) { // bigmod
if(p==0) return 1;
int r = bm(b, p/2);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) { // modinv
return bm(b, MOD-2);
}
int f[MAXN];
int nCr(int n, int r) { // nCr main function, requires precomputation function
int ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() { // factorials for nCr, function MUST BE CALLED
f[0] = 1;
for(int i=1; i<MAXN; i++) f[i] = (f[i-1] * i) % MOD;
}
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
void solve(int tc) {
int n, m;
cin >> n >> m;
for(int i=1; i<=n*m; i++) dsu[i] = i;
char a[n+1][m+1];
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin >> a[i][j];
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(i < n && a[i][j] == '.' && a[i+1][j] == '.' && set_of((i-1) * m + j) != set_of(i * m + j)) union_((i-1) * m + j, i * m + j);
if(j < m && a[i][j] == '.' && a[i][j+1] == '.' && set_of((i-1) * m + j) != set_of((i-1) * m + j + 1)) union_((i-1) * m + j, (i-1) * m + j + 1);
}
}
int sz[n * m + 1];
for(int i=1; i<=n * m; i++) sz[i] = 0;
for(int i=1; i<=n * m; i++) sz[set_of(i)]++;
int plus[n+2][m+2], minus[n+2][m+2];
int hor[n+2][m+2], ver[n+2][m+2];
for(int i=0; i<=n+1; i++) {
for(int j=0; j<=m+1; j++) {
plus[i][j] = minus[i][j] = hor[i][j] = ver[i][j] = 0;
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
plus[i][j] = plus[i-1][j+1] + (a[i][j] == '#');
minus[i][j] = minus[i-1][j-1] + (a[i][j] == '#');
hor[i][j] = hor[i][j-1] + (a[i][j] == '#');
ver[i][j] = ver[i-1][j] + (a[i][j] == '#');
}
}
int ans = 0;
for(int i=2; i<n; i++) {
for(int j=2; j<m; j++) {
if(a[i][j] == '#') continue;
int lb = 0, rb = j;
int len;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(hor[i][j] - hor[i][mid] == 0) rb = mid - 1;
else lb = mid;
}
if(hor[i][j] - hor[i][lb] == 1) {
len = j - lb - 1; // dist between corner and centre
}
if(j + len > m) continue;
if(i + len > n) continue;
if(i - len < 1) continue;
if(hor[i][j + len] - hor[i][j] == 1 && a[i][j + len] == '#') {
if(ver[i + len][j] - ver[i][j] == 1 && a[i + len][j] == '#') {
if(ver[i - 1][j] - ver[i - len - 1][j] == 1 && a[i - len][j] == '#') {
if(a[i][j - len] == '#') {
if(minus[i][j + len] - minus[i - len][j] == len) {
if(plus[i + len][j] - plus[i][j + len] == len) {
if(minus[i + len][j] - minus[i][j - len] == len) {
if(plus[i][j - len] - plus[i - len][j] == len) {
if(sz[set_of((i-1) * m + j)] == len*len + (len-1) * (len-1)) {
ans++;
}
}
}
}
}
}
}
}
}
}
}
cout << ans << "\n";
}
int32_t main(){
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message (stderr)
Main.cpp: In function 'void solve(int)':
Main.cpp:93:19: warning: 'len' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | if(minus[i + len][j] - minus[i][j - len] == len) {
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |