Submission #201589

#TimeUsernameProblemLanguageResultExecution timeMemory
201589stefdascaNafta (COI15_nafta)C++14
34 / 100
1037 ms22272 KiB
#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define dancila 3.14159265359
#define eps 1e-9

// #define fisier 1

using namespace std;

typedef long long ll;

int n, m, sum[3002][3002];

char mat[3002][3002];

int ox[] = {-1, 0, 1, 0};
int oy[] = {0, 1, 0, -1};

int cum[3002], finish[3002];

int dp[3002][3002];

vector<int> adj[3002];

int aint[8002], lazy[8002];
void lz(int nod, int st, int dr)
{
    aint[nod] += lazy[nod];
    if(st != dr)
    {
        lazy[nod << 1] += lazy[nod];
        lazy[nod << 1|1] += lazy[nod];
    }
    lazy[nod] = 0;
}
void update(int nod, int st, int dr, int L, int R, int val)
{
    if(lazy[nod])
        lz(nod, st, dr);
    if(dr < L || st > R)
        return;
    if(L <= st && dr <= R)
    {
        lazy[nod] += val;
        lz(nod, st, dr);
        return;
    }
    int mid = (st + dr) / 2;
    update(nod << 1, st, mid, L, R, val);
    update(nod << 1|1, mid+1, dr, L, R, val);
    aint[nod] = max(aint[nod << 1], aint[nod << 1|1]);
}
int query(int nod, int st, int dr, int L, int R)
{
    if(lazy[nod])
        lz(nod, st, dr);
    if(L <= st && dr <= R)
        return aint[nod];
    if(dr < L || st > R)
        return 0;
    int mid = (st + dr) / 2;
    int ans = max(query(nod << 1, st, mid, L, R),
                  query(nod << 1|1, mid+1, dr, L, R));
    aint[nod] = max(aint[nod << 1], aint[nod << 1|1]);
    return ans;
}
int main()
{

    #ifdef fisier
        ifstream f("input.in");
        ofstream g("output.out");
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> (mat[i] + 1);
    int acm = 0;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            if(mat[i][j] >= '0' && mat[i][j] <= '9')
            {
                int L = j, R = j;
                int tot = (mat[i][j] - '0');
                deque<pair<int, int> > d;
                d.pb({i, j});
                mat[i][j] = '.';
                while(!d.empty())
                {
                    pair<int, int> nod = d[0];
                    L = min(L, nod.se);
                    R = max(R, nod.se);
                    d.pop_front();
                    for(int x = 0; x <= 3; ++x)
                    {
                        int new_x = nod.fi + ox[x];
                        int new_y = nod.se + oy[x];
                        if(new_x == 0 || new_y == 0 || new_x == n+1 || new_y == m+1)
                            continue;
                        if(mat[new_x][new_y] == '.')
                            continue;
                        tot += (mat[new_x][new_y] - '0');
                        mat[new_x][new_y] = '.';
                        d.pb({new_x, new_y});
                    }
                }
                sum[L][R] += tot;
                acm += tot;
            }
    for(int i = 1; i <= m; ++i)
        for(int j = i; j <= m; ++j)
            cum[i] += sum[i][j], cum[j+1] -= sum[i][j], finish[j] += sum[i][j];
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= i; ++j)
            if(sum[j][i])
                adj[i].pb(j);
    for(int i = 1; i <= m; ++i)
    {
        cum[i] += cum[i-1];
        dp[1][i] = cum[i];
    }
    bool stp = 0;
    for(int i = 2; i <= m; ++i)
    {
        memset(aint, 0, sizeof(aint));
        memset(lazy, 0, sizeof(lazy));
        for(int j = 1; j <= m; ++j)
        {
            dp[i][j] = dp[i-1][j];
            if(j != 1)
                dp[i][j] = max(dp[i][j], query(1, 1, m, 1, j-1) + cum[j]);
            if(dp[i][j] == acm)
                stp = 1;
            update(1, 1, m, j, j, dp[i-1][j] - cum[j]);
            for(int x = 0; x < adj[j].size(); ++x)
            {
                int st = adj[j][x];
                update(1, 1, m, st, j, sum[st][j]);
            }
        }
        if(stp == 1)
            break;
    }
    int ans = 0;
    for(int i = 1; i <= m; ++i)
    {
        for(int j = 1; j <= m; ++j)
            ans = max(ans, dp[i][j]);
        cout << ans << '\n';
    }
    return 0;
}

Compilation message (stderr)

nafta.cpp: In function 'int main()':
nafta.cpp:144:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int x = 0; x < adj[j].size(); ++x)
                            ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...