Submission #771587

#TimeUsernameProblemLanguageResultExecution timeMemory
771587Magikarp4000Catfish Farm (IOI22_fish)C++17
78 / 100
1075 ms243652 KiB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n' 
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
#define int long long

const int MAXN = 3e5+10;
int n,m;
vector<PII> p[MAXN];
vector<int> v1[MAXN];
vector<PII> v[MAXN], v2[MAXN];
vector<PII> dp[MAXN], dp1[MAXN], dp2[MAXN], dp3[MAXN];
int dp4, dp5;
US<int> vis1[MAXN];

void precalc() {
    FOR(i,1,n+1) {
        US<int> vis, vis2;
        FORX(u,v[i]) {
            if (!vis.count(u.F)) {
                vis.insert(u.F);
                v2[i].PB(u);
            }
        }
        FORX(u,v[i+1]) {
            if (!vis2.count(u.F)) {
                vis2.insert(u.F);
                v1[i].PB(u.F);
            }
        }
        FORX(u,v[i-1]) {
            if (!vis2.count(u.F)) {
                vis2.insert(u.F);
                v1[i].PB(u.F);
            }
        }
    }
    FOR(i,1,n+1) {
        sort(ALL(v[i]));
        sort(ALL(v1[i]));
        sort(ALL(v2[i]));
    }
    FOR(i,1,n+1) {
        int cur = 0LL;
        FORX(u,v2[i]) {
            p[i].PB({u.F,cur+u.S});
            cur += u.S;
        }
    }
}

int locate(vector<PII> vec, int x, int t) {
    if (t == 0) {
        int ans = lower_bound(ALL(vec), make_pair(x,-LLINF))-vec.begin();
        return ans == vec.size() ? -1 : ans;
    }
    else {
        return upper_bound(ALL(vec), make_pair(x,LLINF))-vec.begin()-1;
    }
}

void pr(vector<PII> vec) {
    FORX(u,vec) cout << "(" << u.F << "," << u.S << ")" << " ";
    cout << ln;
}

long long max_weights(int32_t N, int32_t M, std::vector<int32_t> X, std::vector<int32_t> Y, std::vector<int32_t> W) {
    OPTM;
    n = N; m = M;
    FOR(i,0,m) {
        if (!vis1[i+1].count(Y[i]+1)) {
            vis1[i+1].insert(Y[i]+1);
            v[X[i]+1].PB({Y[i]+1,W[i]});
        }
    }
    FOR(i,0,n+2) {
        v[i].PB({0LL,0LL});
        if (!vis1[i].count(n)) {
            vis1[i].insert(n);
            v[i].PB({n,0LL});
        }
    }
    precalc();
    dp[1].PB({0LL,0LL});
    dp1[1].PB({0LL,0LL});
    dp2[1].PB({0LL,0LL});
    int ma3 = 0LL;
    int vn = v1[1].size();
    
    FORR(pos,vn-1,-1) {
        int j = v1[1][pos];
        int idx = locate(p[2],j,1);
        ma3 = max(ma3, p[2][idx].S);
        dp3[1].PB({j,ma3});
    }
    reverse(ALL(dp3[1]));
    // FOR(i,1,n+1) {
    //     // pr(v2[i]);
    //     // FORX(u,v1[i]) cout << u << " ";
    //     // cout << ln;
    // }
    // pr(dp3[1]);
    // FOR(i,1,n+1) {
    //     pr(p[i]);
    // }
    FOR(i,2,n+1) {
        int vn = v1[i].size();
        int idx = 0, idx1 = 0, idx2 = 0, idx3 = dp3[i-1].size()-1;
        int pn = p[i].size(), p1n = p[i-1].size(), dp2n = dp2[i-1].size();
        FORX(j,v1[i]) {
            while (idx < pn-1 && p[i][idx+1].F <= j) idx++;
            while (idx1 < p1n-1 && p[i-1][idx1+1].F <= j) idx1++;
            while (idx2 < dp2n-1 && dp2[i-1][idx2+1].F <= j) idx2++;
            while (idx3 > 0 && dp3[i-1][idx3-1].F >= j) idx3--;
            int zero = max(dp4, dp5+p[i-1][idx1].S);
            dp[i].PB({j, max(zero, dp2[i-1][idx2].S + p[i-1][idx1].S)});
            dp1[i].PB({j, max(zero, dp3[i-1][idx3].S - p[i][idx].S)});
        }
        // cout << i << ": \n";
        // pr(dp[i]); pr(dp1[i]);
        if (i == n) break;
        int ma2 = 0LL;
        dp4 = 0LL, dp5 = 0LL;
        idx = 0, idx1 = 0, idx2 = 0, idx3 = dp3[i-1].size()-1;
        int dpn = dp[i].size(), dpi1n = dp[i-1].size();
        FORX(j,v1[i]) {
            while (idx < dpn-1 && dp[i][idx+1].F <= j) idx++;
            while (idx1 < pn-1 && p[i][idx1+1].F <= j) idx1++;
            while (idx2 < dpi1n-1 && dp1[i-1][idx2+1].F <= j) idx2++;
            ma2 = max(ma2, dp[i][idx].S - p[i][idx1].S);
            dp2[i].PB({j,ma2});
            dp4 = max(dp4, max(dp[i-1][idx2].S, dp1[i-1][idx2].S) + p[i][idx1].S);
            dp5 = max(dp5, max(dp[i-1][idx2].S, dp1[i-1][idx2].S));
        }
        int ma3 = 0LL;
        int p2n = p[i+1].size();
        idx = p2n-1;
        FORR(pos,vn-1,-1) {
            int j = v1[i][pos];
            while (idx > 0 && p[i+1][idx].F > j) idx--;
            ma3 = max(ma3, max(dp[i][pos].S, dp1[i][pos].S) + p[i+1][idx].S);
            dp3[i].PB({j,ma3});
        }
        reverse(ALL(dp3[i]));
    }
    int res = -LLINF;
    FORX(u,dp[n]) res = max(res, u.S);
    FORX(u,dp1[n]) res = max(res, u.S);
    return res;
}

Compilation message (stderr)

fish.cpp: In function 'long long int locate(std::vector<std::pair<long long int, long long int> >, long long int, long long int)':
fish.cpp:75:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         return ans == vec.size() ? -1 : ans;
      |                ~~~~^~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...