C11WATER - Đọng nước

Tác giả: RR

Ngôn ngữ: C++

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

const int MN = 1011;
const int MN2 = MN * MN;

int id[MN][MN], h[MN][MN], m, n;
int lab[MN2], height[MN2];
bool out[MN2];
vector< pair<int,int> > ls[MN2];

int getRoot(int u) {
    if (lab[u] < 0) return u;
    return lab[u] = getRoot(lab[u]);
}

void merge(int u, int v) {
    lab[u] = lab[u] + lab[v];
    lab[v] = u;

    height[u] = max(height[u], height[v]);
    out[u] = out[u] | out[v];
}

const int di[] = {-1,1,0,0};
const int dj[] = {0,0,-1,1};

int main() {
    scanf("%d%d", &m, &n);
    int now = 0;
    FOR(i,2,m+1) FOR(j,2,n+1)
        scanf("%d", &h[i][j]);

    m += 2; n += 2;

    FOR(i,1,m) FOR(j,1,n) {
        id[i][j] = ++now;
        lab[now] = -1;
        height[now] = h[i][j];
        if (h[i][j] == 0) out[now] = true;
        else out[now] = false;

        ls[h[i][j]].PB(MP(i,j));
    }

    long long res = 0;

    FOR(t,1,1000000) REP(tt,ls[t].size()) {
        int u = ls[t][tt].F, v = ls[t][tt].S, uu, vv;

        REP(dir,4) {
            uu = u + di[dir], vv = v + dj[dir];
            if (uu < 1 || uu > m || vv < 1 || vv > n) continue;

            int id2 = getRoot(id[uu][vv]);
            int id1 = getRoot(id[u][v]);

            if (height[id1] >= height[id2] && id2 != id1) {
                if (!out[id2]) {
                    res += -lab[id2] * (height[id1] - height[id2]);
                }
                merge(id2, id1);
            }
        }
    }
    cout << res << endl;

    return 0;
}

Download