题目描述
Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If Si,j is 1, the square at the i-th row and j-th column is blue; if Si,j is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.
Phantom Thnook, Nuske’s eternal rival, gives Q queries to Nuske. The i-th query consists of four integers xi,1, yi,1, xi,2 and yi,2 and asks him the following: when the rectangular region of the grid bounded by (and including) the xi,1-th row, xi,2-th row, yi,1-th column and yi,2-th column is cut out, how many connected components consisting of blue squares there are in the region?
Process all the queries.
Constraints
1≤N,M≤2000
1≤Q≤200000
Si,j is either 0 or 1.
Si,j satisfies the condition explained in the statement.
1≤xi,1≤xi,2≤N(1≤i≤Q)
1≤yi,1≤yi,2≤M(1≤i≤Q)
输入
The input is given from Standard Input in the following format:
N M Q
S1,1…S1,M
:
SN,1…SN,M
x1,1 yi,1 xi,2 yi,2
:
xQ,1 yQ,1 xQ,2 yQ,2
输出
For each query, print the number of the connected components consisting of blue squares in the region.
样例输入 Copy
3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4
样例输出 Copy
3
2
2
2
提示
In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.
In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed. Note that squares that belong to the same component in the original grid may belong to different components.
题意:
给定一个n∗m的网格图。每个格子有0/1两种颜色,保证任意两个1的格子最多有一条路径连接它们.
每次询问一个矩形区域内1格子的联通块的数目。
题目中所有联通都是四联块
解析:
而对于森林来说,联通块的个数=点数-边数.
用二维前缀和维护。
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
const int N=2500;
char g[N][N];
int n,m,q;
int cnt[N][N];
int sum[N][N];//点数
int cnt_x[N][N];//横着连起来的边
int cnt_y[N][N] ;//竖着连起来的边
void init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cnt[i][j]=g[i][j-1]-'0';
}
}
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]+cnt[i][j];
if(cnt[i][j]==1&&cnt[i-1][j]==1) cnt_y[i][j]=1;
else cnt_y[i][j]=0;
if(cnt[i][j]==1&&cnt[i][j-1]==1) cnt_x[i][j]=1;
else cnt_x[i][j]=0;
cnt_x[i][j]+=cnt_x[i-1][j]+cnt_x[i][j-1]-cnt_x[i-1][j-1];
cnt_y[i][j]+=cnt_y[i-1][j]+cnt_y[i][j-1]-cnt_y[i-1][j-1];
}
}
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=n;i++) cin>>g[i] ;
init();
int x1,x2,y1,y2;
while(q--)
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
int ans=sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];
//这边有个地方要注意下我们在求每一行连通块的边数时,是拿j和j-1去作比较。最终只会加在j这个位置上,所以求得时候y1+1.同理求每一列得连通块得边数时x1+1
ans-=cnt_x[x2][y2]-cnt_x[x2][y1]-cnt_x[x1-1][y2]+cnt_x[x1-1][y1];
ans-=cnt_y[x2][y2]-cnt_y[x2][y1-1]-cnt_y[x1][y2]+cnt_y[x1][y1-1];
printf("%d\n",ans);
}
}