結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-09-09 14:18:01
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 620 ms / 5,000 ms
コード長 5,201 bytes
コンパイル時間 3,085 ms
コンパイル使用メモリ 121,088 KB
実行使用メモリ 29,472 KB
最終ジャッジ日時 2024-09-22 19:37:16
合計ジャッジ時間 16,388 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 604 ms
27,256 KB
testcase_12 AC 569 ms
27,096 KB
testcase_13 AC 406 ms
25,952 KB
testcase_14 AC 558 ms
27,752 KB
testcase_15 AC 589 ms
27,892 KB
testcase_16 AC 620 ms
29,472 KB
testcase_17 AC 510 ms
28,348 KB
testcase_18 AC 503 ms
28,344 KB
testcase_19 AC 319 ms
27,680 KB
testcase_20 AC 333 ms
28,016 KB
testcase_21 AC 342 ms
27,640 KB
testcase_22 AC 327 ms
27,860 KB
testcase_23 AC 350 ms
27,936 KB
testcase_24 AC 313 ms
27,184 KB
testcase_25 AC 322 ms
27,624 KB
testcase_26 AC 331 ms
27,272 KB
testcase_27 AC 316 ms
27,468 KB
testcase_28 AC 340 ms
27,160 KB
testcase_29 AC 557 ms
27,864 KB
testcase_30 AC 589 ms
27,824 KB
testcase_31 AC 620 ms
29,392 KB
testcase_32 AC 178 ms
27,616 KB
testcase_33 AC 303 ms
27,424 KB
testcase_34 AC 332 ms
27,800 KB
testcase_35 AC 309 ms
27,416 KB
testcase_36 AC 304 ms
27,540 KB
testcase_37 AC 296 ms
27,444 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, 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; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum INF = 10L^^9 + 10;

struct T {
  bool same;
  long mx, sum, lcm;
  T opBinary(string op)(ref const(T) t) const {
    return T(same && t.same && mx == t.mx,
             max(mx, t.mx),
             sum + t.sum,
             min(lcm / gcd(lcm, t.lcm) * t.lcm, INF));
  }
};
enum idT = T(true, 0, 0, 1);

T sameOf(long val, int sz) {
  return T(true, val, val * sz, val);
}

class SegmentTree {
  int n;
  T[] ts;
  long[] ss;
  this(int n_, T[] ini) {
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ini[];
    ts[n + n_ .. n << 1] = idT;
    foreach_reverse (a; 1 .. n) ts[a] = ts[a << 1] * ts[a << 1 | 1];
    ss = new long[n << 1];
    ss[] = -1;
  }

  // x -> val
  private void query1(int u, int l, int r, int a, int b, long val) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) return;
    if (a == l && b == r) {
      ts[u] = sameOf(val, r - l);
      ss[u] = val;
      return;
    }
    const uL = u << 1, uR = u << 1 | 1, mid = (l + r) >> 1;
    if (ss[u] != -1) {
      ts[uL] = sameOf(ss[u], mid - l);
      ts[uR] = sameOf(ss[u], r - mid);
      ss[uL] = ss[u];
      ss[uR] = ss[u];
      ss[u] = -1;
    }
    query1(uL, l, mid, a, b, val);
    query1(uR, mid, r, a, b, val);
    ts[u] = ts[uL] * ts[uR];
  }
  void query1(int a, int b, long val) {
    query1(1, 0, n, a, b, val);
  }

  // x -> gcd(x, val)
  private void query2(int u, int l, int r, int a, int b, long val) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) return;
    if (val % ts[u].lcm == 0) return;
    if (a == l && b == r && ts[u].same) {
      const g = gcd(ts[u].mx, val);
      ts[u] = sameOf(g, r - l);
      ss[u] = g;
      return;
    }
    const uL = u << 1, uR = u << 1 | 1, mid = (l + r) >> 1;
    if (ss[u] != -1) {
      ts[uL] = sameOf(ss[u], mid - l);
      ts[uR] = sameOf(ss[u], r - mid);
      ss[uL] = ss[u];
      ss[uR] = ss[u];
      ss[u] = -1;
    }
    query2(uL, l, mid, a, b, val);
    query2(uR, mid, r, a, b, val);
    ts[u] = ts[uL] * ts[uR];
  }
  void query2(int a, int b, long val) {
    query2(1, 0, n, a, b, val);
  }

  private T query(int u, int l, int r, int a, int b) {
    chmax(a, l);
    chmin(b, r);
    if (a >= b) return idT;
    if (a == l && b == r) return ts[u];
    const uL = u << 1, uR = u << 1 | 1, mid = (l + r) >> 1;
    if (ss[u] != -1) {
      ts[uL] = sameOf(ss[u], mid - l);
      ts[uR] = sameOf(ss[u], r - mid);
      ss[uL] = ss[u];
      ss[uR] = ss[u];
      ss[u] = -1;
    }
    const resL = query(uL, l, mid, a, b);
    const resR = query(uR, mid, r, a, b);
    ts[u] = ts[uL] * ts[uR];
    return resL * resR;
  }
  T query(int a, int b) {
    return query(1, 0, n, a, b);
  }
}


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

void main() {
  try {
    for (; ; ) {
      N = readInt();
      Q = readInt();
      A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      TYP = new int[Q];
      L = new int[Q];
      R = new int[Q];
      X = new long[Q];
      foreach (q; 0 .. Q) {
        TYP[q] = readInt();
        L[q] = readInt() - 1;
        R[q] = readInt();
        if (TYP[q] == 1 || TYP[q] == 2) {
          X[q] = readLong();
        }
      }
      
      auto ini = new T[N];
      foreach (i; 0 .. N) {
        ini[i] = sameOf(A[i], 1);
      }
      auto seg = new SegmentTree(N, ini);
      foreach (q; 0 .. Q) {
        switch (TYP[q]) {
          case 1: {
            seg.query1(L[q], R[q], X[q]);
          } break;
          case 2: {
            seg.query2(L[q], R[q], X[q]);
          } break;
          case 3: {
            const res = seg.query(L[q], R[q]);
            writeln(res.mx);
          } break;
          case 4: {
            const res = seg.query(L[q], R[q]);
            writeln(res.sum);
          } break;
          default: assert(false);
        }
        debug {
          for (int a = 1; a <= seg.n; a <<= 1) {
            writeln(seg.ts[a .. a << 1]);
          }
          writeln("ss = ", seg.ss);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0