• AtCoder Grand Contest 015 C.Nuske vs Phantom Thnook


    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

    • 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≤iQ)
    • 1≤yi,1≤yi,2≤M(1≤iQ)

    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

    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

    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

    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

    3
    2
    1
    1
    3
    2

    看了题解才会做,以前只遇到过求整个连通块数量,求局部还是第一次。题解的意思是:
    连通块数量=blue数量-水平方向相邻blue对的数量-垂直方向相邻blue对的数量

    用二维前缀和构造一下,注意下细节即可。
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)>(y)?(y):(x))
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1<<28;
    const int maxn=2e3+10;
    const int maxm=110;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    int n,m,q;
    int mapp[maxn][maxn];
    int sum[maxn][maxn];
    int xsum[maxn][maxn];
    int ysum[maxn][maxn];
    int sfunc(int x1,int y1,int x2,int y2)
    {
        return sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];
    }
    int xfunc(int x1,int y1,int x2,int y2)
    {
        return xsum[x2][y2]-xsum[x2][y1]-xsum[x1-1][y2]+xsum[x1-1][y1];
    }
    int yfunc(int x1,int y1,int x2,int y2)
    {
        return ysum[x2][y2]-ysum[x2][y1-1]-ysum[x1][y2]+ysum[x1][y1-1];
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d %d %d",&n,&m,&q))
        {
            for(int i=1;i<=n;i++)
            {
                char s[maxn];
                scanf("%s",s);
                for(int j=1;j<=m;j++)
                    mapp[i][j]=s[j-1]-'0';
            }
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                {
                    sum[i][j]=mapp[i][j]+sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1];
                    xsum[i][j]=(mapp[i][j-1]==1&&mapp[i][j]==1)+xsum[i][j-1]+xsum[i-1][j]-xsum[i-1][j-1];
                    ysum[i][j]=(mapp[i-1][j]==1&&mapp[i][j]==1)+ysum[i][j-1]+ysum[i-1][j]-ysum[i-1][j-1];
                }
            while(q--)
            {
                int x1,y1,x2,y2;
                scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                printf("%d
    ",sfunc(x1,y1,x2,y2)-xfunc(x1,y1,x2,y2)-yfunc(x1,y1,x2,y2));
            }
        }
        return 0;
    }
     
  • 相关阅读:
    IOS面试题
    iOS开发多线程篇—线程的状态
    iOS开发多线程篇—GCD介绍
    iOS开发多线程篇—线程间的通信
    iOS开发多线程篇—线程安全
    iOS开发多线程篇—创建线程
    iOS开发多线程篇—多线程简单介绍
    iOS开发UI篇—Quartz2D使用(绘制基本图形)
    iOS开发UI篇—Quartz2D(自定义UIImageView控件)
    iOS开发UI篇—核心动画(UIView封装动画)
  • 原文地址:https://www.cnblogs.com/mgz-/p/6920177.html
Copyright © 2020-2023  润新知