結果

問題 No.59 鉄道の旅
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-08-20 02:25:19
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,643 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 75,488 KB
実行使用メモリ 68,480 KB
最終ジャッジ日時 2023-08-26 06:36:45
合計ジャッジ時間 7,439 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
62,040 KB
testcase_01 AC 124 ms
61,632 KB
testcase_02 AC 125 ms
61,864 KB
testcase_03 AC 134 ms
62,540 KB
testcase_04 AC 567 ms
67,484 KB
testcase_05 WA -
testcase_06 AC 147 ms
62,268 KB
testcase_07 WA -
testcase_08 AC 270 ms
68,480 KB
testcase_09 AC 278 ms
66,524 KB
testcase_10 WA -
testcase_11 AC 253 ms
65,420 KB
testcase_12 AC 450 ms
67,156 KB
testcase_13 AC 607 ms
67,632 KB
testcase_14 WA -
testcase_15 AC 124 ms
62,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

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

		out.println(ft.get(SIZE));
	}

	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