• AtCoder Grand Contest 015(C


    花样式过DFS题

    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

    • 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 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.

    题意是求:(x1,y1)到(x2,y2)有多少个连成的块,由于数据过大,直接DFS肯定会超时,在网上看到一种解法好6,就是求出(x1,y1)到(x2,y2)之间的点,行边,列边。边是由两个正方形组成的。把点减去边数就是答案了。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N = 2e3+10;
     4 char a[N][N];
     5 int vectex[N][N], edgeH[N][N], edgeW[N][N];
     6 
     7 int main(){
     8     int n, m, q;
     9     scanf("%d %d %d
    ",&n,&m,&q);
    10     for(int i = 0; i < n; i ++){
    11         scanf("%s",a[i]);
    12     }
    13     for(int i = 0; i < n; i ++){
    14         for(int j = 0; j < m; j ++){
    15             vectex[i+1][j+1] = (a[i][j] == '1') + vectex[i+1][j] + vectex[i][j+1] -vectex[i][j];
    16             edgeH[i+1][j+1] = (a[i+1][j] == '1' && a[i][j] == '1') + edgeH[i+1][j] + edgeH[i][j+1] - edgeH[i][j];
    17             edgeW[i+1][j+1] = (a[i][j+1] == '1' && a[i][j] == '1') + edgeW[i+1][j] + edgeW[i][j+1] - edgeW[i][j];
    18         }
    19     }
    20     while(q--){
    21         int x1, x2, y1, y2;
    22         scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    23         x1--;y1--;
    24         int vec = vectex[x2][y2] - vectex[x2][y1] - vectex[x1][y2] + vectex[x1][y1];
    25         int edg = edgeH[x2-1][y2] - edgeH[x2-1][y1] - edgeH[x1][y2] + edgeH[x1][y1] +
    26                     edgeW[x2][y2-1] - edgeW[x2][y1] - edgeW[x1][y2-1] + edgeW[x1][y1];
    27         printf("%d
    ",vec - edg);
    28     }
    29     return 0;
    30 }
  • 相关阅读:
    HDU1864--01背包
    HDU4508--完全背包
    HDU5410--01背包+完全背包
    HDU1284--完全背包
    HDU1248--完全背包
    HDU2191--多重背包(二进制分解+01背包)
    HDU2186--水
    PAT乙级--1003
    51Nod--1006 lcs
    51Nod--1008
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6915292.html
Copyright © 2020-2023  润新知