結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-05-18 05:41:04 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 294 ms / 5,000 ms | 
| コード長 | 2,334 bytes | 
| コンパイル時間 | 3,807 ms | 
| コンパイル使用メモリ | 78,940 KB | 
| 実行使用メモリ | 54,440 KB | 
| 最終ジャッジ日時 | 2024-12-24 22:20:37 | 
| 合計ジャッジ時間 | 6,597 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 12 | 
ソースコード
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);
		}
	}
}
            
            
            
        