Submission #627574

# Submission time Handle Problem Language Result Execution time Memory
627574 2022-08-12T17:08:48 Z dqhungdl Catfish Farm (IOI22_fish) C++17
67 / 100
418 ms 73676 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
const int MAX = 1e5 + 5;
const long long INF = 1e18;
bool markLow[MAX];
vector<ii> g[MAX];
vector<long long> S[MAX];
vector<vector<long long>> f[MAX];

struct FenwickTreeLow {
    int n;
    vector<int> buffer;
    vector<long long> tree;

    FenwickTreeLow(int _n) {
        n = _n;
        tree.resize(n + 5, -INF);
    }

    void update(int idx, long long val) {
        idx++;
        for (int i = idx; i <= n; i += i & -i) {
            buffer.push_back(i);
            tree[i] = max(tree[i], val);
        }
    }

    long long get(int idx) {
        idx++;
        long long rs = 0;
        for (int i = idx; i > 0; i -= i & -i)
            rs = max(rs, tree[i]);
        return rs;
    }

    void reset() {
        for (int id: buffer)
            tree[id] = -INF;
        buffer.clear();
    }
};

struct FenwickTreeHigh {
    int n;
    vector<int> buffer;
    vector<long long> tree;

    FenwickTreeHigh(int _n) {
        n = _n;
        tree.resize(n + 5, -INF);
    }

    void update(int idx, long long val) {
        idx++;
        for (int i = idx; i > 0; i -= i & -i) {
            buffer.push_back(i);
            tree[i] = max(tree[i], val);
        }
    }

    long long get(int idx) {
        idx++;
        long long rs = 0;
        for (int i = idx; i <= n; i += i & -i)
            rs = max(rs, tree[i]);
        return rs;
    }

    void reset() {
        for (int id: buffer)
            tree[id] = -INF;
        buffer.clear();
    }
};

//long long calcCost(int col, int L, int R) {
//    int l = lower_bound(g[col].begin(), g[col].end(), ii(L, 0)) - g[col].begin();
//    int r = upper_bound(g[col].begin(), g[col].end(), ii(R, 0)) - g[col].begin() - 1;
//    if (l <= r)
//        return S[col][r] - (l ? S[col][l - 1] : 0);
//    return 0;
//}

long long calcCostLeft(int col, int L) {
    int l = lower_bound(g[col].begin(), g[col].end(), ii(L, 0)) - g[col].begin();
    return l ? S[col][l - 1] : 0;
}

long long calcCostRight(int col, int R) {
    int r = upper_bound(g[col].begin(), g[col].end(), ii(R, INF)) - g[col].begin() - 1;
    return r >= 0 ? S[col][r] : 0;
}

long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    for (int i = 0; i < M; i++) {
        g[X[i]].emplace_back(Y[i], W[i]);
        if (!Y[i])
            markLow[X[i]] = true;
    }
    for (int i = 0; i < N; i++) {
        if (!markLow[i])
            g[i].emplace_back(0, 0);
        g[i].emplace_back(N, 0);
    }
    for (int i = 0; i < N; i++) {
        sort(g[i].begin(), g[i].end());
        S[i].resize(g[i].size());
        S[i][0] = g[i][0].second;
        for (int j = 1; j < g[i].size(); j++)
            S[i][j] = S[i][j - 1] + g[i][j].second;
        f[i].resize(g[i].size(), vector<long long>(2));
    }
    FenwickTreeLow lowTree(N), lowJump(N);
    FenwickTreeHigh highTree(N), highJump(N);
    long long rs = 0;
    for (int i = 1; i < N; i++) {
        if (i > 1) {
            lowJump.reset(), highJump.reset();
            for (int t = 0; t < g[i - 2].size(); t++) {
                lowJump.update(t, f[i - 2][t][1]);
                highJump.update(t, f[i - 2][t][1] + calcCostRight(i - 1, g[i - 2][t].first));
            }
        }
        lowTree.reset(), highTree.reset();
        for (int t = 0; t < g[i - 1].size(); t++) {
            lowTree.update(t, f[i - 1][t][0] - calcCostLeft(i - 1, g[i - 1][t].first));
            highTree.update(t, f[i - 1][t][1] + calcCostRight(i, g[i - 1][t].first));
        }
        for (int j = 0; j < g[i].size(); j++) {
            if (i > 1) {
                int low = upper_bound(g[i - 2].begin(), g[i - 2].end(), ii(g[i][j].first, INF)) - g[i - 2].begin() - 1;
                long long tmp = lowJump.get(low) + calcCostRight(i - 1, g[i][j].first);
                f[i][j][0] = max(f[i][j][0], tmp);
                f[i][j][1] = max(f[i][j][1], tmp);

                int high = low + 1;
                tmp = highJump.get(high);
                f[i][j][0] = max(f[i][j][0], tmp);
                f[i][j][1] = max(f[i][j][1], tmp);

//                for (int t = 0; t < g[i - 2].size(); t++) {
//                    long long tmp = f[i - 2][t][1] + calcCost(i - 1, 0, max(g[i - 2][t].first, g[i][j].first));
//                    f[i][j][0] = max(f[i][j][0], tmp);
//                    f[i][j][1] = max(f[i][j][1], tmp);
//                }
            } else {
                long long tmp = calcCostRight(0, g[i][j].first);
                f[i][j][0] = max(f[i][j][0], tmp);
                f[i][j][1] = max(f[i][j][1], tmp);
            }

            int low = upper_bound(g[i - 1].begin(), g[i - 1].end(), ii(g[i][j].first, INF)) - g[i - 1].begin() - 1;
            long long tmp = lowTree.get(low) + calcCostRight(i - 1, g[i][j].first);
            f[i][j][0] = max(f[i][j][0], tmp);
            f[i][j][1] = max(f[i][j][1], tmp);

            int high = lower_bound(g[i - 1].begin(), g[i - 1].end(), ii(g[i][j].first, 0)) - g[i - 1].begin();
            f[i][j][1] = max(f[i][j][1], highTree.get(high) - calcCostLeft(i, g[i][j].first));

            rs = max({rs, f[i][j][0], f[i][j][1]});
//            for (int t = 0; t < g[i - 1].size(); t++) {
//                if (g[i - 1][t].first <= g[i][j].first) {
//                    f[i][j][0] = max(f[i][j][0], f[i - 1][t][0] + calcCost(i - 1, g[i - 1][t].first, g[i][j].first));
//                    f[i][j][1] = max(f[i][j][1], f[i - 1][t][0] + calcCost(i - 1, g[i - 1][t].first, g[i][j].first));
//                }
//                if (g[i - 1][t].first >= g[i][j].first)
//                    f[i][j][1] = max(f[i][j][1], f[i - 1][t][1] + calcCost(i, g[i][j].first, g[i - 1][t].first));
//            }
        }
    }
    return rs;
}

