Submission #1160076

#TimeUsernameProblemLanguageResultExecution timeMemory
1160076InvMODColouring a rectangle (eJOI19_colouring)C++20
100 / 100
845 ms66216 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 4e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;

struct Item{
    int l, r; ll c;
    Item(int l = -1, int r = -1, ll c = 0): l(l), r(r), c(c) {}
};

ll n, m, val[2][N], dp[2][N], pref[N];

vector<Item> cross[2];

ll calc_dp(vector<Item>& cross, ll val[], ll dp[]){
    FOR(i, 1, n + m - 1){
        pref[i] = val[i];
        pref[i] += pref[i - 1];
    }

    /*
        +   dp[i] = how to fill all from 1 -> i with i must be fill by horizontal line
        -   choose all line that need to fill i
            and the prefix from 1 -> i - 1 will be fill by vertical line (base case)
        -   for each j from 1 -> i - 1 we add horizontal line that need to fill i
            and from j + 1 -> i - 1 will be fill by vertical line
    */

    /*
            to calculate answer
        -   we will iterate from 1 -> n and take min of
            dp[i] + (vertical line will be fill from i+1 -> n)
        -   base case of answer is full of vertical with no horizontal
    */

    ll answer = pref[n + m - 1];
    FOR(i, 1, n + m - 1){
        ll sum = 0;

        vector<ll> lazy(n + m + 2);
        FOR(j, 1, sz(cross) - 1){
            if(cross[j].l <= i && i <= cross[j].r){
                sum += cross[j].c;
                lazy[1] += cross[j].c;
                lazy[cross[j].l] -= cross[j].c;
            }
        }

        // base case
        dp[i] = sum + pref[i - 1];

        FOR(j, 1, i - 1){
            dp[i] = min(dp[i], dp[j] + lazy[j] +
                                pref[i - 1] - pref[j]);
            lazy[j + 1] += lazy[j];
        }

        answer = min(answer, dp[i] + pref[n + m - 1] - pref[i]);
    }

    return answer;
}

struct SMT{
    int trsz;
    vector<ll> st, laz;
    SMT(int n = 0): trsz(n), st((n << 2 | 1)), laz((n << 2 | 1)) {}

    void apply(int id, ll val){
        st[id] += val;
        laz[id] += val;
    }

    void down(int id){
        ll val = laz[id]; laz[id] = 0;

        apply(id << 1, val);
        apply(id << 1|1, val);
    }

    void update(int id, int l, int r, int u, int v, ll val){
        if(l >= u && r <= v){
            apply(id, val);
        }
        else{
            int m = l+r>>1;
            down(id);
            if(u <= m) update(id << 1, l, m, u, v, val);
            if(v > m) update(id << 1|1, m+1, r, u, v, val);
            st[id] = min(st[id << 1], st[id << 1|1]);
        }
    }

    ll get(int id, int l, int r, int u, int v){
        if(l >= u && r <= v) return st[id];

        down(id);
        int m = l+r>>1;

        ll answer = INF;
        if(u <= m) ckmn(answer, get(id << 1, l, m, u, v));
        if(v > m) ckmn(answer, get(id << 1|1, m+1, r, u, v));

        return answer;
    }

    void update(int l, int r, ll val){
        if(l > r) return;
        update(1, 1, trsz, l, r, val);
    }

    ll query(int l, int r){
        return get(1, 1, trsz, l, r);
    }

    void debugST(int id, int l, int r){
        if(l == r){
     //       cout << l << " " << st[id] << "\n";
        }
        else{
            int m = l+r>>1;
            down(id);
            debugST(id << 1, l, m);
            debugST(id << 1|1, m+1, r);
        }
    }
};

ll opti_dp(vector<Item>& cross, ll val[]){
    FOR(i, 1, n + m - 1){
        pref[i] = val[i];
        pref[i] += pref[i - 1];
    }

    vector<vector<ll>> add(n + m + 2, vector<ll>());
    vector<vector<ll>> rem(n + m + 2, vector<ll>());

    FOR(i, 1, sz(cross) - 1){
        add[cross[i].l].push_back(i);
        rem[cross[i].r + 1].push_back(i);
    }

    SMT dp(n + m - 1);

    ll answer = pref[n + m - 1];
    FOR(i, 1, n + m - 1){
        for(int id : add[i]){
            // we will need line id for 1 -> cross[id].r
            //cout << "ADD: " << cross[id].r <<" " << cross[id].c << "\n";
            dp.update(1, cross[id].r, cross[id].c);
        }

        for(int id : rem[i]){
            // cross[id].l -> cross[id].r will now fill with vertical line
            //cout << "DEL: " << cross[id].c << "\n";
            dp.update(1, cross[id].l - 1, -cross[id].c);
        }
        dp.update(i, i, pref[i - 1]);
        //dp.debugST(1, 1, n + m - 1);

        ll ndp = dp.query(1, i);
        answer = min(answer, ndp + pref[n + m - 1] - pref[i]);
        dp.update(i, i, -dp.query(i, i)); // wtf how dumb ?
        dp.update(i, i, ndp);

        //cout << i <<" " << ndp << " " << dbg(pref[n + m - 1]) << dbg(pref[i]) << "\n";
        dp.update(1, i - 1, val[i]);
    }
    //cout << answer << "\n";
    //cout << "END DP\n\n";

    return answer;
}

void solve()
{
    cin >> m >> n;

    cross[0].push_back(Item());
    cross[1].push_back(Item());

    FOR(i, 1, m + n - 1){
        int l,r;

        if(i <= n){
            l = n - i + 1;
        }
        else l = i - n + 1;
        if(i <= m){
            r = n + i - 1;
        }
        else r = n + m - 1 - (i - m);

        int cur_w; cin >> cur_w;
        cross[(i & 1)].push_back(Item(l, r, cur_w));
    }

    FOR(i, 1, m + n - 1){
        int cur_w; cin >> cur_w;
        if((i & 1) == (n & 1)){
            val[1][i] = cur_w;
        }
        else val[0][i] = cur_w;
    }

    ll answer = 0;
    FOR(i, 0, 1){
        answer += opti_dp(cross[i], val[i]);
        //answer += calc_dp(cross[i], val[i], dp[i]);
    }

    cout << answer << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

/* Test
Input:
4 3
5 3 4 3 5 3
5 2 5 2 4 2
Output: 19

Input:
3 3
4 1 2 2 3
4 1 2 1 4
Output: 6
*/

Compilation message (stderr)

colouring.cpp: In function 'int main()':
colouring.cpp:245:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  245 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
colouring.cpp:246:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  246 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...