結果

問題 No.59 鉄道の旅
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-08-20 02:53:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 599 ms / 5,000 ms
コード長 1,679 bytes
コンパイル時間 3,419 ms
コンパイル使用メモリ 78,136 KB
実行使用メモリ 67,724 KB
最終ジャッジ日時 2023-08-26 06:37:23
合計ジャッジ時間 8,776 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
62,268 KB
testcase_01 AC 129 ms
61,856 KB
testcase_02 AC 128 ms
61,964 KB
testcase_03 AC 135 ms
62,120 KB
testcase_04 AC 594 ms
67,452 KB
testcase_05 AC 137 ms
61,780 KB
testcase_06 AC 145 ms
62,540 KB
testcase_07 AC 137 ms
61,920 KB
testcase_08 AC 272 ms
66,636 KB
testcase_09 AC 270 ms
66,248 KB
testcase_10 AC 283 ms
66,716 KB
testcase_11 AC 239 ms
65,740 KB
testcase_12 AC 490 ms
67,116 KB
testcase_13 AC 599 ms
67,440 KB
testcase_14 AC 561 ms
67,724 KB
testcase_15 AC 128 ms
61,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
	}
}
0