답안 #702564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702564 2023-02-24T12:19:40 Z boris_mihov 메기 농장 (IOI22_fish) C++17
9 / 100
1000 ms 173344 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[4*MAXM];
    SegmentTree()
    {
        std::fill(tree, tree + 4*MAXM, (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[2], minusL[2], minusIN[2];
 
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;
        }
    }

    llong max = 0;
    for (int x = n ; x >= 1 ; --x)
    {
        if (x + 3 <= n)
        {
            for (const Position &curr : pos[x + 3])
            {
                max = std::max(max, dp[curr.idx][0] + curr.left + curr.right);
            }
        }

        for (int type = 0 ; type < 2 ; ++type)
        {
            for (const Position &curr : pos[x])
            {
                if (x == n)
                {
                    minusL[type].update(curr.idx, dp[curr.idx][type] - curr.left + curr.right);
                    minusIN[type].update(curr.idx, dp[curr.idx][type] - curr.in + curr.right);
                    LR[type].update(curr.idx, dp[curr.idx][type] + curr.left + curr.right);
                    continue;
                }

                dp[curr.idx][type] = max;
                for (const Position &next : pos[x + 1])
                {
                    if (next.y < curr.y)
                    {
                        dp[curr.idx][type] = std::max(dp[curr.idx][type], dp[next.idx][1] + next.right - next.in);
                    } else
                    {
                        dp[curr.idx][type] = std::max(dp[curr.idx][type], dp[next.idx][0] + next.left + next.right - curr.in - curr.right);
                    }
                }
 
                if (x + 2 <= n)
                {
                    for (const Position &next : pos[x + 2])
                    {
                        if (next.y < curr.y)
                        {
                            dp[curr.idx][type] = std::max(dp[curr.idx][type], dp[next.idx][0] + next.right - next.left);
                        } else
                        {
                            dp[curr.idx][type] = std::max(dp[curr.idx][type], dp[next.idx][0] + next.left + next.right - curr.right);
                        }
                    }
                }
            }
        }
    }

    for (int x = std::min(n, 3) ; x >= 1 ; --x)
    {
        for (const Position &curr : pos[x])
        {
            max = std::max(max, dp[curr.idx][0] + curr.left + curr.right);
        }
    }
 
    return max;
}

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)
      |                    ~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 131260 KB Output is correct
2 Correct 92 ms 132508 KB Output is correct
3 Correct 48 ms 119968 KB Output is correct
4 Correct 48 ms 119976 KB Output is correct
5 Execution timed out 1091 ms 173344 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 120028 KB Output is correct
2 Execution timed out 1082 ms 144044 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 119964 KB Output is correct
2 Correct 48 ms 120012 KB Output is correct
3 Correct 77 ms 130828 KB Output is correct
4 Correct 68 ms 127380 KB Output is correct
5 Correct 105 ms 139596 KB Output is correct
6 Correct 100 ms 139576 KB Output is correct
7 Correct 104 ms 139608 KB Output is correct
8 Correct 107 ms 139592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 119944 KB Output is correct
2 Correct 49 ms 119964 KB Output is correct
3 Correct 48 ms 120004 KB Output is correct
4 Correct 47 ms 119936 KB Output is correct
5 Correct 47 ms 120036 KB Output is correct
6 Correct 46 ms 119988 KB Output is correct
7 Correct 47 ms 119952 KB Output is correct
8 Correct 46 ms 120012 KB Output is correct
9 Incorrect 46 ms 120136 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '254964121270'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 119944 KB Output is correct
2 Correct 49 ms 119964 KB Output is correct
3 Correct 48 ms 120004 KB Output is correct
4 Correct 47 ms 119936 KB Output is correct
5 Correct 47 ms 120036 KB Output is correct
6 Correct 46 ms 119988 KB Output is correct
7 Correct 47 ms 119952 KB Output is correct
8 Correct 46 ms 120012 KB Output is correct
9 Incorrect 46 ms 120136 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '254964121270'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 119944 KB Output is correct
2 Correct 49 ms 119964 KB Output is correct
3 Correct 48 ms 120004 KB Output is correct
4 Correct 47 ms 119936 KB Output is correct
5 Correct 47 ms 120036 KB Output is correct
6 Correct 46 ms 119988 KB Output is correct
7 Correct 47 ms 119952 KB Output is correct
8 Correct 46 ms 120012 KB Output is correct
9 Incorrect 46 ms 120136 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '254964121270'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 119964 KB Output is correct
2 Correct 48 ms 120012 KB Output is correct
3 Correct 77 ms 130828 KB Output is correct
4 Correct 68 ms 127380 KB Output is correct
5 Correct 105 ms 139596 KB Output is correct
6 Correct 100 ms 139576 KB Output is correct
7 Correct 104 ms 139608 KB Output is correct
8 Correct 107 ms 139592 KB Output is correct
9 Correct 126 ms 144312 KB Output is correct
10 Incorrect 100 ms 136472 KB 1st lines differ - on the 1st token, expected: '36454348383152', found: '37838158552678'
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 131260 KB Output is correct
2 Correct 92 ms 132508 KB Output is correct
3 Correct 48 ms 119968 KB Output is correct
4 Correct 48 ms 119976 KB Output is correct
5 Execution timed out 1091 ms 173344 KB Time limit exceeded
6 Halted 0 ms 0 KB -