AtCoder Grand Contest 015 C.Nuske vs Phantom Thnook

来源:互联网 发布:股海网指标公式源码 编辑:程序博客网 时间:2024/06/13 21:52

C - Nuske vs Phantom Thnook


Time limit : 4sec / Memory limit : 256MB

Score : 700 points

Problem Statement

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,1yi,1xi,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

  • 1N,M2000
  • 1Q200000
  • Si,j is either 0 or 1.
  • Si,j satisfies the condition explained in the statement.
  • 1xi,1xi,2N(1iQ)
  • 1yi,1yi,2M(1iQ)

Input

The input is given from Standard Input in the following format:

N M QS1,1..S1,M:SN,1..SN,Mx1,1 yi,1 xi,2 yi,2:xQ,1 yQ,1 xQ,2 yQ,2

Output

For each query, print the number of the connected components consisting of blue squares in the region.


Sample Input 1

Copy
3 4 41101011011011 1 3 41 1 3 12 2 3 41 2 2 4

Sample Output 1

Copy
3222

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.


Sample Input 2

Copy
5 5 611010011101010111101010101 1 5 51 2 4 52 3 3 43 3 3 33 1 3 51 1 3 4

Sample Output 2

Copy
321132
题目大意:n*m的矩阵中存在01点,一共有Q次查询,每次询问的时候查询一个子矩阵中存在着1的连通块的数量
解题思路:利用了树的原理,两个相邻的1点相连接,可以找到多颗树,每次查询统计出子矩阵中1点的个数,和边的个数,相减可求出连通块的个数,原理是点减去边就是树的个数
#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map> #define inf 9999999; using namespace std; int n,m,T,i,j,ans,xx,x,yy,y,k;int a[2005][2005],b[2005][2005],c[2005][2005];char tu[2005][2005];int main(){cin>>n>>m>>T;for(i=1;i<=n;i++)for(j=1;j<=m;j++)cin>>tu[i][j];memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));//cout<<1111111<<endl;for(i=1;i<=n;i++){for(j=1;j<=m;j++){a[i][j]=a[i][j-1]+a[i-1][j]-a[i-1][j-1];b[i][j]=b[i][j-1]+b[i-1][j]-b[i-1][j-1];c[j][i]=c[j][i-1]+c[j-1][i]-c[j-1][i-1];if(tu[i][j]=='1')a[i][j]++;if(tu[i][j-1]=='1'&&tu[i][j]=='1')b[i][j]++;if(tu[i-1][j]=='1'&&tu[i][j]=='1')c[j][i]++; //cout<<"i: "<<i<<" j: "<<j<<" b[i][j]"<<b[i][j]<<endl;}}//cout<<b[n][m]<<endl;//cout<<111111111<<endl;while(T--){cin>>x>>y>>xx>>yy;k=a[xx][yy]-a[x-1][yy]+a[x-1][y-1]-a[xx][y-1];//cout<<"k "<<k<<endl;ans=b[xx][yy]-b[xx][y]-b[x-1][yy]+b[x-1][y];//cout<<"ans "<<ans<<endl;ans+=c[yy][xx]-c[yy][x]-c[y-1][xx]+c[y-1][x];//cout<<"1 :"<<ans<<endl;cout<<k-ans<<endl;}}


原创粉丝点击