挂个写时候的思路。
考虑 Ll,r,Rl,r 为对于 (l,r) 所能延伸到最左边的 (l,Ll,r) 和最右边的 (l,Rl,r)。gl,r 为 (l,r) 所能延伸到最右上的 (l−gl,r,r+gl,r)。
倘若枚举 l,r 为左下角,于是答案只可能在 1∼min(Rl,r,gl,r) 中出现。而对于每个可能的答案 i,又需要保证 Ll−i,r+i≤l,这个可以枚举每条斜线二位数点做。
复杂度 O(n2logn)。
#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <random>
#include <ctime>
#include <queue>
#include <map>
#include <set>
using namespace std;
// #define int long long
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << (x) << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
}
template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s = s * w;
}
template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
read(x), read(arp...);
}
template <typename T>
inline void write(T x, char ch = '\n') {
if (x < 0) x = -x, putchar('-');
static char stk[25];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top) putchar(stk[--top]);
putchar(ch);
return ;
}
template <typename T>
inline void smax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void smin(T &x, T y) {
if (x > y) x = y;
}
void quit() {
exit(0);
}
} using namespace FastIO;
const int N = 3030;
int L[N][N], R[N][N], n, m, ans[N];
char c[N][N]; pii Q[N];
struct BIT {
int c[N];
void clear() { memset(c, 0, sizeof c); }
void add(int x, int s) {
while (x < N) {
c[x] += s; x += lowbit(x);
}
}
int ask(int x) {
int ans = 0;
while (x) {
ans += c[x]; x -= lowbit(x);
} return ans;
}
int query(int l, int r) { return ask(r)-ask(l-1); }
} bit;
signed main() {
read(n, m);
for (int i = 1; i <= n; ++i) scanf("%s", c[i]+1);
for (int i = 1; i <= n; ++i) {
for (int j = 1, last = 0; j <= m; ++j) {
if (c[i][j] == '.') last = j;
L[i][j] = last+1;
}
for (int j = m, last = m+1; j; --j) {
if (c[i][j] == '.') last = j;
R[i][j] = last-1;
}
} int sum = 0;
for (int i = 1; i <= m; ++i) {
bit.clear();
int x = 1, y = i, len = 0, lst = i;
while (x <= n && y) {
if (c[x][y] == 'z') Q[++len] = {min(lst, R[x][y]), y};
else lst = y-1;
x++; y--;
} sort(Q+1,Q+1+len,greater<pii>()); Q[len+1].F=0;
x = 1; y = i; int l=1,r;
while (x <= n && y) {
for (r = l; Q[r].F == y; ++r);
for (int k = l; k < r; ++k) sum += -bit.query(1, Q[k].S);
if (c[x][y] == 'z') { bit.add(L[x][y], 1); sum += bit.query(1, y); }
l = r;
x++; y--;
}
}
for (int i = 2; i <= n; ++i) {
bit.clear();
int x = i, y = m, len = 0, lst = i;
while (x <= n && y) {
if (c[x][y] == 'z') Q[++len] = {min(lst, R[x][y]), y};
else lst = y-1;
x++; y--;
} sort(Q+1,Q+1+len,greater<pii>()); Q[len+1].F=0;
x = i; y = m; int l=1,r;
while (x <= n && y) {
for (r = l; Q[r].F == y; ++r);
for (int k = l; k < r; ++k) sum += -bit.query(1, Q[k].S);
if (c[x][y] == 'z') { bit.add(L[x][y], 1); sum += bit.query(1, y); }
l = r;
x++; y--;
}
} write(sum);
return 0;
}