結果

問題 No.2020 Sum of Common Prefix Length
ユーザー 👑 potato167potato167
提出日時 2022-07-22 22:21:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 3,381 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 211,188 KB
実行使用メモリ 75,840 KB
最終ジャッジ日時 2023-09-17 10:45:58
合計ジャッジ時間 9,332 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 65 ms
7,540 KB
testcase_08 AC 67 ms
7,680 KB
testcase_09 AC 67 ms
7,484 KB
testcase_10 AC 65 ms
7,624 KB
testcase_11 AC 67 ms
7,508 KB
testcase_12 AC 66 ms
7,668 KB
testcase_13 AC 68 ms
7,480 KB
testcase_14 AC 68 ms
7,480 KB
testcase_15 AC 107 ms
17,252 KB
testcase_16 AC 107 ms
17,236 KB
testcase_17 AC 194 ms
32,196 KB
testcase_18 AC 167 ms
24,572 KB
testcase_19 AC 168 ms
25,480 KB
testcase_20 AC 211 ms
74,984 KB
testcase_21 AC 224 ms
75,208 KB
testcase_22 AC 226 ms
58,336 KB
testcase_23 AC 213 ms
57,440 KB
testcase_24 AC 223 ms
75,240 KB
testcase_25 AC 242 ms
75,840 KB
testcase_26 AC 158 ms
39,112 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 87 ms
10,592 KB
testcase_29 AC 90 ms
11,164 KB
testcase_30 AC 88 ms
10,448 KB
testcase_31 AC 195 ms
74,172 KB
testcase_32 AC 200 ms
75,068 KB
testcase_33 AC 173 ms
40,300 KB
testcase_34 AC 103 ms
20,672 KB
testcase_35 AC 171 ms
21,080 KB
testcase_36 AC 174 ms
45,740 KB
testcase_37 AC 180 ms
29,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


namespace po167{
template<int char_size,int base>
struct Trie_Tree
{
	struct Node{
		std::vector<int> next_node;
		std::vector<int> accept_node;
		int parent_node;
		int char_number;
		int cou;
		Node(int c_):next_node(char_size),char_number(c_),parent_node(-1),cou(0){
			for(int i=0;i<char_size;i++) next_node[i]=-1;
		}
	};
	std::vector<Node> nodes;
	Trie_Tree(){
		nodes.push_back(Node(-1));
	}
	vector<int> insert(std::string &word,int word_id){
		int node_id=0;
		vector<int> ids;
		for(int i=0;i<(int)word.size();i++){
			int c=(int)(word[i]-base);
			int next_id=nodes[node_id].next_node[c];
			if(next_id==-1){
				next_id=(int)nodes.size();
				nodes.push_back(Node(c));
			}
			nodes[next_id].cou++;
			nodes[next_id].parent_node=node_id;
			nodes[node_id].next_node[c]=next_id;
			nodes[next_id].accept_node.push_back(word_id);
			node_id=next_id;
			ids.push_back(node_id);
		}
		return ids;
	}
	int min_insert(char ch,int node_id,int word_id){
		int c=(int)(ch-base);
		int next_id=nodes[node_id].next_node[c];
		if(next_id==-1){
			next_id=(int)nodes.size();
			nodes.push_back(Node(c));
		}
		nodes[next_id].cou++;
		nodes[next_id].parent_node=node_id;
		nodes[node_id].next_node[c]=next_id;
		nodes[next_id].accept_node.push_back(word_id);
		node_id=next_id;
		return node_id;
	}
};
}
using po167::Trie_Tree;




void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N;
	cin>>N;
	string S;
	vector<vector<int>> ids(N);
	po167::Trie_Tree<26,(int)('a')> T;
	rep(i,N){
		string S;
		cin>>S;
		ids[i]=T.insert(S,i);
		//vec_out(ids[i]);
	}
	//cout<<endl;
	int Q;
	cin>>Q;
	vector<ll> base(N);
	int B=600;
	rep(i,N){
		for(int j=B;j<(int)(ids[i].size());j++){
			base[i]+=(int)(T.nodes[ids[i][j]].accept_node.size());
		}
	}
	rep(i,Q){
		int t,ind;
		cin>>t>>ind;
		ind--;
		if(t==1){
			char c;
			cin>>c;
			int tmp=T.min_insert(c,(*ids[ind].rbegin()),ind);
			if((int)(ids[ind].size())>=B){
				base[ind]--;
				for(auto x:T.nodes[tmp].accept_node) base[x]++,base[ind]++;
			}
			ids[ind].push_back(tmp);
		}else{
			ll ans=base[ind];
			rep(j,min((int)(ids[ind].size()),B)) ans+=(ll)(T.nodes[ids[ind][j]].accept_node.size());
			cout<<ans<<"\n";
		}
	}
}

0