//int main() {
//    freopen("../_input", "r", stdin);
//    int N, M;
//    assert(2 == scanf("%d %d", &N, &M));
//
//    std::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;
//}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:112:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for (int j = 1; j < g[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~
fish.cpp:122:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |             for (int t = 0; t < g[i - 2].size(); t++) {
      |                             ~~^~~~~~~~~~~~~~~~~
fish.cpp:128:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |         for (int t = 0; t < g[i - 1].size(); t++) {
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:132:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |         for (int j = 0; j < g[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 178 ms 49708 KB Output is correct
2 Correct 216 ms 55784 KB Output is correct
3 Correct 117 ms 29268 KB Output is correct
4 Correct 113 ms 29272 KB Output is correct
5 Correct 412 ms 73676 KB Output is correct
6 Correct 418 ms 58528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 289 ms 57408 KB Output is correct
3 Correct 308 ms 65068 KB Output is correct
4 Correct 174 ms 49704 KB Output is correct
5 Correct 225 ms 55636 KB Output is correct
6 Correct 3 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 3 ms 7252 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
10 Correct 116 ms 29196 KB Output is correct
11 Correct 114 ms 29272 KB Output is correct
12 Correct 195 ms 49808 KB Output is correct
13 Correct 212 ms 55600 KB Output is correct
14 Correct 187 ms 42556 KB Output is correct
15 Correct 230 ms 45860 KB Output is correct
16 Correct 179 ms 42608 KB Output is correct
17 Correct 201 ms 45912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 29268 KB Output is correct
2 Correct 122 ms 29272 KB Output is correct
3 Correct 129 ms 28464 KB Output is correct
4 Correct 131 ms 30208 KB Output is correct
5 Correct 162 ms 31608 KB Output is correct
6 Correct 175 ms 31692 KB Output is correct
7 Correct 155 ms 31688 KB Output is correct
8 Correct 160 ms 31720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 6 ms 7356 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7252 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 5 ms 7636 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 5 ms 7508 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 6 ms 7356 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7252 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 5 ms 7636 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 5 ms 7508 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 9 ms 7508 KB Output is correct
17 Correct 47 ms 11748 KB Output is correct
18 Correct 53 ms 11988 KB Output is correct
19 Correct 46 ms 11860 KB Output is correct
20 Correct 43 ms 11948 KB Output is correct
21 Correct 44 ms 11804 KB Output is correct
22 Correct 83 ms 16392 KB Output is correct
23 Correct 14 ms 8296 KB Output is correct
24 Correct 31 ms 10196 KB Output is correct
25 Correct 5 ms 7412 KB Output is correct
26 Correct 12 ms 8152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 5 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 6 ms 7356 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 6 ms 7252 KB Output is correct
8 Correct 5 ms 7252 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 5 ms 7636 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 5 ms 7508 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 9 ms 7508 KB Output is correct
17 Correct 47 ms 11748 KB Output is correct
18 Correct 53 ms 11988 KB Output is correct
19 Correct 46 ms 11860 KB Output is correct
20 Correct 43 ms 11948 KB Output is correct
21 Correct 44 ms 11804 KB Output is correct
22 Correct 83 ms 16392 KB Output is correct
23 Correct 14 ms 8296 KB Output is correct
24 Correct 31 ms 10196 KB Output is correct
25 Correct 5 ms 7412 KB Output is correct
26 Correct 12 ms 8152 KB Output is correct
27 Correct 9 ms 8148 KB Output is correct
28 Correct 230 ms 29068 KB Output is correct
29 Correct 313 ms 37640 KB Output is correct
30 Correct 302 ms 36736 KB Output is correct
31 Correct 294 ms 36828 KB Output is correct
32 Incorrect 304 ms 37628 KB 1st lines differ - on the 1st token, expected: '99669450104468', found: '99667954366631'
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 122 ms 29268 KB Output is correct
2 Correct 122 ms 29272 KB Output is correct
3 Correct 129 ms 28464 KB Output is correct
4 Correct 131 ms 30208 KB Output is correct
5 Correct 162 ms 31608 KB Output is correct
6 Correct 175 ms 31692 KB Output is correct
7 Correct 155 ms 31688 KB Output is correct
8 Correct 160 ms 31720 KB Output is correct
9 Correct 218 ms 38356 KB Output is correct
10 Correct 117 ms 24028 KB Output is correct
11 Correct 242 ms 40936 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Correct 5 ms 7252 KB Output is correct
14 Correct 5 ms 7252 KB Output is correct
15 Correct 4 ms 7252 KB Output is correct
16 Correct 4 ms 7252 KB Output is correct
17 Correct 4 ms 7252 KB Output is correct
18 Correct 116 ms 29276 KB Output is correct
19 Correct 115 ms 29280 KB Output is correct
20 Correct 118 ms 29388 KB Output is correct
21 Correct 133 ms 29196 KB Output is correct
22 Correct 220 ms 38540 KB Output is correct
23 Correct 303 ms 48228 KB Output is correct
24 Correct 306 ms 48496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 49708 KB Output is correct
2 Correct 216 ms 55784 KB Output is correct
3 Correct 117 ms 29268 KB Output is correct
4 Correct 113 ms 29272 KB Output is correct
5 Correct 412 ms 73676 KB Output is correct
6 Correct 418 ms 58528 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 289 ms 57408 KB Output is correct
9 Correct 308 ms 65068 KB Output is correct
10 Correct 174 ms 49704 KB Output is correct
11 Correct 225 ms 55636 KB Output is correct
12 Correct 3 ms 7252 KB Output is correct
13 Correct 4 ms 7252 KB Output is correct
14 Correct 3 ms 7252 KB Output is correct
15 Correct 4 ms 7252 KB Output is correct
16 Correct 116 ms 29196 KB Output is correct
17 Correct 114 ms 29272 KB Output is correct
18 Correct 195 ms 49808 KB Output is correct
19 Correct 212 ms 55600 KB Output is correct
20 Correct 187 ms 42556 KB Output is correct
21 Correct 230 ms 45860 KB Output is correct
22 Correct 179 ms 42608 KB Output is correct
23 Correct 201 ms 45912 KB Output is correct
24 Correct 122 ms 29268 KB Output is correct
25 Correct 122 ms 29272 KB Output is correct
26 Correct 129 ms 28464 KB Output is correct
27 Correct 131 ms 30208 KB Output is correct
28 Correct 162 ms 31608 KB Output is correct
29 Correct 175 ms 31692 KB Output is correct
30 Correct 155 ms 31688 KB Output is correct
31 Correct 160 ms 31720 KB Output is correct
32 Correct 4 ms 7252 KB Output is correct
33 Correct 4 ms 7252 KB Output is correct
34 Correct 5 ms 7252 KB Output is correct
35 Correct 4 ms 7252 KB Output is correct
36 Correct 6 ms 7356 KB Output is correct
37 Correct 4 ms 7252 KB Output is correct
38 Correct 6 ms 7252 KB Output is correct
39 Correct 5 ms 7252 KB Output is correct
40 Correct 4 ms 7380 KB Output is correct
41 Correct 5 ms 7636 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7508 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7380 KB Output is correct
46 Correct 4 ms 7380 KB Output is correct
47 Correct 9 ms 7508 KB Output is correct
48 Correct 47 ms 11748 KB Output is correct
49 Correct 53 ms 11988 KB Output is correct
50 Correct 46 ms 11860 KB Output is correct
51 Correct 43 ms 11948 KB Output is correct
52 Correct 44 ms 11804 KB Output is correct
53 Correct 83 ms 16392 KB Output is correct
54 Correct 14 ms 8296 KB Output is correct
55 Correct 31 ms 10196 KB Output is correct
56 Correct 5 ms 7412 KB Output is correct
57 Correct 12 ms 8152 KB Output is correct
58 Correct 9 ms 8148 KB Output is correct
59 Correct 230 ms 29068 KB Output is correct
60 Correct 313 ms 37640 KB Output is correct
61 Correct 302 ms 36736 KB Output is correct
62 Correct 294 ms 36828 KB Output is correct
63 Incorrect 304 ms 37628 KB 1st lines differ - on the 1st token, expected: '99669450104468', found: '99667954366631'
64 Halted 0 ms 0 KB -