Submission #1147007

#TimeUsernameProblemLanguageResultExecution timeMemory
1147007blackslexNafta (COI15_nafta)C++20
100 / 100
291 ms121268 KiB
#include<bits/stdc++.h>
#define lsb(x) (x &(-x))

using namespace std;
using pii = pair<int, int>;
using tp = tuple<int, int, int>;

const int N = 2005, K = N * N, cd[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, a[N][N], val[N][N], par[K], sz[K], mx[K], mn[K], fwk[N], dp[N][N];
string s;
vector<pii> pos[N];

void upd (int idx, int val) {
    for (; idx < N; idx += lsb(idx)) fwk[idx] += val;
}

int qr (int idx) {
    int res = 0;
    for (; idx; idx -= lsb(idx)) res += fwk[idx];
    return res;
}

void updran (int l, int r, int val) {
    upd(l, val);
    upd(r + 1, -val);
}

int fset (int x) {
    return (par[x] == x ? x : par[x] = fset(par[x]));
}

void mg (int x, int y) {
    if ((x = fset(x)) == (y = fset(y))) return;
    sz[x] += sz[y];
    mx[x] = max(mx[x], mx[y]);
    mn[x] = min(mn[x], mn[y]);
    par[y] = x;
}

int get (int x, int y) {
    return (--x) * m + y;
}

void compute (int l, int r, int optl, int optr, int st) {
    if (l > r) return;
    int mid = (l + r) >> 1;
    pii cans = {-1e9, -1};
    for (int k = optl; k <= min(mid, optr); k++) {
        cans = max(cans, pii(dp[st - 1][k] + val[mid][k], k));
    }
    dp[st][mid] = cans.first;
    compute(l, mid - 1, optl, cans.second, st); compute(mid + 1, r, cans.second, optr, st);
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n * m; i++) par[i] = i;
    for (int i = 1; i <= n; i++) {
        cin >> s; s = " " + s;
        for (int j = 1; j <= m; j++) {
            a[i][j] = -1;
            if (s[j] != '.') {
                a[i][j] = (s[j] - '0');
                int z = get(i, j);
                sz[z] = a[i][j];
                mx[z] = mn[z] = j;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] == -1) continue;
            for (int k = 0; k < 4; k++) {
                int nx = i + cd[k][0], ny = j + cd[k][1];
                if (nx <= 0 || nx > n || ny <= 0 || ny > m || a[nx][ny] == -1) continue;
                mg(get(i, j), get(nx, ny));
            }
        }
    }
    vector<tp> c;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int z = get(i, j);
            if (a[i][j] != -1 && fset(z) == z) c.emplace_back(mn[z], mx[z], sz[z]);
        }
    }
    for (auto &[x, y, z]: c) {
        pos[x].emplace_back(y, z);
    }
    for (int i = 1; i <= m; i++) {
        for (auto &[x, y]: pos[i]) updran(i, x, y);
    }
    for (int i = 0; i <= m; i++) {
        for (auto &[x, y]: pos[i]) updran(i, x, -y);
        for (int j = i + 1; j <= m; j++) val[j][i] = qr(j);
    }
    for (int i = 1; i <= m; i++) {
        compute(0, m, 0, m, i);
    }
    for (int i = 1; i <= m; i++) {
        int ans = 0;
        for (int j = 1; j <= m; j++) ans = max(ans, dp[i][j]);
        printf("%d\n", ans);
    }
}

Compilation message (stderr)

nafta.cpp: In function 'int main()':
nafta.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...