結果

問題 No.59 鉄道の旅
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-09-09 15:42:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 2,306 bytes
コンパイル時間 1,620 ms
コンパイル使用メモリ 171,084 KB
実行使用メモリ 11,528 KB
最終ジャッジ日時 2023-09-10 21:17:55
合計ジャッジ時間 3,199 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,272 KB
testcase_01 AC 5 ms
11,212 KB
testcase_02 AC 6 ms
11,268 KB
testcase_03 AC 5 ms
11,340 KB
testcase_04 AC 67 ms
11,240 KB
testcase_05 AC 5 ms
11,268 KB
testcase_06 AC 5 ms
11,392 KB
testcase_07 AC 5 ms
11,212 KB
testcase_08 AC 14 ms
11,404 KB
testcase_09 AC 11 ms
11,216 KB
testcase_10 AC 11 ms
11,280 KB
testcase_11 AC 10 ms
11,260 KB
testcase_12 AC 52 ms
11,212 KB
testcase_13 AC 56 ms
11,528 KB
testcase_14 AC 72 ms
11,224 KB
testcase_15 AC 6 ms
11,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

template<class Monoid> struct SegmentTree {
private:
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int n;
    vector<Monoid> node;
public:
    //m=size, f=[](M a,M b){return ;}
    SegmentTree(int m, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        n = 1; while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
    }

    SegmentTree(const vector<Monoid>& v, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        int sz = v.size();
        n = 1; while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        REP(i, sz) node[i + n - 1] = v[i];
        REPR(i, n - 2) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }

    void update(int x, int val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    // [a,b)
    Monoid get(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return UNITY;
        if (a <= l && r <= b) return node[k];
        int vl = get(a, b, 2 * k + 1, l, (r - l) / 2 + l);
        int vr = get(a, b, 2 * k + 2, (r - l) / 2 + l, r);
        return F(vl, vr);
    }

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

int main() {
    int n, k; cin >> n >> k;
    const int max_w = 1000000;
    SegmentTree<int> seg(max_w + 1, [](int a, int b) {return a + b; },0);
    REP(i, n) {
        int w; cin >> w;
        if (w > 0) {
            if (seg.get(w, max_w + 1) < k) {
                seg.update(w, seg[w] + 1);
            }
        }
        else {
            w = -w;
            if (seg[w] > 0) {
                seg.update(w, seg[w] - 1);
            }
        }
    }
    cout << seg.get(0, max_w + 1) << endl;
    getchar(); getchar();
}
0