結果

問題 No.59 鉄道の旅
ユーザー fujisufujisu
提出日時 2015-05-11 02:56:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 261 ms / 5,000 ms
コード長 5,411 bytes
コンパイル時間 2,229 ms
コンパイル使用メモリ 75,656 KB
実行使用メモリ 66,832 KB
最終ジャッジ日時 2023-08-26 05:39:33
合計ジャッジ時間 5,049 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,900 KB
testcase_01 AC 45 ms
55,816 KB
testcase_02 AC 45 ms
55,900 KB
testcase_03 AC 48 ms
55,712 KB
testcase_04 AC 261 ms
66,408 KB
testcase_05 AC 48 ms
56,172 KB
testcase_06 AC 50 ms
55,660 KB
testcase_07 AC 48 ms
55,620 KB
testcase_08 AC 148 ms
60,940 KB
testcase_09 AC 132 ms
59,376 KB
testcase_10 AC 138 ms
59,972 KB
testcase_11 AC 120 ms
59,028 KB
testcase_12 AC 195 ms
65,548 KB
testcase_13 AC 247 ms
66,832 KB
testcase_14 AC 252 ms
66,584 KB
testcase_15 AC 45 ms
55,900 KB
権限があれば一括ダウンロードができます

ソースコード

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(1000000 + 2);

		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (0 < w[i]) {
				if (seg.sum(w[i], 1000000 + 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