結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  t8m8⛄️ | 
| 提出日時 | 2015-08-20 02:53:04 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 659 ms / 5,000 ms | 
| コード長 | 1,679 bytes | 
| コンパイル時間 | 3,805 ms | 
| コンパイル使用メモリ | 80,764 KB | 
| 実行使用メモリ | 56,160 KB | 
| 最終ジャッジ日時 | 2024-12-24 21:50:02 | 
| 合計ジャッジ時間 | 9,091 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 12 | 
ソースコード
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;
public class No0059 {
	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static final int SIZE = 1000001;
	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		FenwickTree ft = new FenwickTree(SIZE);
		while (n-- > 0) {
			int w = in.nextInt();
			if (w > 0) {
				if (ft.get(SIZE-1) - ft.get(w-1) < k) ft.add(w,SIZE,1);
			} else {
				w = -w;
				if (ft.get(w) - ft.get(w-1) > 0) ft.add(w,SIZE,-1);
			}
			//trace(ft);
		}
		//trace(ft);
		out.println(ft.get(SIZE-1));
	}
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		solve();
		out.flush();
		long end = System.currentTimeMillis();
		//trace(end-start + "ms");
		in.close();
		out.close();
	}
	static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
class FenwickTree {
	public static final long MOD = 1_000_000_007;
	public final int length;
	private long[] bit;
	public FenwickTree(int length) {
		this.length = length;
		this.bit = new long[length+2];
	}
	public void add(int begin, int end, long n) {
		add(begin, n);
		add(end, (MOD - n)%MOD);
	}
	private void add(int idx, long n) {
		idx++;
		while (idx <= length) {
			bit[idx] = (bit[idx] + n)%MOD;
			idx += idx&-idx;
		}
	}
	public long get(int idx) {
		idx++;
		long ret = 0;
		while (idx > 0) {
			ret = (ret + bit[idx])%MOD;
			idx -= idx&-idx;
		}
		return ret;
	}
	public String toString() {
		long[] val = new long[length];
		for (int i=0; i<length; i++) val[i] = get(i);
		return Arrays.toString(val);
	}
}
            
            
            
        