結果

問題 No.2020 Sum of Common Prefix Length
ユーザー milanis48663220milanis48663220
提出日時 2022-07-22 23:17:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 359 ms / 2,000 ms
コード長 7,338 bytes
コンパイル時間 1,978 ms
コンパイル使用メモリ 150,024 KB
実行使用メモリ 105,544 KB
最終ジャッジ日時 2024-07-04 08:05:09
合計ジャッジ時間 12,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 5 ms
6,944 KB
testcase_07 AC 207 ms
15,488 KB
testcase_08 AC 204 ms
15,360 KB
testcase_09 AC 206 ms
15,616 KB
testcase_10 AC 208 ms
15,360 KB
testcase_11 AC 210 ms
15,744 KB
testcase_12 AC 206 ms
15,616 KB
testcase_13 AC 208 ms
15,744 KB
testcase_14 AC 210 ms
15,616 KB
testcase_15 AC 247 ms
29,912 KB
testcase_16 AC 253 ms
29,908 KB
testcase_17 AC 255 ms
44,144 KB
testcase_18 AC 237 ms
34,392 KB
testcase_19 AC 243 ms
36,548 KB
testcase_20 AC 318 ms
84,656 KB
testcase_21 AC 346 ms
76,420 KB
testcase_22 AC 343 ms
74,596 KB
testcase_23 AC 347 ms
73,628 KB
testcase_24 AC 356 ms
82,428 KB
testcase_25 AC 359 ms
81,268 KB
testcase_26 AC 270 ms
67,140 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 236 ms
20,108 KB
testcase_29 AC 232 ms
20,428 KB
testcase_30 AC 231 ms
19,664 KB
testcase_31 AC 322 ms
91,480 KB
testcase_32 AC 338 ms
105,544 KB
testcase_33 AC 280 ms
67,848 KB
testcase_34 AC 271 ms
38,304 KB
testcase_35 AC 265 ms
38,308 KB
testcase_36 AC 283 ms
68,492 KB
testcase_37 AC 290 ms
49,756 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