結果

問題 No.2020 Sum of Common Prefix Length
ユーザー milanis48663220milanis48663220
提出日時 2022-07-22 23:17:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 7,338 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 147,388 KB
実行使用メモリ 103,820 KB
最終ジャッジ日時 2023-09-17 12:18:32
合計ジャッジ時間 12,739 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,384 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 211 ms
15,276 KB
testcase_08 AC 209 ms
15,552 KB
testcase_09 AC 209 ms
15,392 KB
testcase_10 AC 212 ms
15,388 KB
testcase_11 AC 209 ms
15,768 KB
testcase_12 AC 211 ms
15,424 KB
testcase_13 AC 211 ms
15,404 KB
testcase_14 AC 214 ms
15,732 KB
testcase_15 AC 257 ms
30,164 KB
testcase_16 AC 255 ms
30,012 KB
testcase_17 AC 258 ms
43,988 KB
testcase_18 AC 237 ms
34,448 KB
testcase_19 AC 240 ms
37,360 KB
testcase_20 AC 328 ms
84,460 KB
testcase_21 AC 358 ms
74,948 KB
testcase_22 AC 362 ms
74,664 KB
testcase_23 AC 347 ms
73,512 KB
testcase_24 AC 363 ms
82,636 KB
testcase_25 AC 374 ms
81,672 KB
testcase_26 AC 268 ms
68,212 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 233 ms
19,816 KB
testcase_29 AC 237 ms
20,344 KB
testcase_30 AC 232 ms
19,504 KB
testcase_31 AC 330 ms
90,912 KB
testcase_32 AC 342 ms
103,820 KB
testcase_33 AC 283 ms
68,136 KB
testcase_34 AC 273 ms
37,988 KB
testcase_35 AC 269 ms
37,996 KB
testcase_36 AC 287 ms
68,236 KB
testcase_37 AC 290 ms
49,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> 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<int, int>;


/**
 * verified?: https://atcoder.jp/contests/tenka1-2016-final/submissions/24942772
 */ 
class Trie{
    struct node{
        char val;
        int depth;
        bool is_end;
        vector<int> children;
        node(char val, int depth): val(val), depth(depth) {
            children = vector<int>();
            is_end = false;
        };
    };
    public:
    vector<node> 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<int> &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<int> &indices){
        if(nd_idx != root()) 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 <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(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<vector<int>> tree;
    vector<int> parents; // もとの木におけるindex
    vector<vector<int>> components;
    vector<P> pos; // (component_idx, ord in component)
    int root;
    HeavyLightDecomposition(vector<vector<int>> tree, int root): tree(tree), root(root){
        n = tree.size();
        pos.resize(n);
        seen.resize(n);
        subtree_size.resize(n);
        exec();
    }
    private:
    vector<bool> seen;
    vector<int> subtree_size;
    void exec(){
        vector<int> 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<int> &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<int> &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<string> s(n);
    for(int i = 0; i < n; i++) cin >> s[i];
    auto t = s;
    int q; cin >> q;
    vector<int> x(q);
    vector<char> 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<vector<int>> indices(n);
    for(int i = 0; i < n; i++) {
        trie.add(t[i], indices[i]);
    }
    int m = trie.nodes.size();
    vector<vector<int>> g(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(auto v: trie.nodes[0].children){
        g[0].push_back(v);
        g[v].push_back(0);
    }
    auto hld = HeavyLightDecomposition(g, 0);
    vector<bit<ll>> bts;
    for(int i = 0; i < hld.components.size(); i++){
        int m = hld.components[i].size();
        bit<ll> 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, 1);
        }
    }
    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];
        }
    }
}
0