結果
| 問題 |
No.2020 Sum of Common Prefix Length
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2022-07-22 23:17:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 353 ms / 2,000 ms |
| コード長 | 7,338 bytes |
| コンパイル時間 | 1,968 ms |
| コンパイル使用メモリ | 143,376 KB |
| 最終ジャッジ日時 | 2025-01-30 13:03:56 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 38 |
ソースコード
#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];
}
}
}
milanis48663220