結果

問題 No.230 Splarraay スプラレェーイ
ユーザー te-shte-sh
提出日時 2017-05-23 17:00:27
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,050 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 86,512 KB
実行使用メモリ 9,136 KB
最終ジャッジ日時 2023-09-03 13:30:50
合計ジャッジ時間 9,363 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,504 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
5,144 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 13 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,384 KB
testcase_09 AC 163 ms
4,380 KB
testcase_10 AC 108 ms
4,384 KB
testcase_11 AC 60 ms
4,384 KB
testcase_12 AC 159 ms
5,736 KB
testcase_13 AC 17 ms
4,380 KB
testcase_14 AC 90 ms
4,684 KB
testcase_15 AC 156 ms
4,664 KB
testcase_16 AC 199 ms
4,652 KB
testcase_17 AC 339 ms
5,668 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto q = readln.chomp.to!size_t;

  auto sa = SegmentTree(n), sb = SegmentTree(n);
  auto pa = 0L, pb = 0L;

  foreach (_; 0..q) {
    auto rd = readln.split, x = rd[0], l = rd[1].to!size_t, r = rd[2].to!size_t + 1;
    switch (x) {
    case "0":
      auto ca = sa[l..r], cb = sb[l..r];
      if (ca > cb) pa += ca;
      if (ca < cb) pb += cb;
      break;
    case "1":
      sa[l..r] = true;
      sb[l..r] = false;
      break;
    case "2":
      sa[l..r] = false;
      sb[l..r] = true;
      break;
    default:
      assert(0);
    }
  }

  pa += sa[0..$];
  pb += sb[0..$];

  writeln(pa, " ", pb);
}

struct SegmentTree
{
  import core.bitop, std.conv;

  const size_t n, an;
  bool[] buf, prop;

  this(size_t n)
  {
    this.n = n;
    an = (1 << ((n - 1).bsr + 1));
    buf = new bool[](an * 2);
    prop = new bool[](an * 2);
    prop[an..$] = true;
  }

  void propagate(size_t k)
  {
    if (k >= an || !prop[k]) return;
    buf[k*2] = buf[k*2+1] = buf[k];
    prop[k*2] = prop[k*2+1] = true;
    prop[k] = false;
  }

  void opSliceAssign(bool val, size_t l, size_t r)
  {
    fill(val, l, r, 1, 0, an);
  }

  void fill(bool val, size_t l, size_t r, size_t k, size_t nl, size_t nr)
  {
    if (nr <= l || r <= nl) return;

    if (l <= nl && nr <= r) {
      buf[k] = val;
      prop[k] = true;
      return;
    }

    propagate(k);

    auto nm = (nl + nr) / 2;
    fill(val, l, r, k*2,   nl, nm);
    fill(val, l, r, k*2+1, nm, nr);
  }

  int opSlice(size_t l, size_t r)
  {
    return count(l, r, 1, 0, an);
  }

  int count(size_t l, size_t r, size_t k, size_t nl, size_t nr)
  {
    if (nr <= l || r <= nl) return 0;

    if (l <= nl && nr <= r && prop[k]) return (nr - nl).to!int * (buf[k] ? 1 : 0);

    propagate(k);

    auto nm = (nl + nr) / 2;
    auto vl = count(l, r, k*2,   nl, nm);
    auto vr = count(l, r, k*2+1, nm, nr);
    return vl + vr;
  }

  pure size_t opDollar() const { return n; }
}
0