結果

問題 No.59 鉄道の旅
ユーザー hornistyfhornistyf
提出日時 2020-01-08 22:49:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 1,479 bytes
コンパイル時間 1,151 ms
コンパイル使用メモリ 106,340 KB
実行使用メモリ 9,584 KB
最終ジャッジ日時 2023-08-15 00:51:19
合計ジャッジ時間 2,972 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 101 ms
8,728 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 18 ms
4,376 KB
testcase_13 AC 79 ms
8,580 KB
testcase_14 AC 91 ms
9,584 KB
testcase_15 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//yuki59.cpp
//Wed Jan  8 22:21:38 2020

#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;

int nodenum,seg[400000];

void init(int n){
	nodenum = 1;
	while (nodenum<n){
		nodenum*=2;
	}
	fill(seg,seg+nodenum,0);
}

void add(int index, int num){
	index += nodenum-1;
	if (num==1){
		seg[index]++;
	}else if (seg[index]>0){
		seg[index]--;
	}
	while(index>0){
		index = (index-1)/2;
		seg[index] = seg[index*2+1]+seg[index*2+2];
	}
}

int query(int a, int b, int k, int l, int r){
	if (r<=a || b<=l){
		return 0;
	}
	if (a<=l && r<=b){
		return seg[k];
	}else {
		int v1 = query(a,b,k*2+1,l,(l+r)/2);
		int v2 = query(a,b,k*2+2,(l+r)/2,r);
		return v1+v2;
	}
}

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

	int w[n];
	vector<int> weight;
	for (int i=0;i<n;i++){
		cin >> w[i];
		weight.push_back(abs(w[i]));
	}

	sort(weight.begin(),weight.end());
	weight.erase(unique(weight.begin(),weight.end()),weight.end());

	map<int,int> mp;
	for (int i=0;i<weight.size();i++){
		mp[weight[i]] = i;
	}

	init(weight.size());

	for (int i=0;i<n;i++){
		if (w[i]<0){
			add(mp[abs(w[i])],-1);
		}else {
			int thres = query(mp[w[i]],nodenum,0,0,nodenum);
			if (thres<k){
				add(mp[w[i]],1);
			}
		}
	}

	cout << query(0,nodenum,0,0,nodenum) << endl;
}
0