大意: 给定01矩阵, 单点赋值为1, 求最大全0正方形.
将询问倒序处理, 那么答案一定是递增的, 最多增长$O(n)$次, 对于每次操作暴力判断答案是否增长即可, 也就是说转化为判断是否存在一个边长$x$的正方形包含给定点, 可以维护左右两侧第一个1的位置, 从上往下滑动窗口即可$O(n)$判断, 总复杂度$O(n^2)$
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} //head const int N = 2e3+10; int n, m, k, ans, Ans[N]; char s[N][N]; int dp[N][N], L[N][N], R[N][N], x[N], y[N]; void upd(int i) { REP(j,1,m) if (s[i][j]=='.') L[i][j] = L[i][j-1]?L[i][j-1]:j; PER(j,1,m) if (s[i][j]=='.') R[i][j] = R[i][j+1]?R[i][j+1]:j; } void DP() { REP(i,1,n) REP(j,1,m) if (s[i][j]=='.') { dp[i][j] = min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]})+1; ans = max(ans,dp[i][j]); } REP(i,1,n) upd(i); } int chk(int U, int D, int y, int v) { if (D-U+1<v) return 0; deque<int> q1, q2; REP(i,U,D) { if (q1.size()&&i-q1[0]==v) q1.pop_front(); if (q2.size()&&i-q2[0]==v) q2.pop_front(); while (q1.size()&&L[i][y]>=L[q1.back()][y]) q1.pop_back(); while (q2.size()&&R[i][y]<=R[q2.back()][y]) q2.pop_back(); q1.push_back(i), q2.push_back(i); if (i-U+1>=v&&R[q2[0]][y]-L[q1[0]][y]+1>=v) return 1; } return 0; } int main() { scanf("%d%d%d", &n, &m, &k); REP(i,1,n) scanf("%s", s[i]+1); REP(i,1,k) { scanf("%d%d", x+i, y+i); s[x[i]][y[i]] = 'X'; } DP(); PER(i,1,k) { Ans[i] = ans; s[x[i]][y[i]] = '.', upd(x[i]); int U = x[i], D = x[i]; while (s[U][y[i]]=='.') --U; ++U; while (s[D][y[i]]=='.') ++D; --D; while (chk(U,D,y[i],ans+1)) ++ans; } REP(i,1,k) printf("%d ", Ans[i]); }