Submission #702587

# Submission time Handle Problem Language Result Execution time Memory
702587 2023-02-24T13:35:39 Z boris_mihov Catfish Farm (IOI22_fish) C++17
64 / 100
880 ms 193628 KB
#include "fish.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
 
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXM = 600000 + 10;
 
struct Position
{
    int y;
    int idx;
    llong in;
    llong left;
    llong right;
 
    Position(int _y, int _idx, llong _in, llong _left, llong _right)
    {
        y = _y;
        idx = _idx;
        left = _left;
        right = _right;
        in = _in;
    }
};
 
int cnt;
struct SegmentTree
{
    llong tree[1 << 20];
    SegmentTree()
    {
        std::fill(tree, tree + (1 << 20), (llong)-1e18);
    }
 
    void update(int l, int r, int node, int queryIdx, llong value)
    {
        if (l == r)
        {
            tree[node] = value;
            return;
        }
 
        int mid = (l + r) / 2;
        if (queryIdx <= mid) update(l, mid, 2*node, queryIdx, value);
        else update(mid + 1, r, 2*node + 1, queryIdx, value);
        tree[node] = std::max(tree[2*node], tree[2*node + 1]);
    }
 
    llong query(int l, int r, int node, int queryL, int queryR)
    {
        if (queryL <= l && r <= queryR)
        {
            return tree[node];
        }
 
        llong ans = -1e18;
        int mid = (l + r) / 2;
        if (queryL <= mid) ans = std::max(ans, query(l, mid, 2*node, queryL, queryR));
        if (mid + 1 <= queryR) ans = std::max(ans, query(mid + 1, r, 2*node + 1, queryL, queryR));
        return ans; 
    }
 
    void update(int pos, llong value)
    {
        update(1, cnt, 1, pos, value);
    }
 
    llong query(int l, int r)
    {
        return query(1, cnt, 1, l, r);
    }
};
 
int n, m;
llong dp[MAXM][2];
std::vector <Position> pos[MAXN];
std::vector <Position> posUncleared[MAXN];
std::vector <std::pair <int,int>> fish[MAXN];
SegmentTree LR, minusL, minusIN;
 
llong max_weights(int N, int M, std::vector <int> X, std::vector <int> Y, std::vector <int> W) 
{
    n = N; m = M;   
    for (int i = 0 ; i < m ; ++i)
    {
        fish[X[i] + 1].emplace_back(Y[i] + 1, W[i]);
    }
 
    for (int x = 1 ; x <= n ; ++x)
    {
        std::sort(fish[x].begin(), fish[x].end());
        for (const auto &[y, w] : fish[x])
        {
            if (x != 1)
            {
                posUncleared[x - 1].push_back({y, 0, 0LL, 0LL, 0LL});
            }
 
            if (x != n)
            {
                posUncleared[x + 1].push_back({y, 0, 0LL, 0LL, 0LL});
            }
        }
    }
 
    for (int x = 1 ; x <= n ; ++x)
    {
        std::sort(posUncleared[x].begin(), posUncleared[x].end(), [&](Position a, Position b)
        {
            return a.y < b.y;
        });
        
        for (Position p : posUncleared[x])
        {
            if (!pos[x].empty() && pos[x].back().y == p.y)
            {
                continue;
            }
 
            pos[x].push_back(p);
        }
 
        int ptrL = 0;
        int ptrR = 0;
        int ptrIN = 0;
        llong inSum = 0;
        llong leftSum = 0;
        llong rightSum = 0;
        for (Position &p : pos[x])
        {
            while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
            {
                leftSum += fish[x - 1][ptrL].second;
                ptrL++;
            }
 
            while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
            {
                rightSum += fish[x + 1][ptrR].second;
                ptrR++;
            }
 
            while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
            {
                inSum += fish[x][ptrIN].second;
                ptrIN++;
            }
 
            p.in = inSum;
            p.left = leftSum;
            p.right = rightSum;
        }
    }
    
    for (int x = 1 ; x <= n ; ++x)
    {
        for (Position &p : pos[x])
        {
            p.idx = ++cnt;
        }
    }

    for (int x = n ; x >= 1 ; --x)
    {
        for (int type = 0 ; type < 2 ; ++type)
        {
            for (const Position &curr : pos[x])
            {
                if (x == n) continue;
                int l = -1, r = pos[x + 1].size(), mid;
                while (l < r - 1)
                {
                    mid = (l + r) / 2;
                    if (pos[x + 1][mid].y < curr.y) l = mid;
                    else r = mid;
                }
 
                if (l >= 0)
                {
                    dp[curr.idx][type] = std::max(dp[curr.idx][type], minusIN.query(pos[x + 1][0].idx, pos[x + 1][l].idx));
                }
 
                if (r < pos[x + 1].size() && type == 0)
                {
                    dp[curr.idx][type] = std::max(dp[curr.idx][type], LR.query(pos[x + 1][r].idx, pos[x + 1].back().idx) - curr.in - curr.right);
                }
 
                if (x + 2 <= n)
                {
                    l = -1;
                    r = pos[x + 2].size();
                    while (l < r - 1)
                    {
                        mid = (l + r) / 2;
                        if (pos[x + 2][mid].y < curr.y) l = mid;
                        else r = mid;
                    }
 
                    if (l >= 0)
                    {
                        dp[curr.idx][type] = std::max(dp[curr.idx][type], minusL.query(pos[x + 2][0].idx, pos[x + 2][l].idx));
                    }
 
                    if (r < pos[x + 2].size())
                    {
                        dp[curr.idx][type] = std::max(dp[curr.idx][type], LR.query(pos[x + 2][r].idx, pos[x + 2].back().idx) - curr.right);
                    }
                }
 
                int firstIdx = pos[x].back().idx + 1;
                if (x + 1 <= n && !pos[x + 1].empty())
                {
                    firstIdx = pos[x + 1].back().idx + 1;
                }
 
                if (x + 2 <= n && !pos[x + 2].empty())
                {
                    firstIdx = pos[x + 2].back().idx + 1;
                }
 
                if (firstIdx <= cnt)
                {
                    dp[curr.idx][type] = std::max(dp[curr.idx][type], LR.query(firstIdx, cnt));
                }
            }

            for (const Position &curr : pos[x])
            {
                minusIN.update(curr.idx, dp[curr.idx][1] - curr.in + curr.right);
                LR.update(curr.idx, dp[curr.idx][0] + curr.left + curr.right);
                minusL.update(curr.idx, dp[curr.idx][0] + curr.right);
            }
        }
    }
 
    return LR.query(1, cnt);
}

