結果

問題 No.59 鉄道の旅
ユーザー GrenacheGrenache
提出日時 2016-05-18 05:41:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 250 ms / 5,000 ms
コード長 2,334 bytes
コンパイル時間 3,316 ms
コンパイル使用メモリ 74,932 KB
実行使用メモリ 63,564 KB
最終ジャッジ日時 2023-08-26 07:02:34
合計ジャッジ時間 5,848 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
56,076 KB
testcase_01 AC 44 ms
56,080 KB
testcase_02 AC 45 ms
55,940 KB
testcase_03 AC 47 ms
55,884 KB
testcase_04 AC 250 ms
63,564 KB
testcase_05 AC 44 ms
56,048 KB
testcase_06 AC 45 ms
55,868 KB
testcase_07 AC 47 ms
55,880 KB
testcase_08 AC 115 ms
59,764 KB
testcase_09 AC 117 ms
59,788 KB
testcase_10 AC 114 ms
59,352 KB
testcase_11 AC 103 ms
59,744 KB
testcase_12 AC 173 ms
62,732 KB
testcase_13 AC 216 ms
62,544 KB
testcase_14 AC 225 ms
63,272 KB
testcase_15 AC 44 ms
56,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder59 {

    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Printer pr = new Printer(System.out);

		final int MAX = 1_000_000;

		int n = sc.nextInt();
		int k = sc.nextInt();

		SegmentTree st = new SegmentTree(MAX);

		for (int i = 0; i < n; i++) {
			int w = sc.nextInt();

			if (w > 0) {
				if (st.query(w - 1, MAX) < k) {
					st.update(w - 1, 1);
				}
			} else {
				w = -w;
				if (st.query(w - 1, w) > 0) {
					st.update(w - 1, -1);
				}
			}
		}

		pr.println(st.query(0, MAX));

		pr.close();
		sc.close();
    }

	private static class SegmentTree {
		int[] st;
		int n;

		SegmentTree(int n) {
			this.n = 1;
			while (this.n < n) {
				this.n *= 2;
			}
			st = new int[2 * this.n - 1];
		}

		// i:0-indexed
		void update(int i, int x) {
			i = n - 1 + i;
			st[i] += x;
			while (i > 0) {
				i = (i - 1) / 2;
				st[i] += x;
			}
		}

		// a, b:0-indexed
		// [a, b)
		int query(int a, int b) {
			return query(a, b, 0, 0, n);
		}

		private int query(int a, int b, int i, int l, int r) {
			if (a >= r || b <= l) {
				return 0;
			}
			if (a <= l && b >= r) {
				return st[i];
			}

			return query(a, b, i * 2 + 1, l, (l + r) / 2) + query(a, b, i * 2 + 2, (l + r) / 2, r);
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0