結果

問題 No.2020 Sum of Common Prefix Length
ユーザー woodywoodywoodywoody
提出日時 2022-07-23 18:27:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 6,974 bytes
コンパイル時間 4,129 ms
コンパイル使用メモリ 237,628 KB
実行使用メモリ 80,440 KB
最終ジャッジ日時 2023-09-18 18:23:51
合計ジャッジ時間 12,130 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 138 ms
11,664 KB
testcase_08 AC 138 ms
11,568 KB
testcase_09 AC 140 ms
11,552 KB
testcase_10 AC 138 ms
11,668 KB
testcase_11 AC 140 ms
11,584 KB
testcase_12 AC 142 ms
11,652 KB
testcase_13 AC 140 ms
11,600 KB
testcase_14 AC 142 ms
11,664 KB
testcase_15 AC 197 ms
31,816 KB
testcase_16 AC 194 ms
31,536 KB
testcase_17 AC 157 ms
35,860 KB
testcase_18 AC 145 ms
27,592 KB
testcase_19 AC 148 ms
29,964 KB
testcase_20 AC 200 ms
71,792 KB
testcase_21 AC 227 ms
65,936 KB
testcase_22 AC 221 ms
62,236 KB
testcase_23 AC 214 ms
61,392 KB
testcase_24 AC 235 ms
72,068 KB
testcase_25 AC 235 ms
69,924 KB
testcase_26 AC 144 ms
50,244 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 165 ms
18,416 KB
testcase_29 AC 172 ms
18,816 KB
testcase_30 AC 165 ms
17,716 KB
testcase_31 AC 191 ms
72,036 KB
testcase_32 AC 197 ms
80,440 KB
testcase_33 AC 158 ms
50,628 KB
testcase_34 AC 192 ms
33,440 KB
testcase_35 AC 176 ms
33,396 KB
testcase_36 AC 176 ms
57,332 KB
testcase_37 AC 190 ms
43,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,b) for(int i=0;i<b;i++)
#define rrep(i,b) for(int i=b-1;i>=0;i--)
#define rep1(i,b) for(int i=1;i<b;i++)
#define repx(i,x,b) for(int i=x;i<b;i++)
#define rrepx(i,x,b) for(int i=b-1;i>=x;i--)
#define fore(i,a) for(auto i:a)
#define fore1(i,a) for(auto &i:a)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define fi first
#define se second

using namespace std;
using namespace atcoder;

using ll = long long;
template<typename T> using mpq = priority_queue<T, vector<T>, greater<T>>;
template<typename T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T> ll sumv(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
bool yn(bool a) { if(a) {cout << "Yes" << endl; return 1;} else {cout << "No" << endl; return 0;}}
#define dame { cout << "No" << endl; return 0;}
#define dame1 { cout << -1 << endl; return 0;}
#define test(x) cout << "test" << x << endl;
#define deb(x,y) cout << x << " " << y << endl;
#define debp(p) cout << p.fi << " " << p.se << endl;
#define deb3(x,y,z) cout << x << " " << y << " " << z << endl;
#define deb4(x,y,z,x2) cout << x << " " << y << " " << z << " " << x2 << endl;
#define out cout << ans << endl;
#define outv fore(yans , ans) cout << yans << "\n";
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define showv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;}
#define showv2(v) fore(vy , v) cout << vy << "\n";
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using pii = pair<int,int>;
using tp = tuple<int ,int ,int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;
using vpil = vector<pil>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using vvpii = vector<vector<pii>>;
using vvpli = vector<vector<pli>>;
using vvpll = vector<vpll>;
using vvpil = vector<vpil>;
using mint = modint998244353;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
vector<int> dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1};
ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

template <int char_size, int base>
struct Trie {
    struct Node {            // 頂点を表す構造体
        vector<int> next;    // 子の頂点番号を格納。存在しなければ-1
        vector<int> accept;  // 末端がこの頂点になる単語の word_id を保存
        int c;               // base からの間隔をint型で表現したもの
        int common;          // いくつの単語がこの頂点を共有しているか
        Node(int c_) : c(c_), common(0) {
            next.assign(char_size, -1);
        }
    };
    
    vector<Node> nodes;  // trie 木本体
    int root;
    Trie() : root(0) {
        nodes.push_back(Node(root));
    }

