結果

問題 No.59 鉄道の旅
ユーザー face4face4
提出日時 2018-09-26 14:26:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,107 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 68,596 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-19 02:10:43
合計ジャッジ時間 1,738 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,168 KB
testcase_01 AC 5 ms
7,168 KB
testcase_02 AC 4 ms
7,296 KB
testcase_03 AC 4 ms
7,168 KB
testcase_04 AC 42 ms
11,136 KB
testcase_05 AC 5 ms
7,296 KB
testcase_06 AC 3 ms
7,296 KB
testcase_07 AC 5 ms
7,168 KB
testcase_08 AC 10 ms
11,136 KB
testcase_09 AC 11 ms
10,624 KB
testcase_10 AC 11 ms
10,496 KB
testcase_11 AC 5 ms
7,296 KB
testcase_12 AC 17 ms
7,168 KB
testcase_13 AC 40 ms
7,424 KB
testcase_14 AC 36 ms
7,680 KB
testcase_15 AC 5 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

typedef long long ll;

struct BIT{
    // 1-origin
    vector<int> v;
    int n;

    BIT(int _n):
        v(vector<int>(_n+1, 0)), n(_n) {}

    int sum(int i){
        int s = 0;
        while(i > 0){
            s += v[i];
            i -= lsb(i);
        }
        return s;
    }
    
    // 閉区間[l, r]の和
    int sum(int l, int r){
        return sum(r) - sum(l-1);
    }

    void add(int i, int x){
        while(i <= n){
            v[i] += x;
            i += lsb(i);
        }
    }

private:
    // least significant bit
    int lsb(int i){
        return i & -i;
    }
};

#define MAX 1000000
int cnt[MAX] = {};

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

    BIT bit(MAX);

    for(int i = 0; i < n; i++){
        cin >> w;
        if(w > 0){
            if(bit.sum(w, MAX) < k){
                bit.add(w, 1);
                cnt[w]++;
            }
        }else{
            if(cnt[-w]){
                cnt[-w]--;
                bit.add(-w, -1);
            }
        }
    }

    cout << bit.sum(1,MAX) << endl;
}

0