結果

問題 No.2020 Sum of Common Prefix Length
ユーザー milanis48663220milanis48663220
提出日時 2022-07-22 23:08:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,360 bytes
コンパイル時間 2,498 ms
コンパイル使用メモリ 148,596 KB
実行使用メモリ 106,588 KB
最終ジャッジ日時 2023-09-17 11:57:23
合計ジャッジ時間 14,421 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 209 ms
16,172 KB
testcase_08 AC 209 ms
16,344 KB
testcase_09 AC 215 ms
16,204 KB
testcase_10 AC 209 ms
16,076 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 262 ms
34,756 KB
testcase_16 AC 262 ms
34,808 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 269 ms
68,988 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 236 ms
22,448 KB
testcase_29 AC 241 ms
23,080 KB
testcase_30 AC 235 ms
22,088 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 278 ms
67,852 KB
testcase_34 AC 280 ms
40,904 KB
testcase_35 AC 275 ms
40,920 KB
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

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){
        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);
    vector<int> 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<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, 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];
        }
    }
}
0