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;
    }
  • 相关阅读:
    Leetcode 122. Best Time to Buy and Sell Stock II
    Leetcode 337. House Robber III
    Leetcode 213. House Robber II
    java 常用正则表达式总结
    eclipse异常解决:Errors occurred during the build.........
    关于匿名内部类的理解!
    接口/抽象类的思考
    IO流对文件的读取操作
    标准类的建立,以及用集合对标准类中的元素进行存取操作
    利用集合进行对元素的添加和删除
  • 原文地址:https://www.cnblogs.com/csushl/p/9386578.html
Copyright © 2011-2022 走看看