답안 #701585

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
701585 2023-02-21T14:18:12 Z boris_mihov 메기 농장 (IOI22_fish) C++17
컴파일 오류
0 ms 0 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());
        for (Position p : posUncleared[x])
        {
            if (!pos[x].empty() && pos[x].back().y == p.y)
            {
                continue;
            }

            p.idx = ++cnt;
            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 = n ; x >= 1 ; --x)
    {
        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;
                }

                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[1].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[0].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[0].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[0].query(pos[x + 2][r].idx, pos[x + 2].back().idx) - curr.right);
                    }
                }

                int firstIdx = pos[x].back().idx + 1;
                if (!pos[x + 1].empty())
                {
                    firstIdx = pos[x + 1].back().idx + 1;
                }

                if (!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[0].query(firstIdx, cnt));
                }

                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);
            }
        }
    }

    return LR[0].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:132: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]
  132 |             while (ptrL < fish[x - 1].size() && fish[x - 1][ptrL].first <= p.y)
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:138: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]
  138 |             while (ptrR < fish[x + 1].size() && fish[x + 1][ptrR].first <= p.y)
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:144: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]
  144 |             while (ptrIN < fish[x].size() && fish[x][ptrIN].first <= p.y)
      |                    ~~~~~~^~~~~~~~~~~~~~~~
fish.cpp:183:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |                 if (r < pos[x + 1].size() && type == 0)
      |                     ~~^~~~~~~~~~~~~~~~~~~
fish.cpp:204:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Position>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |                     if (r < pos[x + 2].size())
      |                         ~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Iterator2 = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >]':
/usr/include/c++/10/bits/stl_algo.h:82:17:   required from 'void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1924:34:   required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1958:38:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1974:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:4859:18:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >]'
fish.cpp:112:65:   required from here
/usr/include/c++/10/bits/predefined_ops.h:43:23: error: no match for 'operator<' (operand types are 'Position' and 'Position')
   43 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
 1096 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:43:23: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   43 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)'
 1104 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:43:23: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   43 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = Position; _Iterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >]':
/usr/include/c++/10/bits/stl_algo.h:1826:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1854:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1886:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1977:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:4859:18:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >]'
fish.cpp:112:65:   required from here
/usr/include/c++/10/bits/predefined_ops.h:96:22: error: no match for 'operator<' (operand types are 'Position' and 'Position')
   96 |       { return __val < *__it; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
 1096 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:96:22: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   96 |       { return __val < *__it; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)'
 1104 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:96:22: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   96 |       { return __val < *__it; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Value = Position]':
/usr/include/c++/10/bits/stl_heap.h:139:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Distance = long int; _Tp = Position; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
/usr/include/c++/10/bits/stl_heap.h:246:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Distance = long int; _Tp = Position; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_heap.h:355:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1666:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1937:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1953:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:1974:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/include/c++/10/bits/stl_algo.h:4859:18:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Position*, std::vector<Position> >]'
fish.cpp:112:65:   required from here
/usr/include/c++/10/bits/predefined_ops.h:67:22: error: no match for 'operator<' (operand types are 'Position' and 'Position')
   67 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
 1096 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1096:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:67:22: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   67 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:67,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)'
 1104 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/10/bits/stl_iterator.h:1104:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/vector:60,
                 from fish.h:1,
                 from fish.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h:67:22: note:   'Position' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   67 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~