题解:CF2158E Sink

· · 题解

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 4;
int n, m;
int ans;
int siz[MAXN], sum[MAXN], tot;
//siz维护的连通块中的无向边数,sum维护的连通块中每个小格的总入度之和,sum-siz*2就是连通块的入度
int fa[MAXN];
bool vis[MAXN];//开这个数组是为了调试时看见答案统计了哪些连通块
vector<int> a[MAXN];
vector<int> in[MAXN];//维护每小格入度
vector<int> g[MAXN];//维护每小格编号
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool check(int x, int y)
{
    return x > 0 && x <= n && y > 0 && y <= m;
}
int _find(int x)
{
    if (fa[x] ==x) return x;
    return fa[x] = _find(fa[x]);
}
void Union(int x, int y)
{
    x = _find(x), y = _find(y);
    if (x == y) return;
    fa[y] = x; sum[x] += sum[y]; siz[x] += siz[y];
}
void update(int x, int y, int c)
{
    int cnt = 0;
    for (int i = 0; i < 4; i++)
    {
        int tx = x + dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] == a[tx][ty] && _find(g[x][y]) == _find(g[tx][ty])) cnt++;
    }
    a[x][y] -= c;
    if (sum[_find(g[x][y])] == (siz[_find(g[x][y])])*2)
    {
        ans--; vis[g[x][y]] = 0;
    }
    int anc = _find(g[x][y]);
    sum[anc] -= in[x][y]; siz[anc] -= cnt;
    g[x][y] = ++tot;
    fa[g[x][y]] = g[x][y]; siz[g[x][y]] = 0; sum[g[x][y]] = 0; in[x][y] = 0;
    for (int i = 0; i < 4; i++)
    {
        int tx = x+dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] <= a[tx][ty] && a[x][y] + c > a[tx][ty])
        {
            in[tx][ty]++; sum[_find(g[tx][ty])]++;
            if (sum[_find(g[tx][ty])] == 1 + (siz[_find(g[tx][ty])])*2)
            {
                ans--; vis[_find(g[tx][ty])] = 0;
            }
        }
        if (a[x][y] >= a[tx][ty])
        {
            in[x][y]++; sum[_find(g[x][y])]++;
        }
    }
    for (int i = 0; i < 4; i++)
    {
        int tx = x+dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] == a[tx][ty])
        {
            Union(g[x][y], g[tx][ty]);
            siz[_find(g[x][y])] ++;
        }
    }
    if (sum[_find(g[x][y])] == (siz[_find(g[x][y])])*2)
    {
        ans++; vis[_find(g[x][y])] = 1;
    }
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        tot = 0; ans = 0;
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            a[i].push_back(0); in[i].push_back(0); g[i].push_back(0);
            for (int j = 1; j <= m; j++)
            {
                int x;
                cin >> x;
                a[i].push_back(x); in[i].push_back(0); g[i].push_back(0);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                g[i][j] = ++tot; fa[tot] = tot; siz[tot] = 0;
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    int x = i+dx[k], y = j+dy[k];
                    if (!check(x, y)) continue;
                    if (a[i][j] >= a[x][y]) in[i][j]++;
                }
                sum[g[i][j]] += in[i][j];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    int x = i+dx[k], y = j+dy[k];
                    if (!check(x, y)) continue;
                    if (a[i][j] == a[x][y])
                    {
                        Union(g[i][j], g[x][y]), siz[_find(g[x][y])] ++;
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int tmp = _find(g[i][j]);
                if (!vis[tmp] && sum[tmp] == (siz[tmp])*2)
                {
                    ans++; vis[tmp] = 1;
                }
            }
        }
        cout << ans <<endl;
        int q;
        cin >> q;
        while(q--)
        {
            int x, y, c;
            cin >> x >> y >> c;
            update(x, y, c);
            cout << ans <<"\n";
        }
        for (int i = 1; i <= tot; i++) vis[i] = sum[i] = siz[i] = fa[i] = 0;
        for (int i = 1; i <= n; i++) in[i].clear(), a[i].clear(), g[i].clear();
    }
    return 0;
}