题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_c
题目大意:
现有一个\(N×M\)的矩阵\(S\),若\(S_{i,j}=1\),则该处为蓝色,否则为白色。保证蓝色格子构成的联通块为树,即联通块内蓝格子相邻的边为\(cnt-1\),多次询问子矩阵内蓝色联通块个数
题目都保证是棵树了就是个SB题,二维前缀和,记录蓝块数目,相邻蓝块个数(行列分开记好写些),子矩阵内用蓝块个数减去相邻蓝块个数即为联通块个数
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=2e3;
char map[N+10][N+10];
int sum[N+10][N+10],lne[N+10][N+10],row[N+10][N+10];
int main(){
int n=read(),m=read(),q=read();
for (int i=1;i<=n;i++){
scanf("%s",map[i]+1);
for (int j=1;j<=m;j++){
if (map[i][j]=='1'){
sum[i][j]=1;
if (map[i][j-1]=='1') lne[i][j]=1;
if (map[i-1][j]=='1') row[i][j]=1;
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
lne[i][j]+=lne[i-1][j]+lne[i][j-1]-lne[i-1][j-1];
row[i][j]+=row[i-1][j]+row[i][j-1]-row[i-1][j-1];
}
}
for (int i=1;i<=q;i++){
int x1=read(),y1=read(),x2=read(),y2=read(),Ans=0;
Ans+=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
Ans-=lne[x2][y2]-lne[x1-1][y2]-lne[x2][y1]+lne[x1-1][y1];
Ans-=row[x2][y2]-row[x1][y2]-row[x2][y1-1]+row[x1][y1-1];
printf("%d\n",Ans);
}
return 0;
}