結果

問題 No.1197 モンスターショー
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-08-22 16:03:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,128 ms / 3,000 ms
コード長 6,582 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 122,748 KB
実行使用メモリ 27,940 KB
最終ジャッジ日時 2024-06-22 08:18:48
合計ジャッジ時間 21,816 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 158 ms
24,680 KB
testcase_08 AC 346 ms
8,496 KB
testcase_09 AC 416 ms
12,628 KB
testcase_10 AC 683 ms
13,252 KB
testcase_11 AC 481 ms
7,156 KB
testcase_12 AC 388 ms
6,940 KB
testcase_13 AC 180 ms
9,324 KB
testcase_14 AC 699 ms
10,520 KB
testcase_15 AC 509 ms
7,168 KB
testcase_16 AC 693 ms
16,320 KB
testcase_17 AC 795 ms
15,868 KB
testcase_18 AC 302 ms
6,940 KB
testcase_19 AC 644 ms
13,708 KB
testcase_20 AC 131 ms
6,940 KB
testcase_21 AC 495 ms
13,084 KB
testcase_22 AC 48 ms
6,944 KB
testcase_23 AC 740 ms
10,668 KB
testcase_24 AC 534 ms
10,420 KB
testcase_25 AC 323 ms
8,932 KB
testcase_26 AC 663 ms
10,808 KB
testcase_27 AC 706 ms
13,700 KB
testcase_28 AC 558 ms
9,280 KB
testcase_29 AC 395 ms
9,844 KB
testcase_30 AC 292 ms
10,840 KB
testcase_31 AC 285 ms
8,464 KB
testcase_32 AC 311 ms
6,944 KB
testcase_33 AC 169 ms
7,520 KB
testcase_34 AC 1,128 ms
17,684 KB
testcase_35 AC 1,093 ms
18,396 KB
testcase_36 AC 1,112 ms
18,752 KB
testcase_37 AC 1,097 ms
16,568 KB
testcase_38 AC 1,092 ms
17,716 KB
testcase_39 AC 1,097 ms
19,640 KB
testcase_40 AC 475 ms
27,940 KB
testcase_41 AC 1 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


// T, S: monoid
// opTT: T * T -> T
// opST: S * T * int -> T
//   (s t_a) ... (s t_{b-1}) = opST(s, t_a ... t_{b-1}, b - a)
// opSS: S * S -> S
// query(a, b, s): t_a <- s t_a, ..., t_{b-1} <- s t_{b-1};
//   returns t_a ... t_{b-1}
class SegmentTree(T, S, alias opTT, alias opST, alias opSS) {
  import std.functional : binaryFun;
  alias opTTFun = binaryFun!opTT;
  alias opSTFun = opST;
  alias opSSFun = binaryFun!opSS;
  const(T) idT;
  const(S) idS;

  int n;
  T[] ts;
  S[] ss;
  this(int n_, const(T) idT, const(S) idS) {
    this.idT = idT;
    this.idS = idS;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ss = new S[n << 1];
    ts[] = idT;
    ss[] = idS;
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opTTFun(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const(S) s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const(S) s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opSTFun(s, ts[u], r - l);
      ss[u] = opSSFun(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    // speed-up: if (ss[u] != idS)
    {
      ts[uL] = opSTFun(ss[u], ts[uL], mid - l);
      ts[uR] = opSTFun(ss[u], ts[uR], r - mid);
      ss[uL] = opSSFun(ss[u], ss[uL]);
      ss[uR] = opSSFun(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTTFun(ts[uL], ts[uR]);
    return opTTFun(resL, resR);
  }
}


int N;
int[] A, B;

int[][] G;
int[] par, sz, dep;
int O;
int[] ord, head;

void dfs0(int u, int p) {
  par[u] = p;
  sz[u] = 1;
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs0(v, u);
      sz[u] += sz[v];
    }
  }
}

void dfs1(int u, int p, int h) {
  ord[u] = O++;
  head[u] = h;
  int vm = -1;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      if (vm == -1 || sz[vm] < sz[v]) {
        vm = v;
      }
    }
  }
  if (vm != -1) {
    dfs1(vm, u, h);
    foreach (i; G[u]) {
      const v = A[i] ^ B[i] ^ u;
      if (v != p && v != vm) {
        dfs1(v, u, v);
      }
    }
  }
}


void main() {
  try {
    for (; ; ) {
      N = readInt();
      const K = readInt();
      const Q = readInt();
      auto C = new int[K];
      foreach (k; 0 .. K) {
        C[k] = readInt() - 1;
      }
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }
      auto typ = new int[Q];
      auto P = new int[Q];
      auto D = new int[Q];
      auto E = new int[Q];
      foreach (q; 0 .. Q) {
        typ[q] = readInt();
        switch (typ[q]) {
          case 1: {
            P[q] = readInt() - 1;
            D[q] = readInt() - 1;
          } break;
          case 2: {
            E[q] = readInt() - 1;
          } break;
          default: assert(false);
        }
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      
      par = new int[N];
      sz = new int[N];
      dep = new int[N];
      ord = new int[N];
      head = new int[N];
      const int rt = 0;
      dfs0(rt, -1);
      O = 0;
      dfs1(rt, -1, rt);
      debug {
        writeln("ord = ", ord);
        writeln("head = ", head);
      }
      
      auto seg = new SegmentTree!(long, long, "a + b", (s, t, sz) => t + s * sz, "a + b")(N, 0, 0);
      
      // add val to the path from u to root
      long query(int u, long val) {
        long ret = 0;
        for (; u != -1; ) {
          const h = head[u];
          ret += seg.query(ord[h], ord[u] + 1, val);
          u = par[h];
        }
        return ret;
      }
      
      long sum0, sum1;
      void add(int u, long val) {
        sum0 += val;
        sum1 += val * dep[u];
        query(u, val);
      }
      foreach (k; 0 .. K) {
        add(C[k], +1);
      }
      
      debug {
        auto dist = new long[][](N, N);
        foreach (u; 0 .. N) {
          dist[u][] = N;
          dist[u][u] = 0;
        }
        foreach (i; 0 .. N - 1) {
          chmin(dist[A[i]][B[i]], 1);
          chmin(dist[B[i]][A[i]], 1);
        }
        foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
          chmin(dist[u][v], dist[u][w] + dist[w][v]);
        }
      }
      
      auto cs = C.dup;
      foreach (q; 0 .. Q) {
        switch (typ[q]) {
          case 1: {
            const k = P[q];
            add(cs[k], -1);
            cs[k] = D[q];
            add(cs[k], +1);
          } break;
          case 2: {
            const res = query(E[q], 0);
            debug {
              writeln(E[q], "; ", sum0, " ", sum1, " ", dep[E[q]], " ", res);
            }
            long ans;
            ans += sum0 * dep[E[q]];
            ans += sum1;
            ans -= 2 * (res - sum0);
            writeln(ans);
            debug {
              long brt;
              foreach (k; 0 .. K) {
                brt += dist[E[q]][cs[k]];
              }
              assert(brt == ans);
            }
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0