結果

問題 No.59 鉄道の旅
ユーザー koprickykopricky
提出日時 2017-08-07 11:12:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 82 ms / 5,000 ms
コード長 2,846 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 166,980 KB
実行使用メモリ 23,828 KB
最終ジャッジ日時 2024-04-20 03:22:25
合計ジャッジ時間 3,086 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
23,452 KB
testcase_01 AC 12 ms
23,420 KB
testcase_02 AC 12 ms
23,388 KB
testcase_03 AC 12 ms
23,340 KB
testcase_04 AC 82 ms
23,764 KB
testcase_05 AC 13 ms
23,364 KB
testcase_06 AC 12 ms
23,284 KB
testcase_07 AC 12 ms
23,276 KB
testcase_08 AC 21 ms
23,256 KB
testcase_09 AC 20 ms
23,440 KB
testcase_10 AC 20 ms
23,456 KB
testcase_11 AC 16 ms
23,400 KB
testcase_12 AC 42 ms
23,828 KB
testcase_13 AC 67 ms
23,616 KB
testcase_14 AC 69 ms
23,676 KB
testcase_15 AC 13 ms
23,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

template<class V> class segtree {
private:
    int n,sz;
    vector<V> node, lazy;

public:
    segtree(vector<V> v) {
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);
        rep(i,sz){
            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 eval(int k, int l, int r) {
        if(lazy[k] != 0) {
            node[k] += lazy[k];
            if(r - l > 1) {
                lazy[2*k+1] += lazy[k] / 2;
                lazy[2*k+2] += lazy[k] / 2;
            }
            lazy[k] = 0;
        }
    }
    void range(int a, int b, V x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b) {
            lazy[k] += (r - l) * x;
            eval(k, l, r);
        }
        else {
            range(a, b, x, 2*k+1, l, (l+r)/2);
            range(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }
    V query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return 0;
        }
        if(a <= l && r <= b){
            return node[k];
        }
        V vl = query(a, b, 2*k+1, l, (l+r)/2);
        V vr = query(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,K;
    cin >> n >> K;
    vector<int> vec(n,0);
    rep(i,n){
        cin >> vec[i];
    }
    segtree<int> st(vector<int>(1000000,0));
    rep(i,n){
        if(vec[i] > 0){
            int cnt = st.query(vec[i],1000001);
            if(cnt < K){
                st.range(vec[i],vec[i]+1,1);
            }
        }else{
            int cnt = st.query(-vec[i],-vec[i]+1);
            if(cnt){
                st.range(-vec[i],-vec[i]+1,-1);
            }
        }
    }
    cout << st.query(1,1000001) << endl;
    return 0;
}
0