結果

問題 No.59 鉄道の旅
ユーザー siro53siro53
提出日時 2019-09-04 00:46:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 2,369 bytes
コンパイル時間 1,878 ms
コンパイル使用メモリ 169,832 KB
実行使用メモリ 27,316 KB
最終ジャッジ日時 2023-08-26 17:19:16
合計ジャッジ時間 3,017 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
27,260 KB
testcase_01 AC 13 ms
27,316 KB
testcase_02 AC 13 ms
27,108 KB
testcase_03 AC 13 ms
27,116 KB
testcase_04 AC 81 ms
27,180 KB
testcase_05 AC 13 ms
27,256 KB
testcase_06 AC 13 ms
27,108 KB
testcase_07 AC 13 ms
27,180 KB
testcase_08 AC 19 ms
27,140 KB
testcase_09 AC 19 ms
27,232 KB
testcase_10 AC 18 ms
27,144 KB
testcase_11 AC 16 ms
27,232 KB
testcase_12 AC 43 ms
27,240 KB
testcase_13 AC 61 ms
27,124 KB
testcase_14 AC 46 ms
27,184 KB
testcase_15 AC 14 ms
27,248 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 EPS (1e-7)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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

struct RSQ
{
private:
    ll n;
    vector<ll> node;

public:
    RSQ(vector<ll> &v)
    {
        ll sz = v.size();
        n = 1;
        while (n < sz)
        {
            n *= 2;
        }
        node.resize(2 * n - 1, 0);
        for (ll i = 0; i < sz; i++)
        {
            node[i + n - 1] = v[i];
        }
        for (ll i = n - 2; i >= 0; i--)
        {
            node[i] = node[2 * i + 1] + node[2 * i + 2];
        }
    }

    void add(ll x, ll val)
    {
        x += (n - 1);
        node[x] += val;
        while (x > 0)
        {
            x = (x - 1) / 2;
            node[x] = node[2 * x + 1] + node[2 * x + 2];
        }
    }

    ll getsum(ll a, ll b, ll now = 0, ll l = 0, ll r = -1)
    {
        if (r < 0)
        {
            r = n;
        }
        if (r <= a || l >= b)
        {
            return 0;
        }
        if (a <= l && r <= b)
        {
            return node[now];
        }
        ll vl = getsum(a, b, 2 * now + 1, l, (l + r) / 2);
        ll vr = getsum(a, b, 2 * now + 2, (l + r) / 2, r);
        return vl + vr;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, k;
    cin >> n >> k;
    const ll mxw = 1000000;
    vector<ll> a(mxw + 1); // a[i] := 重さiの荷物の個数
    RSQ seg(a);
    for (ll i = 0; i < n; i++)
    {
        ll w;
        cin >> w;
        if (w > 0)
        {
            if (seg.getsum(w, mxw + 1) < k)
            {
                seg.add(w, 1);
            }
        }
        else
        {
            w = -w;
            if (seg.getsum(1, w + 1) - seg.getsum(1, w) > 0)
            {
                seg.add(w, -1);
            }
        }
    }
    ll ans = seg.getsum(1, mxw + 1);
    cout << ans << endl;
}
0