結果

問題 No.2020 Sum of Common Prefix Length
ユーザー SSRSSSRS
提出日時 2022-07-22 22:03:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 2,000 ms
コード長 3,241 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 192,000 KB
実行使用メモリ 62,536 KB
最終ジャッジ日時 2023-09-17 10:06:37
合計ジャッジ時間 14,139 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 245 ms
6,272 KB
testcase_08 AC 243 ms
6,244 KB
testcase_09 AC 248 ms
6,256 KB
testcase_10 AC 245 ms
6,220 KB
testcase_11 AC 247 ms
6,464 KB
testcase_12 AC 249 ms
6,220 KB
testcase_13 AC 247 ms
6,216 KB
testcase_14 AC 250 ms
6,300 KB
testcase_15 AC 277 ms
12,916 KB
testcase_16 AC 274 ms
12,648 KB
testcase_17 AC 311 ms
27,728 KB
testcase_18 AC 290 ms
21,080 KB
testcase_19 AC 296 ms
22,120 KB
testcase_20 AC 341 ms
57,772 KB
testcase_21 AC 413 ms
50,892 KB
testcase_22 AC 404 ms
50,380 KB
testcase_23 AC 401 ms
49,496 KB
testcase_24 AC 408 ms
55,796 KB
testcase_25 AC 412 ms
55,920 KB
testcase_26 AC 263 ms
39,624 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 256 ms
8,284 KB
testcase_29 AC 258 ms
8,604 KB
testcase_30 AC 256 ms
8,372 KB
testcase_31 AC 317 ms
56,648 KB
testcase_32 AC 319 ms
62,536 KB
testcase_33 AC 272 ms
38,688 KB
testcase_34 AC 284 ms
19,380 KB
testcase_35 AC 273 ms
19,588 KB
testcase_36 AC 288 ms
40,316 KB
testcase_37 AC 288 ms
27,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct invertible_binary_indexed_tree{
  int N;
  vector<T> BIT;
  function<T(T, T)> f;
  function<T(T)> inv;
  T E;
  invertible_binary_indexed_tree(){
  }
  invertible_binary_indexed_tree(int N, function<T(T, T)> f, function<T(T)> inv, T E): N(N), BIT(N + 1, E), f(f), inv(inv), E(E){
  }
  void add(int i, T x){
    i++;
    while (i <= N){
      BIT[i] = f(BIT[i], x);
      i += i & -i;
    }
  }
  T sum(int i){
    T ans = E;
    while (i > 0){
      ans = f(ans, BIT[i]);
      i -= i & -i;
    }
    return ans;
  }
  T sum(int l, int r){
    return f(sum(r), inv(sum(l)));
  }
};
struct heavy_light_decomposition{
  vector<int> p, sz, in, next;
  invertible_binary_indexed_tree<int> BIT;
  void dfs1(vector<vector<int>> &c, int v){
    sz[v] = 1;
    for (int &w : c[v]){
      dfs1(c, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        swap(w, c[v][0]);
      }
    }
  }
  void dfs2(vector<vector<int>> &c, int &t, int v){
    in[v] = t;
    t++;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(c, t, w);
    }
  }
  heavy_light_decomposition(vector<int> &p, vector<vector<int>> &c, int r = 0): p(p){
    int N = p.size();
    sz = vector<int>(N);
    dfs1(c, r);
    in = vector<int>(N);
    next = vector<int>(N, r);
    int t = 0;
    dfs2(c, t, r);
    BIT = invertible_binary_indexed_tree<int>(N, plus<int>(), negate<int>(), 0);
  }
  void add(int p, int x){
    BIT.add(in[p], x);
  }
  int sum(int v){
    int ans = 0;
    while (next[v] != 0){
      ans += BIT.sum(in[next[v]], in[v] + 1);
      v = p[next[v]];
    }
    ans += BIT.sum(0, in[v] + 1);
    return ans;
  }
};
int main(){
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++){
    cin >> S[i];
  }
  vector<int> L(N);
  for (int i = 0; i < N; i++){
    L[i] = S[i].size();
  }
  int Q;
  cin >> Q;
  vector<int> t(Q), x(Q);
  vector<char> c(Q);
  for (int i = 0; i < Q; i++){
    cin >> t[i];
    if (t[i] == 1){
      cin >> x[i] >> c[i];
      x[i]--;
      S[x[i]] += c[i];
    }
    if (t[i] == 2){
      cin >> x[i];
      x[i]--;
    }
  }
  int V = 1;
  vector<map<char, int>> trie(V);
  for (int i = 0; i < N; i++){
    int v = 0;
    int L2 = S[i].size();
    for (int j = 0; j < L2; j++){
      auto itr = trie[v].find(S[i][j]);
      int w;
      if (itr != trie[v].end()){
        w = (*itr).second;
      } else {
        w = V;
        trie[v][S[i][j]] = V;
        trie.push_back({});
        V++;
      }
      v = w;
    }
  }
  vector<int> pr(V, -1);
  vector<vector<int>> ch(V);
  for (int i = 0; i < V; i++){
    for (auto P : trie[i]){
      pr[P.second] = i;
      ch[i].push_back(P.second);
    }
  }
  heavy_light_decomposition HLD(pr, ch);
  vector<int> curr(N, 0);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < L[i]; j++){
      curr[i] = trie[curr[i]][S[i][j]];
      HLD.add(curr[i], 1);
    }
  }
  for (int i = 0; i < Q; i++){
    if (t[i] == 1){
      curr[x[i]] = trie[curr[x[i]]][c[i]];
      assert(curr[x[i]] != 0);
      HLD.add(curr[x[i]], 1);
    }
    if (t[i] == 2){
      cout << HLD.sum(curr[x[i]]) << endl;
    }
  }
}
0