Problem - D - Codeforces
思路:这个题就是要维护每个位置被修改了几次,但是一直没想到一个好的方法,一直在关注这个点对下面的点产生的影响,但是其实我们可以维护这个点能够由那几个点影响,其实就是一个以x,y为下顶点的倒三角形
能够对这个点产生影响的其实就是这个倒三角形,并且这个倒三角形是可以递推的,当前这个点被翻转的次数是,sum[i-1][j]+l[i-1][j-1]+r[i-1][j+1]
其实就是它上一层的倒三角形+左右两条直线的
// Problem: D. Matrix Cascade
// Contest: Codeforces - Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1864/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}
int T,hackT;
int n,m,k;
char str[3010][3010];
int l[3010][3010];
int r[3010][3010];
int sum[3010][3010];
void solve() {
n=read();
for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
for(int i=1;i<=n+4;i++) {
for(int j=1;j<=n+4;j++) {
sum[i][j]=l[i][j]=r[i][j]=0;
}
}
int res=0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
int temp=sum[i-1][j]+l[i-1][j-1]+r[i-1][j+1];
if(temp%2==1) str[i][j]=((str[i][j]-'0')^1)+'0';
if(str[i][j]=='1') {
res++;
sum[i][j]=temp+1;
l[i][j]=l[i-1][j-1]+1;
r[i][j]=r[i-1][j+1]+1;
} else {
sum[i][j]=temp;
l[i][j]=l[i-1][j-1];
r[i][j]=r[i-1][j+1];
}
}
}
printf("%d\n",res);
}
int main() {
// init();
// stin();
// ios::sync_with_stdio(false);
scanf("%d",&T);
// T=1;
while(T--) hackT++,solve();
return 0;
}