結果

問題 No.59 鉄道の旅
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-07 22:29:01
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,219 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 77,640 KB
実行使用メモリ 70,464 KB
最終ジャッジ日時 2024-04-15 15:44:46
合計ジャッジ時間 6,772 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
60,768 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 154 ms
60,452 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 257 ms
65,532 KB
testcase_12 AC 606 ms
68,352 KB
testcase_13 RE -
testcase_14 AC 635 ms
70,464 KB
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int N;
  static int[] dat;

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    init(1000001);
    int n = sc.nextInt();
    int k = sc.nextInt();
    int ans = 0;
    for(int i = 0; i < n; i++) {
      int w = sc.nextInt();
      if(w > 0) {
        int t = query(w, 1000001, 0, 0, N);
        if(t < k) {
          update(w, 1);
          ans++;
        } 
      } else {
        if(dat[w] > 0) {
          update(w, -1);
          ans--;
        }
      }
    }

    System.out.println(ans);
  }

  public static void init(int n_) {
    int n = 1;
    while(n < n_) {
      n *= 2;
    }
    N = n;
    dat = new int[2 * N - 1];
  }

  public static void update(int k, int x) {
    int a = k + N - 1;
    dat[a] += x;
    while(a > 0) {
      a = (a - 1) / 2;
      dat[a] = dat[2 * a + 1] + dat[2 * a + 2]; 
    }
  }

  public static int query(int a, int b, int k, int l, int r) {
    if((r <= a) || (l >= b)) return 0;
    if((a <= l) && (r <= b)) {
      return dat[k];
    } else {
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
}
0