結果

問題 No.789 範囲の合計
ユーザー te-shte-sh
提出日時 2019-12-25 13:32:00
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 177 ms / 1,000 ms
コード長 3,781 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 166,004 KB
実行使用メモリ 24,988 KB
最終ジャッジ日時 2023-09-04 04:30:03
合計ジャッジ時間 4,427 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 165 ms
22,624 KB
testcase_03 AC 81 ms
5,736 KB
testcase_04 AC 154 ms
24,044 KB
testcase_05 AC 147 ms
24,196 KB
testcase_06 AC 155 ms
23,188 KB
testcase_07 AC 73 ms
6,112 KB
testcase_08 AC 111 ms
10,496 KB
testcase_09 AC 99 ms
10,600 KB
testcase_10 AC 177 ms
24,988 KB
testcase_11 AC 155 ms
23,924 KB
testcase_12 AC 156 ms
22,888 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/789

import std.algorithm, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int n; io.getV(n);
  int[] t, a, b; io.getC(n, t, a, b);

  import std.stdio;
  
  int[] c;
  foreach (ti, ai, bi; lockstep(t, a, b)) {
    c ~= ai;
    if (ti == 1) c ~= bi;
  }
  c = c.sort().uniq.array;

  int[int] z;
  foreach (i, ci; c) z[ci] = cast(int)i;
  foreach (ti, ref ai, ref bi; lockstep(t, a, b)) {
    ai = z[ai];
    if (ti == 1) bi = z[bi];
  }

  auto s = new SegmentTree!(int, "a+b")(c.length);

  auto ans = 0L;
  foreach (ti, ai, bi; lockstep(t, a, b)) {
    if (ti == 0)
      s[ai] += bi;
    else
      ans += s[ai..bi+1];
  }

  io.put(ans);
}

class SegmentTree(T, alias pred = "a+b")
{
  import std.functional, std.math;
  alias predFun = binaryFun!pred;

  size_t n, an;
  T[] buf;
  T unit;

  @property auto data() { return buf[an..an+n]; }

  this(size_t n, T unit = T.init)
  {
    prepare(n, unit);
    if (T.init != unit) {
      buf[an..an+n][] = unit;
      propagateAll();
    }
  }

  this(T[] init, T unit = T.init)
  {
    prepare(init.length, unit);
    buf[an..an+n][] = init[];
    propagateAll();
  }

  auto prepare(size_t n, T unit)
  {
    this.n = n;
    this.unit = unit;
    an = (n-1).nextPow2;
    buf = new T[](an*2);
  }

  auto opIndexAssign(T val, size_t i)
  {
    buf[i += an] = val;
    propagate(i);
  }

  auto opIndexOpAssign(string op)(T val, size_t i)
  {
    mixin("buf[i += an]"~op~"=val;");
    propagate(i);
  }

  auto opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
  {
    mixin(op~"buf[i += an];");
    propagate(i);
  }

  pure auto opSlice(size_t l, size_t r)
  {
    l += an; r += an;
    T r1 = unit, r2 = unit;
    while (l != r) {
      if (l % 2) r1 = predFun(r1, buf[l++]);
      if (r % 2) r2 = predFun(buf[--r], r2);
      l /= 2; r /= 2;
    }
    return predFun(r1, r2);
  }

  pure auto opIndex(size_t i) { return buf[i+an]; }
  pure auto opDollar() { return n; }

private:

  auto propagateAll() { foreach_reverse (i; 1..an) buf[i] = predFun(buf[i*2], buf[i*2+1]); }
  auto propagate(size_t i) { while (i /= 2) buf[i] = predFun(buf[i*2], buf[i*2+1]); }
}

auto io = IO();

struct IO
{
  import std.algorithm, std.conv, std.format, std.meta, std.range, std.stdio, std.traits;

  dchar[] buf;
  auto sp = (new dchar[](0)).splitter;
  int precision = 10;
  string delimiter = " ";

  void nextLine()
  {
    stdin.readln(buf);
    sp = buf.splitter;
  }

  auto get(T)(ref T v)
  {
    if (sp.empty) nextLine();
    v = sp.front.to!T;
    sp.popFront();
  }

  auto getV(T...)(ref T v)
  {
    foreach (ref w; v) get(w);
  }

  auto getA(T)(size_t n, ref T v)
  if (hasAssignableElements!T)
  {
    v = new T(n);
    foreach (ref w; v) get(w);
  }

  auto getC(T...)(size_t n, ref T v)
  if (allSatisfy!(hasAssignableElements, T))
  {
    foreach (ref w; v)
      w = new typeof(w)(n);
    foreach (i; 0..n)
      foreach (ref w; v) get(w[i]);
  }

  auto getM(T)(size_t r, size_t c, ref T v)
  if (hasAssignableElements!T && hasAssignableElements!(ElementType!T))
  {
    v = new T(r);
    foreach (ref w; v) getA(c, w);
  }

  auto putA(T)(T v)
  {
    static if (isInputRange!T && hasLength!T && !isSomeString!T) {
      foreach (i, w; v) {
	putA(w);
	if (i < v.length - 1) write(delimiter);
      }
    } else if (isFloatingPoint!T) {
      writef(format("%%.%df", precision), v);
    } else {
      write(v);
    }
  }

  auto put(T...)(T v)
  {
    foreach (i, w; v) {
      putA(w);
      if (i < v.length - 1) write(delimiter);
    }
    writeln;
  }

  auto putB(S, T)(bool c, S t, T f)
  {
    if (c)
      put(t);
    else
      put(f);
  }

  auto dbg(T...)(T v)
  {
    stderr.writeln(v);
  }
}
0