Submission #771533

#TimeUsernameProblemLanguageResultExecution timeMemory
771533Magikarp4000메기 농장 (IOI22_fish)C++17
0 / 100
242 ms225028 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+2) {
        US<int> vis;
        FORX(u,v[i]) {
            if (!vis.count(u.F)) {
                vis.insert(u.F);
                v2[i].PB(u);
                // v1[i].PB(u.F);
            }
        }
        FORX(u,v[i+1]) {
            if (!vis.count(u.F)) {
                vis.insert(u.F);
                v2[i].PB({u.F,0LL});
                v1[i].PB(u.F);
            }
        }
        FORX(u,v[i-1]) {
            if (!vis.count(u.F)) {
                vis.insert(u.F);
                v2[i].PB({u.F,0LL});
                v1[i].PB(u.F);
            }
        }
    }
    FOR(i,1,n+2) {
        sort(ALL(v[i]));
        sort(ALL(v1[i]));
        sort(ALL(v2[i]));
    }
    FOR(i,1,n+2) {
        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) {
    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,1,n+1) {
        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});
    }
    sort(ALL(dp3[1]));
    // pr(dp3[1]);
    FOR(i,2,n+1) {
        int vn = v1[i].size();
        FORX(j,v1[i]) {
            int idx = locate(p[i],j,1);
            int idx1 = locate(p[i-1],j,1);
            int idx2 = locate(dp2[i-1],j,1);
            int idx3 = locate(dp3[i-1],j,0);
            // cout << "idx3: " << j << " " << idx3 << ln;
            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 << ": ";
        // pr(dp[i]); pr(dp1[i]);
        int ma2 = 0LL;
        dp4 = 0LL, dp5 = 0LL;
        FORX(j,v1[i]) {
            int idx = locate(dp[i],j,1);
            int idx1 = locate(p[i],j,1);
            int idx2 = locate(dp[i-1],j,1);
            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));
        }
        // if (i >= 5) return 0;
        int ma3 = 0LL;
        FORR(pos,vn-1,-1) {
            int j = v1[i][pos];
            int idx = locate(p[i+1],j,1);
            ma3 = max(ma3, max(dp[i][pos].S, dp1[i][pos].S) + p[i+1][idx].S);
            dp3[i].PB({j,ma3});
        }
        sort(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:78: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]
   78 |         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...