您的位置:首页 > 其它

AtCoder Grand Contest 015 C.Nuske vs Phantom Thnook

2017-05-28 17:14 489 查看


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,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

4000
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)

Input

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


Output

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

Sample Input 1

Copy
3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4


Sample Output 1

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.

Sample Input 2

Copy
5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4


Sample Output 2

Copy
3
2
1
1
3
2

题目大意: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
[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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: