結果

問題 No.59 鉄道の旅
ユーザー fujisufujisu
提出日時 2015-05-11 02:53:05
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 5,409 bytes
コンパイル時間 4,142 ms
コンパイル使用メモリ 78,672 KB
実行使用メモリ 46,404 KB
最終ジャッジ日時 2024-06-07 00:56:29
合計ジャッジ時間 4,725 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,944 KB
testcase_01 AC 50 ms
37,596 KB
testcase_02 AC 50 ms
37,636 KB
testcase_03 AC 52 ms
38,072 KB
testcase_04 RE -
testcase_05 WA -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 115 ms
40,304 KB
testcase_12 AC 194 ms
44,780 KB
testcase_13 RE -
testcase_14 AC 192 ms
46,124 KB
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.InputMismatchException;

public class Main {
	class SegmentTree {
		int n;
		int[] tree;

		// ////////////////////////////////////////////////
		// 要素数k(0~k-1)
		// ////////////////////////////////////////////////

		SegmentTree(int k) {
			this.n = Integer.highestOneBit(k) << 1;
			while (k <= (this.n >> 1))
				this.n = this.n >> 1;
			tree = new int[2 * this.n];
		}

		// ////////////////////////////////////////////////
		// 区間和
		// addは更新でなく加算なので注意、半開区間[left, right)
		// ////////////////////////////////////////////////

		void add(int key, int value) {
			add(1, 0, this.n, key, value);
		}

		void add(int k, int low, int high, int key, int value) {
			if (key < low || high <= key) {
				return;
			}
			if (low + 1 == high) {
				this.tree[k] += value;
				return;
			}
			int mid = (low + high) / 2;
			add(2 * k, low, mid, key, value);
			add(2 * k + 1, mid, high, key, value);
			this.tree[k] = this.tree[2 * k] + this.tree[2 * k + 1];
		}

		int sum(int left, int right) {
			return sum(1, 0, this.n, left, right);
		}

		int sum(int k, int low, int high, int left, int right) {
			if (right <= low || high <= left) {
				return 0;
			}
			if (high <= low) {
				return 0;
			}
			if (left == low && high == right) {
				return this.tree[k];
			}

			int mid = (low + high) / 2;
			int sum = 0;
			sum += sum(2 * k, low, Math.min(high, mid), left, Math.min(right, mid));
			sum += sum(2 * k + 1, Math.max(low, mid), high, Math.max(left, mid), right);
			return sum;
		}

		// ////////////////////////////////////////////////
		// 区間最大値
		// updateは加算でなく更新なので注意、半開区間[left, right)
		// ////////////////////////////////////////////////

		void update(int key, int value) {
			update(1, 0, this.n, key, value);
		}

		void update(int k, int low, int high, int key, int value) {
			if (key < low || high <= key) {
				return;
			}
			if (low + 1 == high) {
				this.tree[k] = value;
				return;
			}
			int mid = (low + high) / 2;
			update(2 * k, low, mid, key, value);
			update(2 * k + 1, mid, high, key, value);
			this.tree[k] = Math.max(this.tree[2 * k], this.tree[2 * k + 1]);
		}

		int max(int left, int right) {
			return max(1, 0, this.n, left, right);
		}

		int max(int k, int low, int high, int left, int right) {
			if (right <= low || high <= left) {
				return 0;
			}
			if (high <= low) {
				return 0;
			}
			if (left == low && high == right) {
				return this.tree[k];
			}

			int mid = (low + high) / 2;
			int max = 0;
			max = Math.max(max, max(2 * k, low, Math.min(high, mid), left, Math.min(right, mid)));
			max = Math.max(max, max(2 * k + 1, Math.max(low, mid), high, Math.max(left, mid), right));
			return max;
		}

		// ////////////////////////////////////////////////
		// 共通
		// ////////////////////////////////////////////////

		int get(int key) {
			return this.tree[this.tree.length - n + key];
		}
	}

	void run() {
		MyScanner sc = new MyScanner();

		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] w = sc.nextIntArray(n);
		SegmentTree seg = new SegmentTree(100000 + 2);

		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (0 < w[i]) {
				if (seg.sum(w[i], 100000 + 1) < k) {
					seg.add(w[i], 1);
					cnt++;
				}
			} else {
				if (0 < seg.get(-w[i])) {
					seg.add(-w[i], -1);
					cnt--;
				}
			}
		}
		System.out.println(cnt);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

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

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

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

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

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

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0