結果

問題 No.59 鉄道の旅
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-11-22 18:24:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 5,000 ms
コード長 2,731 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 176,808 KB
実行使用メモリ 40,708 KB
最終ジャッジ日時 2023-08-26 07:35:39
合計ジャッジ時間 2,968 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
35,844 KB
testcase_01 AC 11 ms
35,980 KB
testcase_02 AC 12 ms
35,812 KB
testcase_03 AC 12 ms
35,900 KB
testcase_04 AC 106 ms
39,860 KB
testcase_05 AC 12 ms
35,812 KB
testcase_06 AC 12 ms
35,808 KB
testcase_07 AC 13 ms
35,856 KB
testcase_08 AC 22 ms
36,120 KB
testcase_09 AC 19 ms
36,116 KB
testcase_10 AC 21 ms
36,136 KB
testcase_11 AC 14 ms
35,920 KB
testcase_12 AC 34 ms
36,028 KB
testcase_13 AC 74 ms
39,556 KB
testcase_14 AC 86 ms
40,708 KB
testcase_15 AC 12 ms
35,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        auto i = vs.begin();
        os << "[" << *i;
        for (++i; i != vs.end(); ++i) os << " " << *i;
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, K;
    void input() {
        cin >> N >> K;
    }

    typedef long long Long;
    /* 蛹コ髢薙∈縺ョ荳€讒伜刈邂励↓蟇セ蠢懊@縺欒angeSumQuery */
    struct RangeSumQuery2 {
        // root縺ョindex縺ッ1
        int N;
        vector<Long> V, L;
        static int FindPow2(int x) {
            int x1 = 1;
            while (x1 < x) x1 *= 2;
            return x1;
        }
        RangeSumQuery2(int N_) : N(FindPow2(N_)) {
            V = vector<Long>(N * 2, 0);
            L = vector<Long>(N * 2, 0);
        }
        /* [s, t)縺ォ荳€讒倥↓x繧貞刈邂・*/
        void add(Long x, int s, int t) { return add(x, s, t, 0, N, 1); }
        void add(Long x, int s, int t, int a, int b, int k) {
            if (t <= a || b <= s) return;
            if (s <= a && b <= t) {
                L[k] += x;
                return;
            }
            if (s < b || a < t) {
                V[k] += (min(t, b) - max(s, a)) * x;
                if (b - a == 1) return;
                int m = (a + b) / 2;
                add(x, s, t, a, m, k * 2);
                add(x, s, t, m, b, k * 2 + 1);
            }
        }
        /* [s, t)縺ョ蜥・*/
        Long query(int s, int t) { return query(s, t, 0, N, 1); }
        Long query(int s, int t, int a, int b, int k) {
            if (t <= a || b <= s) return 0;
            if (s <= a && b <= t) return V[k] + L[k] * (b - a);
            int m = (a + b) / 2;
            return (min(t, b) - max(s, a)) * L[k] +
                query(s, t, a, m, k * 2) +
                query(s, t, m, b, k * 2 + 1);
        }
    };

    void solve() {
        RangeSumQuery2 rsq(1000000 + 1);
        map<int, int> m;
        for (int i = 0; i < N; i++) {
            int W; cin >> W;
            if (W < 0) {
                W = -W;
                if (m[W] == 0) continue;
                rsq.add(-1, 0, W + 1);
                m[W]--;
            } else {
                if (rsq.query(W, W + 1) >= K) continue;
                m[W]++;
                rsq.add(1, 0, W + 1);
            }
        }
        cout << rsq.query(0, 1) << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0