Ncpc2016-a-artwork

Source: Internet
Author: User

The topic describes a template for an artwork is a white grid of NXM squares. The artwork is created by painting Q horizontal and vertical black strokes. A stroke starts from square (x 1, y 1), ends at square (x 2, y 2) (x 1 = x 2 or y 1 = y 2) and changes the color of a ll squares (x, y) to Black where
X 1≤x≤x 2 and y 1≤y≤y 2.

The beauty of an artwork are the number of regions in the grid. Each region consists of one or more white squares that is connected to each other using a path of white squares in the GR ID, 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.

Enter the? RST 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 integers x 1, y 1, x 2 and y 2 (1≤x 1≤x 2≤n, 1≤y 1≤y 2≤m). either x 1 = x 2 or y 1 = y 2 (or both). Output for each of the Q strokes, outputs a line containing the beauty of the artwork AF ter the stroke. Sample input
4 6 52 2 2 61 3 4 32 5 3 54 6 4 61 6 4 6
Sample output
13343
Test instructions is to give you a nm matrix, q times, each time will be a continuous number of vertical or horizontal lattice dyed black, ask each step after the number of white Unicom block from the last situation forward, first find out all the operation after the number of white Unicom block, and then delete the black line, on the new appearance of the white lattice, Either connected to one of the original Unicom blocks, or belong to a separate Unicom block and check the maintenance of the Unicom block
#include <bits/stdc++.h>#definell Long Longusing namespacestd;Const intn=1e3+Ten;Const intdx[]={1,-1,0,0};Const intdy[]={0,0,1,-1};intn,m,q,cnt;structline{intX1,x2,y1,y2;} li[n*Ten];intf[n*n],num[n][n],ans[n*Ten];intHash (intXinty) {    return(X-1) *m+y;}intFundintx) {    if(f[x]==x)returnF[x]; returnf[x]=Fund (f[x]);}voidJoinintXinty) {    intFx=fund (x), fy=Fund (y); if(fx!=y) {CNT--; F[FX]=fy; }}voidDfsintXinty) {    intId=Hash (x, y);  for(intI=0;i<4; i++)    {        intfx=x+dx[i],fy=y+Dy[i]; if(fx<1|| fx>n| | fy<1|| FY&GT;M)Continue; if(num[fx][fy]==0) {Join (Id,hash (fx,fy)); }    }}voidprint (line l) { for(inti=l.x1;i<=l.x2;i++)     for(intj=l.y1;j<=l.y2;j++)    {        if(num[i][j]==0) cnt--; NUM[I][J]++; }}voidreprint (line l) { for(inti=l.x1;i<=l.x2;i++)     for(intj=l.y1;j<=l.y2;j++) {Num[i][j]--; if(num[i][j]==0) {CNT++;        DFS (I,J); }    }}intMain () {scanf ("%d%d%d",&n,&m,&q); CNT=n*m;  for(intI=1; i<=cnt;i++) f[i]=i;  for(intI=1; i<=q;i++) {scanf ("%d%d%d%d",&li[i].x1,&li[i].y1,&li[i].x2,&li[i].y2);    Print (Li[i]); }     for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++)        if(num[i][j]==0) DFS (I,J);  for(inti=q;i>=1; i--) {Ans[i]=CNT;    Reprint (Li[i]); }     for(intI=1; i<=q;i++) printf ("%d\n", Ans[i]); return 0;}
View Code

Ncpc2016-a-artwork

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.