Artwork Gym - 101550A

topic

  https://cn.vjudge.net/contest/323505#problem/A

The meaning of problems

  Gives a m × n matrix, a start all white, then gives q operations, each time the (x1, y1) to (x2, y2) which is a width of a black region. The output of each operation after the white black may be divided into many pieces.

answer

  BFS beginning to think about optimization from various directions QAQ , and then I was the subject of a disjoint-set. We can consider the opposite, the first operation is completed every black, black + 1 of each to the local, state and the last block with white disjoint-set connected together. Then we reverse delete the black part, when this one weight deleted to 0, which means this one from black to white, then we first treat it as a separate piece of record num ++, and then to run it the up and down, if the two points belong to different collections, then num--, and the outer points connected to the father of the current point. After each operation recorded answers to the final output. Learned learned qwq

 

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#define ull unsigned long long
#define met(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x&(-x))
#define MID (l + r) / 2
#define ll long long

using namespace std;

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e6 + 3;
const int maxn = 1e4 + 7;
const int N = 1010;

struct Que {
    int x1, x2, y1, y2;
}que[maxn];

int MAP[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int f[1000100];
int vis[1000100];
int res[1000100];

int Find(int i) {return f[i] = (f[i] == i) ? f[i] : f[i] = Find(f[i]);}

int main() {
    int n, m, q;
    cin >> m >> n >> q;
    for(int i = 1; i <= n*m+2; i++) f[i] = i;
    for(int i = 1; i <= q; i++) {
        cin >> que[i].y1 >> que[i].x1 >> que[i].y2 >> que[i].x2;
        if(que[i].x1 == que[i].x2) {
            int t1 = min(que[i].y1, que[i].y2);
            int t2 = max(que[i].y1, que[i].y2); 
            for(int j = t1; j <= t2; j++) MAP[que[i].x1][j]++;
        }
        else {
            int t1 = min(que[i].x1, que[i].x2);
            int t2 = max(que[i].x1, que[i].x2);
            for(int j = t1; j <= t2; j++) MAP[j][que[i].y1]++;
        }
    }
    // for(int i = 1; i <= n; i++) {
    //     for(int j = 1; j <= m; j++) {
    //         cout << MAP[i][j] << ' ';
    //     }
    //     cout << endl;
    // }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(!MAP[i][j]) {
                int id = (i-1)*m+j;
                for(int k = 0; k < 4; k++) {
                    int x = i+dx[k];
                    int y = j+dy[k];
                    if(x < 1 || x > n) continue;
                    if(y < 1 || y > m) continue;
                    if(!MAP[x][y]) {
                        int t = (x-1)*m+y;
                        int fi = Find(id);
                        int fo = Find(t);
                        f[fo] = fi;
                    }
                }
            }
        }
    }
    int num = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            int fi = Find((i-1)*m+j);
            if(!MAP[i][j] && !vis[fi]) {
                vis[fi] = 1;
                num++;
            }
        }
    }
    for(int i = q; i >= 1; i--) {
        res[i] = num;
        if(que[i].x1 == que[i].x2) {
            int t1 = min(que[i].y1, que[i].y2);
            int t2 = max(que[i].y1, que[i].y2); 
            for(int j = t1; j <= t2; j++) {
                MAP[que[i].x1][j]--;
                if(!MAP[que[i].x1][j]) {
                    num++;
                    int id = (que[i].x1-1)*m+j;
                    for(int k = 0; k < 4; k++) {
                        int x = que[i].x1+dx[k];
                        int y = j+dy[k];
                        if(x < 1 || x > n) continue;
                        if(y < 1 || y > m) continue;
                        if(!MAP[x][y]) {
                            int t = (x-1)*m+y;
                            int fi = Find(id);
                            int fo = Find(t);
                            if(fi != fo) num--;
                            f[fo] = fi;
                        }
                    }
                }
            }
        }
        else {
            int t1 = min(que[i].x1, que[i].x2);
            int t2 = max(que[i].x1, que[i].x2);
            for(int j = t1; j <= t2; j++) {
                MAP[j][que[i].y1]--;
                if(!MAP[j][que[i].y1]) {
                    num++;
                    int id = (j-1)*m+que[i].y1;
                    for(int k = 0; k < 4; k++) {
                        int x = j+dx[k];
                        int y = que[i].y1+dy[k];
                        if(x < 1 || x > n) continue;
                        if(y < 1 || y > m) continue;
                        if(!MAP[x][y]) {
                            int t = (x-1)*m+y;
                            int fi = Find(id);
                            int fo = Find(t);
                            if(fi != fo) num--;
                            f[fo] = fi;
                        }
                    }
                }
            }
        }
    }
    for(int i = 1; i <= q; i++) cout << res[i] << endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Stay-Online/p/11455786.html