結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  fujisu | 
| 提出日時 | 2015-05-11 02:53:05 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                RE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 5,409 bytes | 
| コンパイル時間 | 3,133 ms | 
| コンパイル使用メモリ | 78,900 KB | 
| 実行使用メモリ | 46,416 KB | 
| 最終ジャッジ日時 | 2024-12-24 20:37:33 | 
| 合計ジャッジ時間 | 6,381 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 3 WA * 1 RE * 8 | 
ソースコード
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();
		}
	}
}
            
            
            
        