結果

問題 No.59 鉄道の旅
ユーザー fura
提出日時 2020-06-07 12:32:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,025 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 193,004 KB
最終ジャッジ日時 2025-01-10 23:45:06
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:40:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |         int n,k; scanf("%d%d",&n,&k);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:43:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |                 int w; scanf("%d",&w);
      |                        ~~~~~^~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(int n):a(n){}
	void add(int i,G val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

int main(){
	int n,k; scanf("%d%d",&n,&k);
	Fenwick_tree<int> F(1e6+1);
	rep(i,n){
		int w; scanf("%d",&w);
		if(w>0){
			if(F.sum(w,1e6+1)<k) F.add(w,1);
		}
		else{
			if(F.sum(-w,-w+1)>0) F.add(-w,-1);
		}
	}
	printf("%d\n",F.sum(0,1e6+1));
	return 0;
}
0