結果

問題 No.59 鉄道の旅
ユーザー snrnsidysnrnsidy
提出日時 2021-08-07 15:17:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 4,605 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 204,564 KB
実行使用メモリ 7,628 KB
最終ジャッジ日時 2023-10-18 23:13:28
合計ジャッジ時間 3,195 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,092 KB
testcase_01 AC 4 ms
7,092 KB
testcase_02 AC 4 ms
7,092 KB
testcase_03 AC 4 ms
7,092 KB
testcase_04 AC 20 ms
7,628 KB
testcase_05 AC 4 ms
7,092 KB
testcase_06 AC 4 ms
7,092 KB
testcase_07 AC 4 ms
7,092 KB
testcase_08 AC 6 ms
7,356 KB
testcase_09 AC 6 ms
7,356 KB
testcase_10 AC 6 ms
7,356 KB
testcase_11 AC 5 ms
7,356 KB
testcase_12 AC 11 ms
7,628 KB
testcase_13 AC 17 ms
7,628 KB
testcase_14 AC 17 ms
7,628 KB
testcase_15 AC 4 ms
7,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

using namespace std;

namespace atcoder {

    namespace internal {

#ifndef _MSC_VER
        template <class T>
        using is_signed_int128 =
            typename std::conditional<std::is_same<T, __int128_t>::value ||
            std::is_same<T, __int128>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using is_unsigned_int128 =
            typename std::conditional<std::is_same<T, __uint128_t>::value ||
            std::is_same<T, unsigned __int128>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using make_unsigned_int128 =
            typename std::conditional<std::is_same<T, __int128_t>::value,
            __uint128_t,
            unsigned __int128>;

        template <class T>
        using is_integral = typename std::conditional<std::is_integral<T>::value ||
            is_signed_int128<T>::value ||
            is_unsigned_int128<T>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using is_signed_int = typename std::conditional<(is_integral<T>::value&&
            std::is_signed<T>::value) ||
            is_signed_int128<T>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using is_unsigned_int =
            typename std::conditional<(is_integral<T>::value&&
                std::is_unsigned<T>::value) ||
            is_unsigned_int128<T>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using to_unsigned = typename std::conditional<
            is_signed_int128<T>::value,
            make_unsigned_int128<T>,
            typename std::conditional<std::is_signed<T>::value,
            std::make_unsigned<T>,
            std::common_type<T>>::type>::type;

#else

        template <class T> using is_integral = typename std::is_integral<T>;

        template <class T>
        using is_signed_int =
            typename std::conditional<is_integral<T>::value&& std::is_signed<T>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using is_unsigned_int =
            typename std::conditional<is_integral<T>::value&&
            std::is_unsigned<T>::value,
            std::true_type,
            std::false_type>::type;

        template <class T>
        using to_unsigned = typename std::conditional<is_signed_int<T>::value,
            std::make_unsigned<T>,
            std::common_type<T>>::type;

#endif

        template <class T>
        using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

        template <class T>
        using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

        template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

    }  // namespace internal

}  // namespace atcoder

namespace atcoder {

    // Reference: https://en.wikipedia.org/wiki/Fenwick_tree
    template <class T> struct fenwick_tree {
        using U = internal::to_unsigned_t<T>;

    public:
        fenwick_tree() : _n(0) {}
        explicit fenwick_tree(int n) : _n(n), data(n) {}

        void add(int p, T x) {
            assert(0 <= p && p < _n);
            p++;
            while (p <= _n) {
                data[p - 1] += U(x);
                p += p & -p;
            }
        }

        T sum(int l, int r) {
            assert(0 <= l && l <= r && r <= _n);
            return sum(r) - sum(l);
        }

    private:
        int _n;
        std::vector<U> data;

        U sum(int r) {
            U s = 0;
            while (r > 0) {
                s += data[r - 1];
                r -= r & -r;
            }
            return s;
        }
    };

}  // namespace atcoder

using namespace atcoder;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, k, t;
    vector <int> v;

    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        v.push_back(t);
    }

    fenwick_tree<int> tree(1000001);

    for (int i = 0; i < n; i++)
    {
        if (v[i] < 0)
        {
            int val = tree.sum(abs(v[i]), abs(v[i]) + 1);
            if (val > 0)
            {
                tree.add(abs(v[i]), -1);
            }
        }
        else
        {
            int val = tree.sum(abs(v[i]), 1000001);
            if (val < k)
            {
                tree.add(abs(v[i]), 1);
            }
        }
    }

    cout << tree.sum(0, 1000001) << '\n';
    return 0;
}
0