結果

問題 No.59 鉄道の旅
ユーザー ChanyuhChanyuh
提出日時 2019-10-07 23:36:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 2,507 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 105,268 KB
実行使用メモリ 35,304 KB
最終ジャッジ日時 2023-08-05 03:41:04
合計ジャッジ時間 2,658 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
35,008 KB
testcase_01 AC 16 ms
35,016 KB
testcase_02 AC 15 ms
35,092 KB
testcase_03 AC 16 ms
35,092 KB
testcase_04 AC 73 ms
34,932 KB
testcase_05 AC 16 ms
35,304 KB
testcase_06 AC 16 ms
35,144 KB
testcase_07 AC 16 ms
35,084 KB
testcase_08 AC 24 ms
35,016 KB
testcase_09 AC 21 ms
35,168 KB
testcase_10 AC 22 ms
35,016 KB
testcase_11 AC 20 ms
35,028 KB
testcase_12 AC 49 ms
35,156 KB
testcase_13 AC 54 ms
34,892 KB
testcase_14 AC 65 ms
35,096 KB
testcase_15 AC 16 ms
35,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct SegmentTree{
    private:
        int n;
        vector<ll> node; //セグ木の持つデータの型を入力
        ll e = 0; //単位元
    public:
        ll F(ll a,ll b){
            return a+b;//二項演算
        }

        void init(vector<ll> v) {
            int sz = v.size();
            n = 1; while(n < sz) n *= 2;
            node.resize(2*n-1, e);

            rep(i,sz) node[i+n-1] = v[i];
            per(i,n-1) node[i] = F(node[2*i+1], node[2*i+2]);

            //rep(i,2*n-1) cout << node[i] << endl;
        }

        void update(int x,ll val){
            x += n-1;
            node[x] = val;
            while(x > 0) {
                x =(x-1)/2;
                node[x]=F(node[2*x+1],node[2*x+2]);
            }
        
        }

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

            ll vl = get(a, b, 2*k+1, l, (l+r)/2);
            ll vr = get(a, b, 2*k+2, (l+r)/2, r);
            return F(vl, vr);
        }
};

int n,k;
vector<ll> a(1000001,0);

void solve(){
  cin >> n >> k;
  SegmentTree seg;seg.init(a);
  rep(i,n){
    int w;
    cin >> w;
    if (w>=1) {
      if (seg.get(w,1000001)<k) seg.update(w,seg.get(w,w+1)+1);
    }
    else{
      if (seg.get(-w,-w+1)!=0) seg.update(-w,seg.get(-w,-w+1)-1);
    }
  }
  cout << seg.get(0,1000001) << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0