結果

問題 No.59 鉄道の旅
ユーザー imulanimulan
提出日時 2016-07-07 20:26:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 1,574 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 169,852 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2023-08-26 07:05:41
合計ジャッジ時間 2,624 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,436 KB
testcase_01 AC 6 ms
19,584 KB
testcase_02 AC 7 ms
19,520 KB
testcase_03 AC 6 ms
19,468 KB
testcase_04 AC 37 ms
19,432 KB
testcase_05 AC 6 ms
19,468 KB
testcase_06 AC 5 ms
19,580 KB
testcase_07 AC 6 ms
19,436 KB
testcase_08 AC 12 ms
19,412 KB
testcase_09 AC 8 ms
19,420 KB
testcase_10 AC 9 ms
19,424 KB
testcase_11 AC 8 ms
19,380 KB
testcase_12 AC 30 ms
19,432 KB
testcase_13 AC 27 ms
19,436 KB
testcase_14 AC 33 ms
19,464 KB
testcase_15 AC 6 ms
19,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N=1000000;

struct SumSegTree{
    long n; vector<ll> dat;
    //初期化
    SumSegTree(long _n){
        n=1;
        while(n<_n) n*=2;
        dat=vector<ll>(2*n-1,0);
    }
    //k番目(0-indexed)の値に+a
    //負にはしない
    void update(long k, ll a){
        k+=n-1;
        dat[k]=max(dat[k]+a,0LL);
        //更新
        while(k>0){
            k=(k-1)/2;
            dat[k]=dat[2*k+1]+dat[2*k+2];
        }
    }
    //内部的に投げられるクエリ
    ll _query(long a, long b, long k, long l, long r){
        if(r<=a || b<=l) return 0;

        if(a<=l && r<=b) return dat[k];
        else{
            ll vl=_query(a,b,2*k+1,l,(l+r)/2);
            ll vr=_query(a,b,2*k+2,(l+r)/2,r);
            return vl+vr;
        }
    }
    //[a,b)の和を求める
    ll query(long a, long b){
        return _query(a,b,0,0,n);
    }
};

int main()
{
    int n,k;
    cin >>n >>k;

    SumSegTree st(N+2);
    rep(i,n)
    {
        int w;
        scanf(" %d", &w);

        if(w>0)
        {
            if(st.query(w,N+1)<k) st.update(w,1);
        }
        else
        {
            st.update(-w,-1);
        }
        //cout << st.query(0,N+1) << endl;;
    }

    cout << st.query(0,N+1) << endl;;
    return 0;
}
0