結果

問題 No.230 Splarraay スプラレェーイ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-15 13:48:28
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 945 ms / 5,000 ms
コード長 3,071 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 104,812 KB
実行使用メモリ 22,456 KB
最終ジャッジ日時 2023-09-03 22:08:31
合計ジャッジ時間 6,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 37 ms
4,376 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 412 ms
14,120 KB
testcase_10 AC 262 ms
5,916 KB
testcase_11 AC 220 ms
14,148 KB
testcase_12 AC 407 ms
14,180 KB
testcase_13 AC 56 ms
4,380 KB
testcase_14 AC 309 ms
21,864 KB
testcase_15 AC 609 ms
21,880 KB
testcase_16 AC 755 ms
21,640 KB
testcase_17 AC 945 ms
21,668 KB
testcase_18 AC 515 ms
22,456 KB
testcase_19 AC 545 ms
21,308 KB
権限があれば一括ダウンロードができます

ソースコード

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)); }



class SegmentTree {
  enum NUM_COLORS = 2;
  int n;
  int[][] cnt;
  int[] add;
  this(int n_) {
    for (n = 1; n < n_; n <<= 1) {}
    cnt = new int[][](n << 1, NUM_COLORS);
    add = new int[n << 1];
    add[] = -1;
  }
  private int[] query(int u, int l, int r, int a, int b, int val) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) {
      return new int[NUM_COLORS];
    }
    if (a == l && b == r) {
      if (val != -1) {
        cnt[u][] = 0;
        cnt[u][val] = r - l;
        add[u] = val;
      }
      return cnt[u];
    }
    const uL = u << 1;
    const uR = u << 1 | 1;
    const mid = (l + r) >> 1;
    if (add[u] != -1) {
      cnt[uL][] = 0;
      cnt[uL][add[u]] = mid - l;
      cnt[uR][] = 0;
      cnt[uR][add[u]] = r - mid;
      add[uL] = add[u];
      add[uR] = add[u];
      add[u] = -1;
    }
    const resL = query(uL, l, mid, a, b, val);
    const resR = query(uR, mid, r, a, b, val);
    auto ret = new int[NUM_COLORS];
    foreach (color; 0 .. NUM_COLORS) {
      cnt[u][color] = cnt[uL][color] + cnt[uR][color];
      ret[color] = resL[color] + resR[color];
    }
    return ret;
  }
  int[] query(int a, int b, int val) {
    return query(1, 0, n, a, b + 1, val);
  }
}

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

void main() {
  try {
    for (; ; ) {
      N = readInt();
      Q = readInt();
      X = new int[Q];
      L = new int[Q];
      R = new int[Q];
      foreach (q; 0 .. Q) {
        X[q] = readInt() - 1;
        L[q] = readInt();
        R[q] = readInt();
      }
      
      auto seg = new SegmentTree(N);
      long[2] ans;
      foreach (q; 0 .. Q) {
        const res = seg.query(L[q], R[q], X[q]);
        if (X[q] == -1) {
          if (res[0] > res[1]) {
            ans[0] += res[0];
          } else if (res[0] < res[1]) {
            ans[1] += res[1];
          }
        }
      }
      {
        const res = seg.query(0, N - 1, -1);
        ans[0] += res[0];
        ans[1] += res[1];
      }
      writeln(ans[0], " ", ans[1]);
    }
  } catch (EOFException e) {
  }
}
0