    // 単語の挿入
    vi insert(const string &word, int word_id) {
        int node_id = 0;
        vi ret;

        for (int i = 0; i < (int)word.size(); i++) {
            int c = (int)(word[i] - base);
            int &next_id = nodes[node_id].next[c];
            if (next_id == -1) {  // 次の頂点が存在しなければ追加
                next_id = (int)nodes.size();
                nodes.push_back(Node(c));
            }
            ret.pb(next_id);
            ++nodes[node_id].common;
            node_id = next_id;
        }
        ++nodes[node_id].common;
        nodes[node_id].accept.push_back(word_id);

        return ret;
    }

    vi insert(const string &word) {
        vi ret = insert(word, nodes[0].common);

        return ret;
    }

    // 単語とprefixの検索
    bool search(const string &word, bool prefix = false) {
        int node_id = 0;
        for (int i = 0; i < (int)word.size(); i++) {
            int c = (int)(word[i] - base);
            int &next_id = nodes[node_id].next[c];
            if (next_id == -1) {  // 次の頂点が存在しなければ終了
                return false;
            }
            node_id = next_id;
        }
        return (prefix) ? true : nodes[node_id].accept.size() > 0;
    }

    // prefix を持つ単語が存在するかの検索
    bool start_with(const string &prefix) {
        return search(prefix, true);
    }

    // 挿入した単語の数
    int count() const {
        return (nodes[0].common);
    }
    // Trie木のノード数
    int size() const {
        return ((int)nodes.size());
    }
};Trie<26 , 'a'> trie;

/* Trie 木: 文字の種類(char_size)、int型で0に対応する文字(base)
    insert(word): 単語 word を Trie 木に挿入する
    search(word): 単語 word が Trie 木にあるか判定する
    start_with(prefix):  prefix が一致する単語が Trie 木にあるか判定する
    count(): 挿入した単語の数を返す
    size(): Trie 木の頂点数を返す
    計算量:insert, search ともに O(M)(Mは単語の長さ)
*/

vi v,in,ou;


void dfs(int x,int pre){
    in[x] = sz(v);
    v.pb(0);

    rep(i,26){
        int y = trie.nodes[x].next[i];
        if (y == -1 || y == pre) continue;

        dfs(y , x);
    }

    ou[x] = sz(v);
    v.pb(0);

    return;
}

int main(){
    int n; cin>>n;

    vs s(n),s2(n);
    rep(i,n) {cin>>s[i]; s2[i] = s[i];}

    int q; cin>>q;
    vi t(q),x(q),c(q);

    rep(i,q){
        cin>>t[i]>>x[i];
        x[i]--;

        if (t[i] == 1){
            char cc;cin>>cc;
            c[i] = cc - 'a';
            s2[x[i]] += cc;
        }

    }

    vvi sv(n);

    rep(i,n){
        sv[i] = trie.insert(s2[i]); 
    }

    int m = sz(trie);

    in.resize(m);
    ou.resize(m);

    dfs(0 , -1);

    fenwick_tree<ll> fw(sz(v));

    vi cnt(n);

    rep(i,n){
        cnt[i] = sz(s[i]);
        rep(j,cnt[i]){
            fw.add(in[sv[i][j]] , 1);
            fw.add(ou[sv[i][j]] , -1);
        }
    }

    // rep(i,sz(v)){
    //     show(i);
    //     cout << fw.sum(i , i+1) << endl;
    // }

    // rep(i,m){
    //     show(i);
    //     deb(in[i] , ou[i]);
    // }

    // rep(i,n){
    //     show(i);
    //     cout << s2[i] << endl;
    //     showv(sv[i]);
    // }

    vl ans;

    rep(i,q){
        if (t[i] == 1){
            fw.add(in[sv[x[i]][cnt[x[i]]]] , 1);
            fw.add(ou[sv[x[i]][cnt[x[i]]]] , -1);
            cnt[x[i]]++;
        }else{
            ll tmp = fw.sum(0 , in[sv[x[i]][cnt[x[i]]-1]]+1);
            ans.pb(tmp);
        }
    }

    outv;


// 消せ!!!!!! #define _GLIBCXX_DEBUG
// priority_queue でしぬ

    return 0;
}
0