zoukankan      html  css  js  c++  java
  • ARTWORK

    Description

    A template for an artwork is a white grid of n × m squares. The artwork will be created by painting q horizontal and vertical black strokes. A stroke starts from square (x1, y1), ends at square (x2,y2)(x1=x2(x2,y2)(x1=x2 or y1=y2)y1=y2) and changes the color of all squares (x, y) to black where x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. The beauty of an artwork is the number of regions in the grid. Each region consists of one or more white squares that are connected to each other using a path of white squares in the grid, walking horizontally or vertically but not diagonally. The initial beauty of the artwork is 1. Your task is to calculate the beauty after each new stroke. Figure A.1 illustrates how the beauty of the artwork varies in Sample Input 1.

    Input

    The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 1000, 1 ≤ q ≤ 104). Then follow q lines that describe the strokes. Each line consists of four integersx1,y1,x2x1,y1,x2 and y2(1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). Either x1 = x2 or y1=y2y1=y2(or both).

    Output

    For each of the q strokes, output a line containing the beauty of the artwork after the stroke.

    Sample Input

    4 6 5
    2 2 2 6
    1 3 4 3
    2 5 3 5
    4 6 4 6
    1 6 4 6

    Sample Output

    1
    3
    3
    4
    3

    代码为:

    #include<iostream>
    #include<cstdio> 
    #include<cstring>
    using namespace std;
    
    
    int n,m,q;
    
    
    struct node{
        int x1,y1,xx,yy;
    } que[10005];
    
    
    int num[1000005];
    int f[1000005];
    
    
    int find(int x)
    {
        return f[x]==x?x:f[x]=find(f[x]);
    }
    
    
    int cnt;
    void merge(int x,int y)
    {
        int fx=find(x);
        int fy=find(y);
        if(fx==fy) 
    return;
        cnt--; 
    f[fx]=fy;
    }
    
    
    int Get_id(int x,int y)
    {
         return x*m+y;
    }
    
    
    int ans[10005];
    
    
    int dfsx[4]={0,0,1,-1};
    int dfsy[4]={1,-1,0,0};
    
    
    bool in(int x,int y)
    {
        return x>=0&&y>=0&&x<n&&y<m;
    }
    void work(int x,int y)
    {
        for(int k=0;k<4;k++)
    {
            int tx=x+dfsx[k];
            int ty=y+dfsy[k];
            if(!in(tx,ty)||num[Get_id(tx,ty)]) 
      continue;
            merge(Get_id(tx,ty),Get_id(x,y));
        }
    }
    int main()
    {
        cin>>m>>n>>q;
        cnt=n*m;
        
        for(int i=0;i<n*m;i++) 
    f[i]=i,num[i]=0;
        for(int i=0;i<q;i++)
    {
            cin>>que[i].y1>>que[i].x1>>que[i].yy>>que[i].xx;
            que[i].x1--; 
    que[i].y1--;
            que[i].xx--; 
    que[i].yy--;
            for(int x=que[i].x1;x<=que[i].xx;x++)
            {
    for(int y=que[i].y1;y<=que[i].yy;y++)
    {
               if(num[Get_id(x,y)]==0) cnt--;
                num[Get_id(x,y)]++;
            }
            }
        }
        for(int i=0;i<n;i++)
    {
            for(int j=0;j<m;j++)
       {
                if(num[Get_id(i,j)]) 
      continue;
                work(i,j);
            }
        }
        for(int i=q-1;i>=0;i--)
    {
            ans[i]=cnt;
            for(int x=que[i].x1;x<=que[i].xx;x++)
            for(int y=que[i].y1;y<=que[i].yy;y++)
    {
                num[Get_id(x,y)]--;
                if(num[Get_id(x,y)]==0) 
    {
                    cnt++;
                    work(x,y);
                }
            }
        }
    
    
        for(int i=0;i<q;i++)
            cout<<ans[i]<<endl;
    
    
       return 0;
    }
  • 相关阅读:
    中译英26
    listen 59
    Speaking 1
    listen 58
    listen 57
    中译英25
    listen 56
    2018.2.27 RF module distance test part I
    中译英24
    第二章、PyQt5应用构建详细过程介绍
  • 原文地址:https://www.cnblogs.com/csushl/p/9386578.html
Copyright © 2011-2022 走看看