Submission #825038

#TimeUsernameProblemLanguageResultExecution timeMemory
825038becaidoCatfish Farm (IOI22_fish)C++17
3 / 100
842 ms37140 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "fish.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const ll INF = 1e18;
const int SIZE = 1e5 + 5;

int n, m, R;
vector<pair<int, int>> v[SIZE];
ll all[2], zero[2], fzero[2];
map<int, ll> inc[2], dek[2];

void chmax(ll &x, ll y) {
    x = max(x, y);
}

ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    n = N;
    FOR (i, 0, M - 1) {
        X[i]++, Y[i]++;
        v[X[i]].pb(Y[i], W[i]);
        m = max(m, Y[i]);
        R = max(R, X[i] + 1);
    }
    R = min(R, n);
    debug(R);
    all[0] = -INF;
    FOR (i, 1, R) {
        int f = i & 1;

        inc[f].clear();
        dek[f].clear();

        all[f] = zero[f] = fzero[f] = -INF;

        map<int, int> mp;
        mp[0] = mp[m] = 0;
        for (auto [j, x] : v[i]) mp[j] = 0;
        for (auto [j, x] : v[i - 1]) mp[j] = x;

        ll mx = zero[!f], sum = 0;
        auto it = inc[!f].begin();
        for (auto [j, x] : mp) {
            while (it != inc[!f].end() && it->F <= j) chmax(mx, it->S - sum), it++;
            sum += x;
            // debug(j, x, sum, mx);
            if (inc[f].count(j)) chmax(inc[f][j], mx + sum);
            else inc[f][j] = mx + sum;
        }

        mp.clear();
        mp[0] = mp[m] = 0;
        sum = 0;
        for (auto [j, x] : v[i - 1]) mp[j] = 0;
        for (auto [j, x] : v[i]) mp[j] = x, sum += x;
        mx = -INF;
        auto rit = dek[!f].rbegin();
        for (auto mit = mp.rbegin(); mit != mp.rend(); mit++) {
            auto [j, x] = *mit;
            while (rit != dek[!f].rend() && rit->F >= j) chmax(mx, rit->S + sum), rit++;
            if (dek[f].count(j)) chmax(dek[f][j], mx - sum);
            else dek[f][j] = mx - sum;
            sum -= x;
        }
        reverse(v[i].begin(), v[i].end());

        chmax(all[f], all[!f]);
        chmax(all[f], fzero[!f]);
        chmax(all[f], inc[f][m]);

        chmax(zero[f], zero[!f]);
        chmax(zero[f], fzero[!f]);

        chmax(fzero[f], dek[f][0]);
        chmax(dek[f][m], all[f]);

        for (auto it = inc[f].begin(); it != inc[f].end();) {
            if (it != inc[f].begin() && it->S == prev(it)->S) it = inc[f].erase(it);
            else it++;
        }
        vector<pair<int, ll>> tmp;
        for (auto it = dek[f].rbegin(); it != dek[f].rend();) {
            auto [j, x] = *it;
            it++;
            if (tmp.size() && x == tmp.back().S) continue;
            else tmp.pb(j, x);
        }
        dek[f].clear();
        for (auto [j, x] : tmp) dek[f][j] = x;

        // debug(i);
        // for (auto [j, x] : inc[f]) debug("inc", j, x);
        // for (auto [j, x] : dek[f]) debug("dek", j, x);
    }
    ll ans = 0;
    int f = R & 1;
    chmax(ans, zero[f]);
    chmax(ans, fzero[f]);
    chmax(ans, all[f]);
    return ans;
}

/*
in1
5 4
0 2 5
1 1 2
4 4 1
3 3 3
out1
8
*/

#ifdef WAIMAI
int main() {
  int N, M;
  assert(2 == scanf("%d %d", &N, &M));

  vector<int> X(M), Y(M), W(M);
  for (int i = 0; i < M; ++i) {
    assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
  }

  long long result = max_weights(N, M, X, Y, W);
  printf("%lld\n", result);
  return 0;
}
#endif

Compilation message (stderr)

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
fish.cpp:47:5: note: in expansion of macro 'debug'
   47 |     debug(R);
      |     ^~~~~
#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...