Compilation message

fish.cpp: In function 'llong max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:135:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |             while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:141:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |             while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:147:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |             while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
      |                    ~~~~~~^~~~~~~~~~~~~~~~
fish.cpp:187:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |                 if (r < pos[x + 1].size() && type == 0)
      |                     ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:208:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  208 |                     if (r < pos[x + 2].size())
      |                         ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 82 ms 43196 KB Output is correct
2 Correct 104 ms 44312 KB Output is correct
3 Correct 15 ms 31892 KB Output is correct
4 Correct 16 ms 31872 KB Output is correct
5 Runtime error 339 ms 174812 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 31980 KB Output is correct
2 Correct 373 ms 55824 KB Output is correct
3 Correct 435 ms 60432 KB Output is correct
4 Correct 86 ms 43112 KB Output is correct
5 Correct 101 ms 44320 KB Output is correct
6 Correct 16 ms 31956 KB Output is correct
7 Correct 14 ms 31964 KB Output is correct
8 Correct 13 ms 31936 KB Output is correct
9 Correct 13 ms 31956 KB Output is correct
10 Correct 17 ms 32084 KB Output is correct
11 Correct 15 ms 31956 KB Output is correct
12 Correct 168 ms 48252 KB Output is correct
13 Correct 218 ms 50628 KB Output is correct
14 Correct 149 ms 44752 KB Output is correct
15 Correct 200 ms 45616 KB Output is correct
16 Correct 166 ms 44736 KB Output is correct
17 Correct 172 ms 45496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31956 KB Output is correct
2 Correct 16 ms 31896 KB Output is correct
3 Correct 92 ms 42764 KB Output is correct
4 Correct 68 ms 39344 KB Output is correct
5 Correct 165 ms 51480 KB Output is correct
6 Correct 148 ms 51448 KB Output is correct
7 Correct 154 ms 51528 KB Output is correct
8 Correct 157 ms 51448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 31956 KB Output is correct
2 Correct 13 ms 31956 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 14 ms 31932 KB Output is correct
5 Correct 13 ms 31956 KB Output is correct
6 Correct 14 ms 31956 KB Output is correct
7 Correct 14 ms 31860 KB Output is correct
8 Correct 14 ms 31956 KB Output is correct
9 Correct 15 ms 32068 KB Output is correct
10 Correct 17 ms 32256 KB Output is correct
11 Correct 16 ms 32212 KB Output is correct
12 Correct 16 ms 32116 KB Output is correct
13 Correct 14 ms 31972 KB Output is correct
14 Correct 15 ms 32084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 31956 KB Output is correct
2 Correct 13 ms 31956 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 14 ms 31932 KB Output is correct
5 Correct 13 ms 31956 KB Output is correct
6 Correct 14 ms 31956 KB Output is correct
7 Correct 14 ms 31860 KB Output is correct
8 Correct 14 ms 31956 KB Output is correct
9 Correct 15 ms 32068 KB Output is correct
10 Correct 17 ms 32256 KB Output is correct
11 Correct 16 ms 32212 KB Output is correct
12 Correct 16 ms 32116 KB Output is correct
13 Correct 14 ms 31972 KB Output is correct
14 Correct 15 ms 32084 KB Output is correct
15 Correct 15 ms 31956 KB Output is correct
16 Correct 18 ms 32392 KB Output is correct
17 Correct 123 ms 40032 KB Output is correct
18 Correct 112 ms 40504 KB Output is correct
19 Correct 111 ms 41164 KB Output is correct
20 Correct 104 ms 40868 KB Output is correct
21 Correct 100 ms 40780 KB Output is correct
22 Correct 193 ms 47492 KB Output is correct
23 Correct 41 ms 34012 KB Output is correct
24 Correct 102 ms 38564 KB Output is correct
25 Correct 17 ms 32220 KB Output is correct
26 Correct 38 ms 33740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 31956 KB Output is correct
2 Correct 13 ms 31956 KB Output is correct
3 Correct 15 ms 31956 KB Output is correct
4 Correct 14 ms 31932 KB Output is correct
5 Correct 13 ms 31956 KB Output is correct
6 Correct 14 ms 31956 KB Output is correct
7 Correct 14 ms 31860 KB Output is correct
8 Correct 14 ms 31956 KB Output is correct
9 Correct 15 ms 32068 KB Output is correct
10 Correct 17 ms 32256 KB Output is correct
11 Correct 16 ms 32212 KB Output is correct
12 Correct 16 ms 32116 KB Output is correct
13 Correct 14 ms 31972 KB Output is correct
14 Correct 15 ms 32084 KB Output is correct
15 Correct 15 ms 31956 KB Output is correct
16 Correct 18 ms 32392 KB Output is correct
17 Correct 123 ms 40032 KB Output is correct
18 Correct 112 ms 40504 KB Output is correct
19 Correct 111 ms 41164 KB Output is correct
20 Correct 104 ms 40868 KB Output is correct
21 Correct 100 ms 40780 KB Output is correct
22 Correct 193 ms 47492 KB Output is correct
23 Correct 41 ms 34012 KB Output is correct
24 Correct 102 ms 38564 KB Output is correct
25 Correct 17 ms 32220 KB Output is correct
26 Correct 38 ms 33740 KB Output is correct
27 Correct 22 ms 32784 KB Output is correct
28 Correct 552 ms 69096 KB Output is correct
29 Correct 880 ms 86860 KB Output is correct
30 Runtime error 638 ms 193628 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31956 KB Output is correct
2 Correct 16 ms 31896 KB Output is correct
3 Correct 92 ms 42764 KB Output is correct
4 Correct 68 ms 39344 KB Output is correct
5 Correct 165 ms 51480 KB Output is correct
6 Correct 148 ms 51448 KB Output is correct
7 Correct 154 ms 51528 KB Output is correct
8 Correct 157 ms 51448 KB Output is correct
9 Correct 281 ms 56144 KB Output is correct
10 Correct 172 ms 48376 KB Output is correct
11 Correct 344 ms 64796 KB Output is correct
12 Correct 17 ms 31956 KB Output is correct
13 Correct 14 ms 31956 KB Output is correct
14 Correct 14 ms 31956 KB Output is correct
15 Correct 14 ms 31956 KB Output is correct
16 Correct 14 ms 31916 KB Output is correct
17 Correct 17 ms 31956 KB Output is correct
18 Correct 16 ms 31864 KB Output is correct
19 Correct 15 ms 31892 KB Output is correct
20 Correct 15 ms 31956 KB Output is correct
21 Correct 16 ms 31896 KB Output is correct
22 Correct 306 ms 56068 KB Output is correct
23 Correct 549 ms 72644 KB Output is correct
24 Correct 582 ms 73816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 43196 KB Output is correct
2 Correct 104 ms 44312 KB Output is correct
3 Correct 15 ms 31892 KB Output is correct
4 Correct 16 ms 31872 KB Output is correct
5 Runtime error 339 ms 174812 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -