結果

問題 No.59 鉄道の旅
ユーザー codershifthcodershifth
提出日時 2015-07-28 00:01:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 1,992 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 146,492 KB
実行使用メモリ 11,556 KB
最終ジャッジ日時 2023-08-26 05:40:14
合計ジャッジ時間 1,990 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,220 KB
testcase_01 AC 4 ms
11,376 KB
testcase_02 AC 4 ms
11,356 KB
testcase_03 AC 4 ms
11,232 KB
testcase_04 AC 19 ms
11,308 KB
testcase_05 AC 4 ms
11,340 KB
testcase_06 AC 4 ms
11,316 KB
testcase_07 AC 5 ms
11,240 KB
testcase_08 AC 5 ms
11,492 KB
testcase_09 AC 6 ms
11,244 KB
testcase_10 AC 6 ms
11,224 KB
testcase_11 AC 4 ms
11,324 KB
testcase_12 AC 10 ms
10,196 KB
testcase_13 AC 17 ms
11,556 KB
testcase_14 AC 17 ms
11,296 KB
testcase_15 AC 4 ms
11,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

class RailroadTrip {
public:
    void solve(void) {
            int N,K;
            cin>>N>>K;
            // 単純にシミュレーションすればよい
            // 駅数が最大 10^5 なので積み荷の載せ・降ろしの判定を高速でできればよい。

            // 次のクエリを log(N) くらいでできればよい
            //  * a[w] += 1
            //  * a[w]+a[w+1]+...+a[maxW] を計算
            // BIT を使う

            int maxW = (1<<(int)ceil(log2(1E+6)));

            // [1,maxW]
            vector<ll> bits(maxW+1,0);
            // a[i] に x を追加する
            auto add = [&](int i, int x) {
                assert(i>0);
                while (i <= maxW)
                {
                    bits[i] += x;
                    i += (i & -i);
                }
            };
            // i 以上の a[i] の和
            auto sum = [&](int i) {
                ll s = 0;
                --i; // 求めたいのは (a[1]+...+a[maxW]) - (a[1]+...+a[i-1]) なので
                while (i > 0)
                {
                    s += bits[i];
                    i -= (i & -i);
                }
                return bits[maxW] - s;
            };
            // O(N*log(N))
            REP(i,N)
            {
                ll w, aw;
                cin>>w;
                aw = abs(w);
                if (w > 0 && sum(w) < K)
                    add(w,1);
                else if (w < 0 && sum(aw)-sum(aw+1) > 0)
                    add(aw,-1);
            }
            cout<<sum(1)<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new RailroadTrip();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0