結果

問題 No.2020 Sum of Common Prefix Length
ユーザー SSRSSSRS
提出日時 2022-07-22 22:04:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 3,240 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 192,964 KB
実行使用メモリ 62,944 KB
最終ジャッジ日時 2023-09-17 10:09:36
合計ジャッジ時間 14,193 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 5 ms
4,384 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 250 ms
6,280 KB
testcase_08 AC 250 ms
6,268 KB
testcase_09 AC 251 ms
6,208 KB
testcase_10 AC 253 ms
6,284 KB
testcase_11 AC 255 ms
6,340 KB
testcase_12 AC 253 ms
6,244 KB
testcase_13 AC 252 ms
6,196 KB
testcase_14 AC 261 ms
6,212 KB
testcase_15 AC 291 ms
12,652 KB
testcase_16 AC 284 ms
12,684 KB
testcase_17 AC 312 ms
27,716 KB
testcase_18 AC 292 ms
20,632 KB
testcase_19 AC 295 ms
22,188 KB
testcase_20 AC 345 ms
57,988 KB
testcase_21 AC 410 ms
50,440 KB
testcase_22 AC 416 ms
50,568 KB
testcase_23 AC 397 ms
49,480 KB
testcase_24 AC 407 ms
56,492 KB
testcase_25 AC 418 ms
55,796 KB
testcase_26 AC 263 ms
39,760 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 272 ms
8,328 KB
testcase_29 AC 271 ms
8,568 KB
testcase_30 AC 267 ms
8,336 KB
testcase_31 AC 315 ms
56,672 KB
testcase_32 AC 314 ms
62,944 KB
testcase_33 AC 269 ms
39,004 KB
testcase_34 AC 300 ms
19,288 KB
testcase_35 AC 286 ms
19,416 KB
testcase_36 AC 287 ms
39,244 KB
testcase_37 AC 303 ms
27,708 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]]][S[x[i]][L[x[i]]]];
      L[x[i]]++;
      HLD.add(curr[x[i]], 1);
    }
    if (t[i] == 2){
      cout << HLD.sum(curr[x[i]]) << endl;
    }
  }
}
0