結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-15 14:18:47
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 4,061 bytes
コンパイル時間 9,324 ms
コンパイル使用メモリ 325,900 KB
実行使用メモリ 182,956 KB
最終ジャッジ日時 2024-06-13 02:48:12
合計ジャッジ時間 31,693 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1,841 ms
179,532 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


immutable MO = 10L^^18 + 9;
immutable NUM_COLORS = 5;

class SegmentTree {
  int n;
  long[] xs;
  long[][] sum;
  Tuple!(int, "color", long, "thick")[] add;
  this(int n_, long[] xs_) {
    for (n = 1; n < n_; n <<= 1) {}
    xs = new long[n << 1];
    xs[0 .. n_ + 1] = xs_[];
    xs[n_ + 1 .. $] = xs[n_];
    sum = new long[][](n << 1, NUM_COLORS);
    add.length = n << 1;
    add[] = tuple!("color", "thick")(-1, 0L);
  }
  private void fill(int u, int l, int r, int col, long thk) {
    foreach (c; 0 .. NUM_COLORS) {
      if (c == col) {
        sum[u][c] += (xs[r] - xs[l]) * thk;
      } else {
        sum[u][c] = 0;
      }
    }
    if (add[u].color != col) {
      add[u] = tuple!("c", "thick")(col, 0L);
    }
    add[u].thick += thk;
  }
  private long[] query(int u, int l, int r, int a, int b, int col, long thk) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) {
      return new long[NUM_COLORS];
    }
    if (a == l && b == r) {
      if (col != -1) {
        fill(u, l, r, col, thk);
      }
      return sum[u];
    }
    const uL = u << 1;
    const uR = u << 1 | 1;
    const mid = (l + r) >> 1;
    if (add[u].color != -1) {
      fill(uL, l, mid, add[u].color, add[u].thick);
      fill(uR, mid, r, add[u].color, add[u].thick);
      add[u] = tuple!("color", "thick")(-1, 0L);
    }
    const resL = query(uL, l, mid, a, b, col, thk);
    const resR = query(uR, mid, r, a, b, col, thk);
    auto ret = new long[NUM_COLORS];
    foreach (c; 0 .. NUM_COLORS) {
      sum[u][c] = sum[uL][c] + sum[uR][c];
      ret[c] = resL[c] + resR[c];
    }
    return ret;
  }
  long[] query(int a, int b, int col, long thk) {
    return query(1, 0, n, a, b + 1, col, thk);
  }
}

long N;
int Q;
int[] X;
long[] L, R;

void main() {
  try {
    for (; ; ) {
      N = readLong();
      Q = readInt();
      X = new int[Q];
      L = new long[Q];
      R = new long[Q];
      foreach (q; 0 .. Q) {
        X[q] = readInt() - 1;
        L[q] = readLong();
        R[q] = readLong();
      }
      
      long[] xs;
      xs ~= 0;
      xs ~= N;
      foreach (q; 0 .. Q) {
        xs ~= L[q];
        xs ~= R[q] + 1;
      }
      xs = xs.sort.uniq.array;
      const xsLen = cast(int)(xs.length) - 1;
      debug {
        writeln("xsLen = ", xsLen);
        writeln("xs = ", xs);
      }
      
      auto ans = new long[NUM_COLORS];
      auto seg = new SegmentTree(xsLen, xs);
      foreach (q; 0 .. Q) {
        const a = xs.lowerBound(L[q]);
        const b = xs.lowerBound(R[q] + 1) - 1;
        const res = seg.query(a, b, X[q], 1);
        if (X[q] == -1) {
          // TODO: bonus
          const mx = res.maxElement;
          if (res.count(mx) == 1) {
            (ans[res.maxIndex] += mx) %= MO;
          }
        }
      }
      {
        const res = seg.query(0, xsLen - 1, -1, 0);
        foreach (c; 0 .. NUM_COLORS) {
          (ans[c] += res[c]) %= MO;
        }
      }
      writeln(ans.to!string.replaceAll(regex(`[\[\],]`), ""));
    }
  } catch (EOFException e) {
  }
}
0