Submission #1007331

# Submission time Handle Problem Language Result Execution time Memory
1007331 2024-06-24T15:50:09 Z VMaksimoski008 Nafta (COI15_nafta) C++17
100 / 100
365 ms 130892 KB
#include <bits/stdc++.h>
using namespace std;
using Comp = array<int, 3>;

struct BIT {
    int n;
    vector<int> tree;

    BIT(int _n) : n(_n+60), tree(_n+60) {}

    void update(int p, int v) {
        for(p++; p<n; p+=p&-p) tree[p] += v;
    }

    int query(int p) {
        int ans = 0;
        for(p++; p>0; p-=p&-p) ans += tree[p];
        return ans;
    }

    int query(int l, int r) { return query(r) - query(l-1); }
};

int dr[4] = {0, -1, 0, 1}, dc[4] = {-1, 0, 1, 0};
char M[2005][2005];
int L=1e9, R=-1, sum=0, n, m, dp[2005][2005], val[2005][2005], vis[2005][2005];
vector<Comp> comp;

void dfs(int r, int c) {
    vis[r][c] = 1;
    L = min(L, c); R = max(R, c);
    sum += (M[r][c] - '0');

    for(int i=0; i<4; i++) {
        int nr = r + dr[i], nc = c + dc[i];
        if(nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
        if(vis[nr][nc] || M[nr][nc] == '.') continue;
        dfs(nr, nc);
    }
}

void f(int l, int r, int tl, int tr, int j) {
    if(l > r) return ;
    int tm = (l + r) / 2, p = tl;

    for(int i=tl; i<=min(tm, tr); i++) {
        int v = dp[i-1][j-1] + val[i][tm];
        if(v > dp[tm][j]) {
            dp[tm][j] = v;
            p = i;
        }
    }

    f(l, tm-1, tl, p, j);
    f(tm+1, r, p, tr, j);
}

signed main() {
    cin >> n >> m;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++) cin >> M[i][j];

    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            if(vis[i][j] || M[i][j] == '.') continue;
            L=1e9, R=-1, sum=0;
            dfs(i, j);
            comp.push_back({ L+1, R+1, sum });
        }
    }

    sort(comp.begin(), comp.end(), [&](Comp &a, Comp &b) { return a[1] < b[1]; });

    BIT bit(m);
    for(auto &[l, r, v] : comp) bit.update(l, v);

    int ptr = 0;
    for(int j=1; j<=m; j++) {
        while(ptr < comp.size() && comp[ptr][1] < j) {
            bit.update(comp[ptr][0], -comp[ptr][2]);
            ptr++;
        }

        for(int i=1; i<=j; i++) val[i][j] = bit.query(i, j);
    }
    
    for(int i=1; i<=m; i++) {
        f(1, m, 1, m, i);
        int mx = 0;
        for(int j=1; j<=m; j++) mx = max(mx, dp[j][i]);
        cout << mx << '\n';
    }
    return 0;
}

Compilation message

nafta.cpp: In function 'int main()':
nafta.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         while(ptr < comp.size() && comp[ptr][1] < j) {
      |               ~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1140 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1140 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 9 ms 5656 KB Output is correct
8 Correct 8 ms 5724 KB Output is correct
9 Correct 9 ms 7260 KB Output is correct
10 Correct 7 ms 4956 KB Output is correct
11 Correct 6 ms 4700 KB Output is correct
12 Correct 6 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1140 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 9 ms 5656 KB Output is correct
8 Correct 8 ms 5724 KB Output is correct
9 Correct 9 ms 7260 KB Output is correct
10 Correct 7 ms 4956 KB Output is correct
11 Correct 6 ms 4700 KB Output is correct
12 Correct 6 ms 4700 KB Output is correct
13 Correct 323 ms 59476 KB Output is correct
14 Correct 329 ms 56584 KB Output is correct
15 Correct 365 ms 130892 KB Output is correct
16 Correct 285 ms 52668 KB Output is correct
17 Correct 265 ms 49680 KB Output is correct
18 Correct 275 ms 49684 KB Output is correct