#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using P = pair; /** * verified?: https://atcoder.jp/contests/tenka1-2016-final/submissions/24942772 */ class Trie{ struct node{ char val; int depth; bool is_end; vector children; node(char val, int depth): val(val), depth(depth) { children = vector(); is_end = false; }; }; public: vector nodes; int n_nodes = 0; int n_bits; Trie() { nodes.push_back(node('#', 0)); n_nodes = 1; }; int get_child(int nd_idx, char c){ for(int i: nodes[nd_idx].children){ if(nodes[i].val == c) return i; } assert(false); } int root(){ return 0; } bool has_child(int nd_idx, char c){ for(int i: nodes[nd_idx].children){ if(nodes[i].val == c) return true; } return false; } void add(string &s, vector &indices){ add(root(), s, indices); } void dfs(){ for(int i: nodes[root()].children) dfs(nodes[i], ""); } private: void add(int nd_idx, string &s, vector &indices){ indices.push_back(nd_idx); int depth = nodes[nd_idx].depth; if(depth == s.size()) { nodes[nd_idx].is_end = true; return; } if(!has_child(nd_idx, s[depth])){ nodes.push_back(node(s[depth], depth+1)); nodes[nd_idx].children.push_back(n_nodes); n_nodes++; } add(get_child(nd_idx, s[depth]), s, indices); } void dfs(node &nd, string s){ s += nd.val; if(nd.is_end){ cout << s << endl; } for(int i: nd.children) dfs(nodes[i], s); } }; //1-indexed template struct bit{ int n; vector data; bit(int n_){ n = 1; while(n < n_) n *= 2; data = vector(n+1); for(int i = 0; i <= n; i++) data[i] = 0; } T sum(int i){ T ret = 0; while(i > 0){ ret += data[i]; i -= i&-i; } return ret; } void add(int i, T x){ while(i <= n){ data[i] += x; i += i&-i; } } }; /** * verified?: https://atcoder.jp/contests/kupc2021/submissions/27139986 */ class HeavyLightDecomposition{ public: int n; vector> tree; vector parents; // もとの木におけるindex vector> components; vector

pos; // (component_idx, ord in component) int root; HeavyLightDecomposition(vector> tree, int root): tree(tree), root(root){ n = tree.size(); pos.resize(n); seen.resize(n); subtree_size.resize(n); exec(); } private: vector seen; vector subtree_size; void exec(){ vector nx(n, -1); dfs1(root, nx); seen.assign(n, false); int m = 0; parents.push_back(-1); dfs2(root, nx, 0); } void add_to_component(int v, int component_idx){ assert(component_idx <= components.size()); if(components.size() == component_idx){ components.push_back({}); } pos[v] = P(component_idx, components[component_idx].size()); components[component_idx].push_back(v); } void dfs1(int v, vector &nx){ seen[v] = true; subtree_size[v] = 1; int max_size = -1; for(int to: tree[v]){ if(seen[to]) continue; dfs1(to, nx); subtree_size[v] += subtree_size[to]; if(chmax(max_size, subtree_size[to])) nx[v] = to; } } void dfs2(int v, vector &nx, int component_idx){ seen[v] = true; add_to_component(v, component_idx); for(int to: tree[v]){ if(seen[to]) continue; if(to == nx[v]){ dfs2(to, nx, component_idx); }else{ parents.push_back(v); dfs2(to, nx, components.size()); } } } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n; cin >> n; vector s(n); for(int i = 0; i < n; i++) cin >> s[i]; auto t = s; int q; cin >> q; vector x(q); vector c(q, '#'); for(int i = 0; i < q; i++){ int tp; cin >> tp >> x[i]; x[i]--; if(tp == 1) { cin >> c[i]; t[x[i]] += c[i]; } } Trie trie; vector> indices(n); for(int i = 0; i < n; i++) { trie.add(t[i], indices[i]); } int m = trie.nodes.size(); vector> g(m); vector sum(m); for(int i = 0; i < n; i++){ for(int j = 0; j+1 < indices[i].size(); j++){ int u = indices[i][j], v = indices[i][j+1]; g[u].push_back(v); g[v].push_back(u); } for(int j = 0; j < s[i].size(); j++){ int u = indices[i][j]; sum[u] = 1; } } auto hld = HeavyLightDecomposition(g, 0); vector> bts; for(int i = 0; i < hld.components.size(); i++){ int m = hld.components[i].size(); bit bt(m); bts.push_back(bt); } for(int i = 0; i < n; i++){ for(int j = 0; j < s[i].size(); j++) { int v = indices[i][j]; auto [ci, ord] = hld.pos[v]; bts[ci].add(ord+1, sum[v]); } } for(int i = 0; i < q; i++){ if(c[i] == '#'){ ll ans = 0; int len = s[x[i]].size(); int v = indices[x[i]][len-1]; while(true){ auto [ci, ord] = hld.pos[v]; ans += bts[ci].sum(ord+1); v = hld.parents[ci]; if(v == -1) break; } cout << ans << endl; }else{ int len = s[x[i]].size(); int idx = indices[x[i]][len]; auto [component_idx, ord] = hld.pos[idx]; bts[component_idx].add(ord+1, 1); s[x[i]] += c[i]; } } }