結果
問題 | No.2020 Sum of Common Prefix Length |
ユーザー | tokusakurai |
提出日時 | 2022-07-22 22:41:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 8,890 bytes |
コンパイル時間 | 2,862 ms |
コンパイル使用メモリ | 232,232 KB |
実行使用メモリ | 86,756 KB |
最終ジャッジ日時 | 2024-07-04 07:14:20 |
合計ジャッジ時間 | 13,331 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | WA | - |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | RE | - |
testcase_29 | WA | - |
testcase_30 | RE | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | WA | - |
testcase_37 | RE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i <= n; i++) #define rep3(i, x, n) for (int i = x; i >= n; i--) #define each(e, v) for (auto &e : v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; } template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; } template <typename T> int flg(T x, int i) { return (x >> i) & 1; } template <typename T> void print(const vector<T> &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' '); if (v.empty()) cout << '\n'; } template <typename T> void printn(const vector<T> &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << '\n'; } template <typename T> int lb(const vector<T> &v, T x) { return lower_bound(begin(v), end(v), x) - begin(v); } template <typename T> int ub(const vector<T> &v, T x) { return upper_bound(begin(v), end(v), x) - begin(v); } template <typename T> void rearrange(vector<T> &v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <typename T> vector<int> id_sort(const vector<T> &v, bool greater = false) { int n = v.size(); vector<int> ret(n); iota(begin(ret), end(ret), 0); sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; }); return ret; } template <typename S, typename T> pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) { return make_pair(p.first + q.first, p.second + q.second); } template <typename S, typename T> pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) { return make_pair(p.first - q.first, p.second - q.second); } template <typename S, typename T> istream &operator>>(istream &is, pair<S, T> &p) { S a; T b; is >> a >> b; p = make_pair(a, b); return is; } template <typename S, typename T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << p.first << ' ' << p.second; } struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; const int MOD = 1000000007; // const int MOD = 998244353; template <typename T> struct Binary_Indexed_Tree { vector<T> bit; const int n; Binary_Indexed_Tree(const vector<T> &v) : n((int)v.size()) { bit.resize(n + 1); copy(begin(v), end(v), begin(bit) + 1); for (int a = 2; a <= n; a <<= 1) { for (int b = a; b <= n; b += a) bit[b] += bit[b - a / 2]; } } Binary_Indexed_Tree(int n, const T &x) : n(n) { bit.resize(n + 1); vector<T> v(n, x); copy(begin(v), end(v), begin(bit) + 1); for (int a = 2; a <= n; a <<= 1) { for (int b = a; b <= n; b += a) bit[b] += bit[b - a / 2]; } } void add(int i, const T &x) { for (i++; i <= n; i += (i & -i)) bit[i] += x; } void change(int i, const T &x) { add(i, x - query(i, i + 1)); } T sum(int i) const { T ret = 0; for (; i > 0; i -= (i & -i)) ret += bit[i]; return ret; } T query(int l, int r) const { return sum(r) - sum(l); } T operator[](int i) const { return query(i, i + 1); } int lower_bound(T x) const { int ret = 0; for (int k = 31 - __builtin_clz(n); k >= 0; k--) { if (ret + (1 << k) <= n && bit[ret + (1 << k)] < x) x -= bit[ret += (1 << k)]; } return ret; } int upper_bound(T x) const { int ret = 0; for (int k = 31 - __builtin_clz(n); k >= 0; k--) { if (ret + (1 << k) <= n && bit[ret + (1 << k)] <= x) x -= bit[ret += (1 << k)]; } return ret; } }; template <bool directed = false> struct Heavy_Light_Decomposition { struct edge { int to, id; edge(int to, int id) : to(to), id(id) {} }; vector<vector<edge>> es; vector<int> par, si, depth; vector<int> root; // 属する連結成分の根 vector<int> id_v, id_e; // 各頂点、各辺が一列に並べたときに何番目に相当するか(辺の番号は 1,2,...,n-1 となることに注意) const int n; int m; Heavy_Light_Decomposition(int n) : es(n), par(n), si(n), depth(n), root(n), id_v(n), id_e(n - 1), n(n), m(0) {} void add_edge(int from, int to) { es[from].emplace_back(to, m); if (!directed) es[to].emplace_back(from, m); m++; } int _dfs1(int now, int pre = -1) { par[now] = pre; if (pre == -1) depth[now] = 0; si[now] = 1; for (auto &e : es[now]) { if (e.to != pre) { depth[e.to] = depth[now] + 1; si[now] += _dfs1(e.to, now); } } return si[now]; } void _dfs2(int now, bool st, int &s, int pre = -1) { root[now] = (st ? now : root[pre]); id_v[now] = s++; edge heavy = {-1, -1}; int M = 0; for (auto &e : es[now]) { if (e.to == pre) continue; if (M < si[e.to]) M = si[e.to], heavy = e; } if (heavy.id != -1) { id_e[heavy.id] = s; _dfs2(heavy.to, false, s, now); } for (auto &e : es[now]) { if (e.to != pre && e.id != heavy.id) { id_e[e.id] = s; _dfs2(e.to, true, s, now); } } } void decompose(int root = 0) { _dfs1(root); int s = 0; _dfs2(root, true, s); } vector<pair<int, int>> get_path(int u, int v, bool use_edge = false) { // パスに対応する区間たちを列挙 vector<pair<int, int>> ret; while (root[u] != root[v]) { if (depth[root[u]] > depth[root[v]]) swap(u, v); ret.emplace_back(id_v[root[v]], id_v[v] + 1); v = par[root[v]]; } if (depth[u] > depth[v]) swap(u, v); ret.emplace_back(id_v[u] + use_edge, id_v[v] + 1); return ret; } vector<pair<int, int>> get_path_noncommutative(int u, int v, bool use_edge = false) { // クエリが非可換の場合 vector<pair<int, int>> l, r; while (root[u] != root[v]) { if (depth[root[u]] > depth[root[v]]) { l.emplace_back(id_v[u] + 1, id_v[root[u]]); u = par[root[u]]; } else { r.emplace_back(id_v[root[v]], id_v[v] + 1); v = par[root[v]]; } } if (depth[u] > depth[v]) { l.emplace_back(id_v[u] + 1, id_v[v] + use_edge); } else { r.emplace_back(id_v[u] + use_edge, id_v[v] + 1); } reverse(begin(r), end(r)); for (auto &e : r) l.push_back(e); return l; } }; int main() { int N; cin >> N; vector<string> S(N); rep(i, N) cin >> S[i]; int Q; cin >> Q; vector<int> t(Q), id(Q); vector<char> c(Q); vector<string> T = S; rep(i, Q) { cin >> t[i] >> id[i]; id[i]--; if (t[i] == 1) { cin >> c[i]; T[id[i]] += c[i]; } } // rep(i, N) cout << T[i] << '\n'; vector<vector<int>> ptr(N); int K = 1; vector<vector<int>> chi(1, vector<int>(26, -1)); rep(i, N) { int now = 0; each(e, S[i]) { int x = e - 'a'; if (chi[now][x] == -1) { chi[now][x] = K; chi.eb(26, -1); K++; } now = chi[now][x]; ptr[i].eb(now); } } vector<ll> a(K, 0); rep(i, N) { int L = sz(S[i]); rep(j, L) a[ptr[i][j]]++; } // cout << K << '\n'; Heavy_Light_Decomposition G(K); rep(i, K) { rep(j, 26) { if (chi[i][j] != -1) G.add_edge(i, chi[i][j]); } } G.decompose(); vector<ll> b(K, 0); rep(i, K) b[G.id_v[i]] = a[i]; Binary_Indexed_Tree<ll> bit(b); vector<int> L(N); rep(i, N) L[i] = sz(S[i]) - 1; rep(i, Q) { int e = id[i]; if (t[i] == 1) { int p = ptr[e][++L[e]]; bit.add(G.id_v[p], 1); } else { int u = 0, v = ptr[e][L[e]]; ll ans = 0; for (auto e : G.get_path(u, v)) ans += bit.query(e.first, e.second); cout << ans << '\n'; } } }