結果

問題 No.2020 Sum of Common Prefix Length
ユーザー KudeKude
提出日時 2022-07-23 00:39:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 5,020 bytes
コンパイル時間 3,221 ms
コンパイル使用メモリ 248,940 KB
実行使用メモリ 91,000 KB
最終ジャッジ日時 2023-09-17 14:16:14
合計ジャッジ時間 9,082 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 48 ms
6,512 KB
testcase_08 AC 49 ms
6,508 KB
testcase_09 AC 48 ms
6,676 KB
testcase_10 AC 49 ms
6,496 KB
testcase_11 AC 50 ms
6,560 KB
testcase_12 AC 50 ms
6,504 KB
testcase_13 AC 49 ms
6,476 KB
testcase_14 AC 51 ms
6,736 KB
testcase_15 AC 64 ms
13,224 KB
testcase_16 AC 64 ms
13,248 KB
testcase_17 AC 130 ms
33,664 KB
testcase_18 AC 112 ms
26,124 KB
testcase_19 AC 115 ms
28,360 KB
testcase_20 AC 170 ms
80,760 KB
testcase_21 AC 222 ms
76,756 KB
testcase_22 AC 211 ms
61,684 KB
testcase_23 AC 210 ms
61,120 KB
testcase_24 AC 221 ms
77,092 KB
testcase_25 AC 225 ms
76,624 KB
testcase_26 AC 104 ms
58,848 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 53 ms
8,928 KB
testcase_29 AC 55 ms
9,068 KB
testcase_30 AC 53 ms
8,896 KB
testcase_31 AC 148 ms
81,376 KB
testcase_32 AC 150 ms
91,000 KB
testcase_33 AC 107 ms
56,988 KB
testcase_34 AC 72 ms
24,548 KB
testcase_35 AC 73 ms
25,164 KB
testcase_36 AC 112 ms
54,240 KB
testcase_37 AC 88 ms
35,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

ll op(ll x, ll y) { return x + y; }
ll e() { return 0; }

struct HLD {
  const vector<vector<int>>& to;
  int root, n;
  vector<int> sz, parent, depth, idx, ridx, head, inv;

  HLD(const vector<vector<int>>& to, int root=0)
      : to(to), root(root), n(to.size()),
        sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) {
    init_tree_data(root, -1, 0);
    int nxt = 0;
    assign_idx(root, root, nxt);
  }
  void init_tree_data(int u, int p, int d) {
    parent[u] = p;
    depth[u] = d;
    int s = 1;
    for (int v: to[u]) if (v != p) {
      init_tree_data(v, u, d+1);
      s += sz[v];
    }
    sz[u] = s;
  }
  void assign_idx(int u, int h, int& nxt, int p=-1) {
    head[u] = h;
    idx[u] = nxt;
    inv[nxt] = u;
    nxt++;
    int heaviest = -1;
    int mxweight = 0;
    for (int v: to[u]) if (v != p) {
      if (sz[v] > mxweight) {
        heaviest = v;
        mxweight = sz[v];
      }
    }
    if (heaviest != -1) {
      assign_idx(heaviest, h, nxt, u);
      for (int v: to[u]) if (v != p && v != heaviest) {
        assign_idx(v, v, nxt, u);
      }
    }
    ridx[u] = nxt;
  }

  int lca(int u, int v) {
    while (head[u] != head[v]) {
      if (depth[head[u]] > depth[head[v]]) {
        u = parent[head[u]];
      } else {
        v = parent[head[v]];
      }
    }
    return depth[u] < depth[v] ? u : v;
  }
  // returns reference to tuple of (path fragments from x upto lca (excluding lca), those from y, lca)
  // storage of retval is reused to avoid creating short vectors on each query
  tuple<vector<pair<int, int>>, vector<pair<int, int>>, int> paths_res;
  auto& paths(int x, int y) {
    auto& [x_paths, y_paths, lca] = paths_res;
    x_paths.clear();
    y_paths.clear();
    while (head[x] != head[y]) {
      int hx = head[x], hy = head[y];
      if (depth[hx] > depth[hy]) {
        x_paths.emplace_back(x, hx); x = parent[hx];
      } else {
        y_paths.emplace_back(y, hy); y = parent[hy];
      }
    }
    if (depth[x] > depth[y]) {
      x_paths.emplace_back(x, inv[idx[y] + 1]); x = y;
    } else if (depth[x] < depth[y]) {
      y_paths.emplace_back(y, inv[idx[x] + 1]); y = x;
    }
    lca = x;
    return paths_res;
  }
  int dist(int u, int v) {
    int w = lca(u, v);
    return depth[u] + depth[v] - 2 * depth[w];
  }
  template <class F> int max_ancestor(int v, F f) {
    if (!f(v)) return -1;
    int hv = head[v];
    int p = parent[hv];
    while (p != -1 && f(p)) {
      v = p;
      hv = head[v];
      p = parent[hv];
    }
    int il = idx[hv] - 1, ir = idx[v];
    while (ir - il > 1) {
      int ic = (il + ir) / 2;
      (f(inv[ic]) ? ir : il) = ic;
    }
    return inv[ir];
  }
  int ascend(int v, int k) {
    assert(depth[v] >= k);
    int td = depth[v] - k;
    int hv = head[v];
    while (depth[hv] > td) {
      v = parent[hv];
      hv = head[v];
    }
    int rest = depth[v] - td;
    return inv[idx[v] - rest];
  }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> s(n);
  vector<array<int, 26>> trie(1);
  VVI to(1);
  VL d(1);
  VI pos(n);
  auto mv = [&](int v, char c) {
    int x = c - 'a';
    int w = trie[v][x];
    if (!w) {
      w = trie.size();
      trie.emplace_back();
      to.emplace_back();
      d.emplace_back();
      trie[v][x] = w;
      to[v].emplace_back(w);
    }
    return w;
  };
  rep(i, n) {
    string s;
    cin >> s;
    int v = 0;
    for (char c: s) {
      v = mv(v, c);
      d[v]++;
    }
    pos[i] = v;
  }
  int q;
  cin >> q;
  vector<tuple<int, int, char>> qs(q);
  VI pos_tmp = pos;
  for(auto& [t, i, c]: qs) {
    cin >> t;
    if (t == 1) {
      cin >> i >> c;
      i--;
      pos_tmp[i] = mv(pos_tmp[i], c);
    } else {
      cin >> i;
      i--;
    }
  }
  int sz = to.size();
  HLD hld(to);
  VL seg_init(sz);
  rep(i, sz) seg_init[hld.idx[i]] = d[i];
  segtree<ll, op, e> seg(move(seg_init));
  for(auto [t, i, c]: qs) {
    if (t == 1) {
      pos[i] = mv(pos[i], c);
      int pi = hld.idx[pos[i]];
      seg.set(pi, seg.get(pi) + 1);
    } else {
      auto& [p1, p2, c] = hld.paths(pos[i], 0);
      ll ans = 0;
      for(auto [u, v]: p1) {
        int iu = hld.idx[u], iv = hld.idx[v];
        ans += seg.prod(iv, iu + 1);
      }
      cout << ans << '\n';
    }
  }
}
0