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,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)
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
3 4 41101011011011 1 3 41 1 3 12 2 3 41 2 2 4
Sample Output 1
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
5 5 611010011101010111101010101 1 5 51 2 4 52 3 3 43 3 3 33 1 3 51 1 3 4
Sample Output 2
321132
#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;}}
- AtCoder Grand Contest 015 C.Nuske vs Phantom Thnook
- 【AtCoder Grand 015C】Nuske vs Phantom Thnook 题解
- [AtCoder][杂题]AGC015 . C .Nuske vs Phantom Thnook
- AtCoder:Nuske vs Phantom Thnook(思维 & 树)
- [agc015c]Nuske vs Phantom Thnook
- AGC 015C Nuske vs Phantom Thnook 思维+二维前缀和
- Atcoder Grand Contest 013C
- AtCoder Grand Contest 017 C
- 【AtCoder】 Grand Contest 018 C
- AtCoder Grand Contest 019 C
- (AtCoder Grand Contest 019) C
- 题解Atcoder Grand Contest C
- AtCoder Grand Contest 001 C
- AtCoder Grand Contest 010
- AtCoder Grand Contest 011
- AtCoder Grand Contest 018
- AtCoder Grand Contest 018
- Atcoder Grand Contest 019
- python的 @staticmethod,@classmethod和@property的区别
- Pushing the Limits of Deep CNNs for Pedestrian Detection
- HDU2222 AC自动机 入门模板
- HDU1827 Summer Holiday
- Linux总结笔记9-搭建Postfix邮件服务器
- AtCoder Grand Contest 015 C.Nuske vs Phantom Thnook
- angularJs中ng-model-options设置数据同步
- (为什么要学习) AsyncTask 原理
- 算法题的五种解法
- jquery中each的使用方法
- 1.5 双 z 变换法
- Action获取请求参数方法
- 人工智能------知识表示
- 回顾