結果

問題 No.59 鉄道の旅
ユーザー siro53siro53
提出日時 2019-09-06 02:04:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 2,672 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 172,768 KB
実行使用メモリ 27,272 KB
最終ジャッジ日時 2023-09-05 06:46:56
合計ジャッジ時間 3,343 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
26,992 KB
testcase_01 AC 15 ms
27,052 KB
testcase_02 AC 14 ms
27,080 KB
testcase_03 AC 15 ms
27,012 KB
testcase_04 AC 56 ms
27,164 KB
testcase_05 AC 14 ms
27,136 KB
testcase_06 AC 15 ms
27,116 KB
testcase_07 AC 14 ms
27,172 KB
testcase_08 AC 21 ms
27,272 KB
testcase_09 AC 17 ms
27,168 KB
testcase_10 AC 18 ms
27,080 KB
testcase_11 AC 18 ms
27,164 KB
testcase_12 AC 50 ms
27,172 KB
testcase_13 AC 44 ms
27,068 KB
testcase_14 AC 59 ms
27,224 KB
testcase_15 AC 15 ms
27,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = INT_MAX;
const ll LLINF = INT64_MAX;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

template <typename Monoid>
struct SegmentTree
{
    using F = function<Monoid(Monoid, Monoid)>;

private:
    int n;
    vector<Monoid> node;
    Monoid E;
    F f;

public:
    SegmentTree(vector<Monoid> &v, Monoid e, const F func) : f(func), E(e)
    {
        int sz = v.size();
        n = 1;
        while (n < sz)
        {
            n *= 2;
        }
        node.resize(2 * n - 1, E);
        for (int i = 0; i < sz; i++)
        {
            node[i + n - 1] = v[i];
        }
        for (int i = n - 2; i >= 0; i--)
        {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    void update(int i, Monoid val)
    {
        i += (n - 1);
        node[i] = val;
        while (i > 0)
        {
            i = (i - 1) / 2;
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    Monoid query(int a, int b, int i = 0, int l = 0, int r = -1)
    {
        if (r < 0)
        {
            r = n;
        }
        if (r <= a || b <= l)
        {
            return E;
        }
        if (a <= l && r <= b)
        {
            return node[i];
        }
        Monoid vl = query(a, b, 2 * i + 1, l, (l + r) / 2);
        Monoid vr = query(a, b, 2 * i + 2, (l + r) / 2, r);
        return f(vl, vr);
    }

    Monoid operator[](const int &i) const
    {
        return node[i + n - 1];
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    const int mxw = 1000000;
    vector<ll> a(mxw + 1);
    SegmentTree<ll> seg(a, 0, [](ll a, ll b) { return a + b; });
    int n;
    ll k;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        ll w;
        cin >> w;
        if (w > 0)
        {
            if (seg.query(w, mxw + 1) < k)
            {
                seg.update(w, seg[w] + 1);
            }
        }
        else
        {
            w = -w;
            if (seg[w] > 0)
            {
                seg.update(w, seg[w] - 1);
            }
        }
    }
    ll ans = seg.query(1, mxw + 1);
    cout << ans << endl;
}
0