結果

問題 No.59 鉄道の旅
ユーザー nebukuro09nebukuro09
提出日時 2016-11-21 16:58:03
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 96 ms / 5,000 ms
コード長 1,744 bytes
コンパイル時間 1,606 ms
コンパイル使用メモリ 97,756 KB
実行使用メモリ 12,852 KB
最終ジャッジ日時 2023-09-02 22:59:09
合計ジャッジ時間 2,328 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,288 KB
testcase_01 AC 5 ms
12,240 KB
testcase_02 AC 4 ms
12,052 KB
testcase_03 AC 4 ms
11,172 KB
testcase_04 AC 96 ms
12,032 KB
testcase_05 AC 4 ms
11,996 KB
testcase_06 AC 4 ms
9,880 KB
testcase_07 AC 4 ms
9,796 KB
testcase_08 AC 17 ms
12,340 KB
testcase_09 AC 13 ms
12,088 KB
testcase_10 AC 14 ms
11,784 KB
testcase_11 AC 12 ms
12,380 KB
testcase_12 AC 75 ms
12,852 KB
testcase_13 AC 75 ms
12,036 KB
testcase_14 AC 90 ms
12,596 KB
testcase_15 AC 4 ms
11,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;


class SegmentTree {
  int[] table;
  int N, table_size;

  this(int n) {
    N = n;
    table_size = 1;
    while (table_size < n) table_size *= 2;
    table_size *= 2;
    table = new int[](table_size);
  }

  void add(int pos, int num) {
    add_rec(0, 0, table_size/2-1, pos, num);
  }

  void add_rec(int i, int left, int right, int pos, int num) {
    table[i] += num;
    if (left == right)
      return;
    auto mid = (left + right) / 2;
    if (pos <= mid)
      add_rec(i*2+1, left, mid, pos, num);
    else
      add_rec(i*2+2, mid+1, right, pos, num);
  }

  int sum(int pl, int pr) {
    return sum_rec(0, pl, pr, 0, table_size/2-1);
  }

  int sum_rec(int i, int pl, int pr, int left, int right) {
    if (pl > right || pr < left)
      return 0;
    else if (pl <= left && right <= pr)
      return table[i];
    else 
      return sum_rec(i*2+1, pl, pr, left, (left+right)/2) +
             sum_rec(i*2+2, pl, pr, (left+right)/2+1, right);
  }
}

void main() {
  auto W_MAX = 1000000;
  auto sg = new SegmentTree(W_MAX);
  auto input = readln.split.map!(to!int);
  auto N = input[0];
  auto K = input[1];
  foreach(_; 0..N) {
    auto W = readln.chomp.to!int;
    if (W > 0 && sg.sum(W, W_MAX) < K)
      sg.add(W, 1);
    else if (W < 0 && sg.sum(-W, -W) > 0)
      sg.add(-W, -1);
  }
  writeln(sg.sum(0, W_MAX));
}


void test_sg() {
  auto sg = new SegmentTree(100);
  sg.add(50, 20);
  sg.add(60, 3);
  writeln(sg.sum(0, 100));
  writeln(sg.sum(0, 40));
  writeln(sg.sum(50, 60));
  writeln(sg.sum(51, 60));
}
0