結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-15 15:23:51
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 5,411 bytes
コンパイル時間 8,812 ms
コンパイル使用メモリ 326,140 KB
実行使用メモリ 190,284 KB
最終ジャッジ日時 2024-06-13 02:49:24
合計ジャッジ時間 37,026 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2,255 ms
186,800 KB
testcase_04 AC 1 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 {
  alias Info = Tuple!(int, "color", long, "thick", bool, "cleared");
  int n;
  long[] xs;
  long[][] sum;
  Info[] 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 = new Info[n << 1];
    add[] = Info(-1, 0L, false);
  }
  private void fill(int u, int l, int r, in Info info) {
// writefln("fill %s [%s, %s) %s",u,l,r,info);
    if (info.cleared) {
      sum[u][] = 0;
    }
    foreach (c; 0 .. NUM_COLORS) {
      if (c == info.color) {
        sum[u][c] += (xs[r] - xs[l]) * info.thick;
      } else {
        sum[u][c] = 0;
      }
    }
    if (add[u].color != info.color) {
      add[u] = Info(info.color, 0L, (add[u].color != -1));
    }
    add[u].thick += info.thick;
    add[u].cleared = add[u].cleared || info.cleared;
  }
  private long[] query(int u, int l, int r, int a, int b, in Info info) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) {
      return new long[NUM_COLORS];
    }
    if (a == l && b == r) {
      if (info.color != -1) {
        fill(u, l, r, info);
      }
      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]);
      fill(uR, mid, r, add[u]);
      add[u] = Info(-1, 0L, false);
    }
    const resL = query(uL, l, mid, a, b, info);
    const resR = query(uR, mid, r, a, b, info);
    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) {
    return query(1, 0, n, a, b + 1, Info(col, 1, false));
  }
}

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();
      }
      
      // brute force
      debug {
        auto brt = new long[NUM_COLORS];
        auto col = new int[cast(int)(N)];
        auto thk = new long[cast(int)(N)];
        col[] = -1;
        foreach (q; 0 .. Q) {
          if (X[q] == -1) {
            auto score = new long[NUM_COLORS];
            foreach (i; L[q] .. R[q] + 1) {
              if (col[cast(int)(i)] != -1) {
                score[col[cast(int)(i)]] += thk[cast(int)(i)];
              }
            }
            const mx = score.maxElement;
            if (score.count(mx) == 1) {
              brt[score.maxIndex] += mx;
            }
          } else {
            foreach (i; L[q] .. R[q] + 1) {
              if (col[cast(int)(i)] != X[q]) {
                col[cast(int)(i)] = X[q];
                thk[cast(int)(i)] = 0;
              }
              ++thk[cast(int)(i)];
            }
          }
        }
        foreach (i; 0 .. N) {
          if (col[cast(int)(i)] != -1) {
            brt[col[cast(int)(i)]] += thk[cast(int)(i)];
          }
        }
        writeln("brute force: ", brt);
      }
      
      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]);
        debug {
          // writeln("  ", seg.sum);
          writeln("  ", res);
        }
        if (X[q] == -1) {
          const mx = res.maxElement;
          if (res.count(mx) == 1) {
            (ans[res.maxIndex] += mx) %= MO;
          }
        }
      }
      {
        const res = seg.query(0, xsLen - 1, -1);
        debug {
          writeln(res);
        }
        foreach (c; 0 .. NUM_COLORS) {
          (ans[c] += res[c]) %= MO;
        }
      }
      writeln(ans.to!string.replaceAll(regex(`[\[\],]`), ""));
    }
  } catch (EOFException e) {
  }
}
0