結果
問題 | No.2020 Sum of Common Prefix Length |
ユーザー | kwm_t |
提出日時 | 2022-07-23 18:04:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 236 ms / 2,000 ms |
コード長 | 5,490 bytes |
コンパイル時間 | 4,569 ms |
コンパイル使用メモリ | 286,012 KB |
実行使用メモリ | 117,496 KB |
最終ジャッジ日時 | 2024-07-05 07:45:44 |
合計ジャッジ時間 | 11,459 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 78 ms
12,672 KB |
testcase_08 | AC | 69 ms
12,800 KB |
testcase_09 | AC | 70 ms
12,672 KB |
testcase_10 | AC | 70 ms
12,668 KB |
testcase_11 | AC | 70 ms
12,800 KB |
testcase_12 | AC | 70 ms
12,800 KB |
testcase_13 | AC | 70 ms
12,928 KB |
testcase_14 | AC | 72 ms
12,928 KB |
testcase_15 | AC | 115 ms
32,172 KB |
testcase_16 | AC | 115 ms
32,172 KB |
testcase_17 | AC | 134 ms
51,556 KB |
testcase_18 | AC | 116 ms
39,396 KB |
testcase_19 | AC | 116 ms
41,952 KB |
testcase_20 | AC | 205 ms
111,120 KB |
testcase_21 | AC | 224 ms
101,360 KB |
testcase_22 | AC | 214 ms
93,044 KB |
testcase_23 | AC | 211 ms
91,744 KB |
testcase_24 | AC | 232 ms
109,552 KB |
testcase_25 | AC | 236 ms
108,696 KB |
testcase_26 | AC | 128 ms
69,576 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 87 ms
19,040 KB |
testcase_29 | AC | 88 ms
19,368 KB |
testcase_30 | AC | 86 ms
18,616 KB |
testcase_31 | AC | 193 ms
108,592 KB |
testcase_32 | AC | 201 ms
117,496 KB |
testcase_33 | AC | 137 ms
69,312 KB |
testcase_34 | AC | 130 ms
38,520 KB |
testcase_35 | AC | 116 ms
38,656 KB |
testcase_36 | AC | 152 ms
76,268 KB |
testcase_37 | AC | 139 ms
53,652 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<int,int> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct trie { int kind; vector<int>node; vector<vector<int>>next; vector<bool>end; trie(int _kind) :kind(_kind) { node.resize(1); end.resize(1); next.emplace_back(vector<int>(kind, -1)); } void insert(const vector<int>&v) { int now = 0; rep(i, v.size()) { if (-1 == next[now][v[i]]) { next[now][v[i]] = node.size(); node.emplace_back(0); end.push_back(false); next.emplace_back(vector<int>(kind, -1)); } now = next[now][v[i]]; node[now]++; } end[now] = true; } bool seach(const vector<int>&v) { int now = 0; rep(i, v.size()) { if (-1 == next[now][v[i]])return false; if (node[next[now][v[i]]] <= 0)return false; now = next[now][v[i]]; } return end[now]; } void erase(const vector<int>&v) { int now = 0; rep(i, v.size()) { now = next[now][v[i]]; node[now]--; } end[now] = false; } // prefixも取得する vector<int> getIndexEx(const vector<int>&v) { vector<int>ret; int idx = 0, now = 0; rep(i, v.size()) { if (0 != now) { idx += node[now]; rep(j, 2)if (-1 != next[now][j])idx -= node[next[now][j]]; } if (0 == v[i]) { if (-1 == next[now][v[i]])break; now = next[now][v[i]]; } else { if (-1 != next[now][0]) idx += node[next[now][0]]; if (-1 == next[now][v[i]])break; now = next[now][v[i]]; } ret.push_back(now); } return ret; } }; vector<int> trans(string s) { vector<int>ret(s.size()); rep(i, s.size()) ret[i] = s[i] - 'a'; return ret; } using S = long long; S op(S a, S b) { return a + b; } S e() { return 0; } struct HeavyLightDecomposition { HeavyLightDecomposition(vector<vector<int>> &_g, vector<S>&_val) :g(_g), val(_val) { n = g.size(); sz.resize(n); par.resize(n); in.resize(n); out.resize(n); rev.resize(n); head.resize(n); } int n; vector<vector<int>>g; vector<int>sz, par, in, out, rev, head; vector<S>val; segtree<S, op, e>seg; void build() { dfs_sz(0); int time = 0; dfs_hld(0, time); initSegmentTree(); } void dfs_sz(int v, int p = -1) { par[v] = p; sz[v] = 1; if (g[v].size() && (p == g[v][0]))swap(g[v][0], g[v].back()); for (auto &e : g[v]) { if (p == e)continue; dfs_sz(e, v); sz[v] += sz[e]; if (sz[g[v][0]] < sz[e])swap(g[v][0], e); } } void dfs_hld(int v, int &time, int p = -1) { in[v] = time; time++; rev[in[v]] = v; for (auto &e : g[v]) { if (p == e)continue; if (e == g[v][0])head[e] = head[v]; else head[e] = e; dfs_hld(e, time, v); } out[v] = time; } void initSegmentTree() { vector<S>_val(n); rep(i, n)_val[i] = val[rev[i]]; segtree<S, op, e> _seg(_val); seg = _seg; } S query(int u, int v) { S ret = e(); for (;; v = par[head[v]]) { if (in[u] > in[v])swap(u, v); if (head[u] == head[v])break; auto get = seg.prod(in[head[v]], in[v] + 1); ret = op(ret, get); } auto get = seg.prod(in[u], in[v] + 1); ret = op(ret, get); return ret; } int la(int v, int k) { while (1) { int u = head[v]; if (in[v] - k > in[u])return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for (;; v = par[head[v]]) { if (in[u] > in[v])swap(u, v); if (head[u] == head[v])return u; } } // point update void update_point(int u, S x) { auto get = seg.get(in[u]); seg.set(in[u], get + 1); } void debug() { rep(i, n) { cout << seg.prod(i, i + 1) << " "; } cout << endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<string>s(n); rep(i, n)cin >> s[i]; int q; cin >> q; vector<int>t(q), x(q); vector<char>c(q); vector<string>s2 = s; rep(i, q) { cin >> t[i] >> x[i], x[i]--; if (1 == t[i]) { cin >> c[i]; s2[x[i]] += c[i]; } } trie tr(26); rep(i, n)tr.insert(trans(s2[i])); vector<vector<int>>idx(n); rep(i, n) idx[i] = tr.getIndexEx(trans(s2[i])); int sz = 0; rep(i, n) chmax(sz, idx[i].back() + 1); vector<S>v(sz); rep(i, n)rep(j, s[i].size()) v[idx[i][j]]++; vector<vector<int>>g(sz); rep(i, n) rep(j, idx[i].size()) { int u = 0, v = 0; if (0 != j)u = idx[i][j - 1]; v = idx[i][j]; g[u].push_back(v); g[v].push_back(u); } rep(i, g.size()) { sort(all(g[i])); g[i].erase(unique(all(g[i])), g[i].end()); } HeavyLightDecomposition hld(g, v); hld.build(); vector<int>now(n); rep(i, n)now[i] = s[i].size(); /*rep(i, n) { rep(j, idx[i].size()) { cout << idx[i][j] << " "; } cout << endl; }*/ rep(i, q) { if (1 == t[i]) { hld.update_point(idx[x[i]][now[x[i]]], 1); now[x[i]]++; } else { S ans = hld.query(0, idx[x[i]][now[x[i]] - 1]); cout << ans << endl; } //shld.debug(); } return 0; }