結果

問題 No.59 鉄道の旅
ユーザー rodearodea
提出日時 2019-10-05 15:15:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 2,390 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 115,348 KB
実行使用メモリ 15,480 KB
最終ジャッジ日時 2024-04-15 19:58:23
合計ジャッジ時間 3,059 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
15,328 KB
testcase_01 AC 11 ms
15,272 KB
testcase_02 AC 10 ms
15,480 KB
testcase_03 AC 12 ms
15,272 KB
testcase_04 AC 57 ms
15,388 KB
testcase_05 AC 10 ms
15,428 KB
testcase_06 AC 11 ms
15,388 KB
testcase_07 AC 11 ms
15,236 KB
testcase_08 AC 21 ms
15,296 KB
testcase_09 AC 16 ms
15,332 KB
testcase_10 AC 16 ms
15,336 KB
testcase_11 AC 15 ms
15,368 KB
testcase_12 AC 52 ms
15,264 KB
testcase_13 AC 49 ms
15,332 KB
testcase_14 AC 75 ms
15,444 KB
testcase_15 AC 10 ms
15,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const int MAX_W = 1000010;

struct SegmentTree{
private:
    int n;
    vector<int> node;

public:
    // Range Sum Query
    SegmentTree(vector<int> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = node[i*2+1] + node[i*2+2];
    }

    void add(int k, int val){
        k += (n - 1);
        node[k] += val;

        while(0 < k){
            k = (k - 1) / 2;
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }

    int getsum(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];

        int vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        int vr = getsum(a, b, 2*k+2, (l+r)/2, r);

        return vl + vr;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, k; cin>>n>>k;
    SegmentTree seg(vector<int>(MAX_W, 0));
    vector<int> cnt(MAX_W, 0);
    while(n--){
        int w; cin>>w;
        if(w > 0){
            if(seg.getsum(1, MAX_W) - seg.getsum(1, w) >= k) continue;
            seg.add(w, 1);
            cnt[w]++;
        }
        else{
            w = -w;
            if(0 < cnt[w]){
                seg.add(w, -1);
                cnt[w]--;
            }
        }
    }

    cout << seg.getsum(0, MAX_W) << endl;

    return 0;
}
0