結果
問題 | No.255 Splarrraaay スプラーレェーーイ |
ユーザー |
👑 |
提出日時 | 2019-01-15 20:53:48 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 2,565 ms / 10,000 ms |
コード長 | 5,427 bytes |
コンパイル時間 | 7,897 ms |
コンパイル使用メモリ | 325,548 KB |
実行使用メモリ | 188,624 KB |
最終ジャッジ日時 | 2024-06-13 02:50:54 |
合計ジャッジ時間 | 33,656 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
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 (info.cleared || 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 forcedebug {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) {}}