Artwork NCPC 2016

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

题目意思:

给你一个n*m的图,给你q次操作,每次操作给两个点之间的所有空格染色(可重复染色),然后,每次操作后问未染色的区域有多少块。


思路

一开始是想按着给操作的顺序来染色,用并查集,染色后对染色周围的点DFS找到一个新的Fa,但,结果直接T了,因为DFS找新的Fa时间代价太大了。

正解:

先将图建立好,DFS求出区域块,每个区域块有个Fa,然后,按染色的顺序逆序来撤销操作,这样就能合并并查集了,每次给一个方块恢复白色后就将其与周围点合并Fa,时间代价就大大减少了,由于二维并查集写着麻烦,我就将二维的转一维了,偷个小懒。

不多说了,上代码。

#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define lson l, mid, pos<<1
#define rson mid+1, r, pos<<|1
using namespace std;
int n,m,q,ans;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int fa[1100000], cnt[1010][1010];
bool is_fa[1100000], vis[1010][1010];
stack ansans;
struct line{
    int xx1, xx2, yy1, yy2;
}ql[10010];
int find_set(int x){
    if (x==fa[x])
        return x;
    else
        return fa[x]=find_set(fa[x]);
}
void dfs(int x, int y, int fapos){
    if (x<=0||y<=0||x>m||y>n)
        return ;
    if (cnt[x][y]!=INF)
        return ;
    if (!vis[x][y])
        return ;
    vis[x][y]=false;
    fa[x*1000+y]=fapos;
    int tx, ty;
    for (int i=0;i<4;++i){
        tx=x+dir[i][0]; ty=y+dir[i][1];
        dfs(tx,ty,fapos);
    }
    return ;
}
void init(){
    for (int i=1;i<=m;++i){
        for (int j=1;j<=n;++j){
            if (vis[i][j]){
                fa[i*1000+j]=i*1000+j;
            }
            if (vis[i][j]&&cnt[i][j]==INF){
                is_fa[i*1000+j]=true;
                dfs(i,j,i*1000+j); ans++;
            }
        }
    }
}

void mergefa(int faa, int fab){
    fa[faa]=fab;
    is_fa[faa]=false;
    ans--;
    return ;
}

void release(int x, int y){
    int tx, ty, ma, mb;
    for (int i=0;i<4;++i){
        tx=x+dir[i][0]; ty=y+dir[i][1];
        if (tx>0&&ty>0&&tx<=m&&ty<=n){
            if (cnt[tx][ty]==INF){
                ma=find_set(x*1000+y);  mb=find_set(tx*1000+ty);
                if (ma!=mb){
                    //printf("%d\t%dwith\t%d\t%d\n",ma/1000,ma%1000,mb/1000,mb%1000);
                    mergefa(ma,mb);
                }
            }
        }
    }
    return ;
}

int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&q)){
        ans=0;
        memset(cnt,INF,sizeof(cnt));
        memset(vis,true,sizeof(vis));
        memset(is_fa,false,sizeof(is_fa));
        for (int i=1;i<=q;++i){
            scanf("%d%d%d%d",&ql[i].yy1,&ql[i].xx1,&ql[i].yy2,&ql[i].xx2);
            for (int j=ql[i].xx1;j<=ql[i].xx2;++j){
                for (int k=ql[i].yy1;k<=ql[i].yy2;++k){
                    cnt[j][k]=min(cnt[j][k],i);
                }
            }
        }
        init();
        ansans.push(ans);
        while(q>1){
            for (int i=ql[q].xx1;i<=ql[q].xx2;i++){
                for (int j=ql[q].yy1;j<=ql[q].yy2;++j){
                    if (cnt[i][j]==q){
                        cnt[i][j]=INF;  fa[i*1000+j]=i*1000+j; is_fa[i*1000+j]=true; ans++;
                        release(i,j);
                    }
                }
            }
            ansans.push(ans);
            q--;
        }
        while(!ansans.empty()){
            printf("%d\n",ansans.top());
            ansans.pop();
        }
    }
    return 0;
}


你可能感兴趣的:(图论,并查集,逆向思维,图论,并查集,逆向思维)