結果

問題 No.2020 Sum of Common Prefix Length
ユーザー kwm_tkwm_t
提出日時 2022-07-23 18:13:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 5,211 bytes
コンパイル時間 5,167 ms
コンパイル使用メモリ 286,116 KB
実行使用メモリ 105,588 KB
最終ジャッジ日時 2024-07-05 07:58:07
合計ジャッジ時間 10,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 73 ms
12,668 KB
testcase_08 AC 70 ms
12,672 KB
testcase_09 AC 71 ms
12,672 KB
testcase_10 AC 73 ms
12,672 KB
testcase_11 AC 72 ms
12,928 KB
testcase_12 AC 69 ms
12,800 KB
testcase_13 AC 68 ms
12,800 KB
testcase_14 AC 71 ms
13,056 KB
testcase_15 AC 117 ms
32,044 KB
testcase_16 AC 115 ms
32,172 KB
testcase_17 AC 122 ms
49,512 KB
testcase_18 AC 101 ms
36,672 KB
testcase_19 AC 105 ms
39,516 KB
testcase_20 AC 192 ms
99,344 KB
testcase_21 AC 215 ms
88,948 KB
testcase_22 AC 210 ms
88,952 KB
testcase_23 AC 207 ms
87,388 KB
testcase_24 AC 218 ms
97,776 KB
testcase_25 AC 223 ms
96,788 KB
testcase_26 AC 122 ms
63,944 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 90 ms
19,040 KB
testcase_29 AC 93 ms
19,492 KB
testcase_30 AC 89 ms
18,488 KB
testcase_31 AC 184 ms
96,688 KB
testcase_32 AC 193 ms
105,588 KB
testcase_33 AC 131 ms
63,680 KB
testcase_34 AC 118 ms
37,244 KB
testcase_35 AC 116 ms
37,248 KB
testcase_36 AC 146 ms
70,388 KB
testcase_37 AC 139 ms
51,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
struct trie {
	int kind;
	vector<int>node;
	vector<vector<int>>next;
	vector<bool>end;
	trie(int _kind) :kind(_kind) {
		node.resize(1);
		end.resize(1);
		next.emplace_back(vector<int>(kind, -1));
	}
	void insert(const vector<int>&v) {
		int now = 0;
		rep(i, v.size()) {
			if (-1 == next[now][v[i]]) {
				next[now][v[i]] = node.size();
				node.emplace_back(0);
				end.push_back(false);
				next.emplace_back(vector<int>(kind, -1));
			}
			now = next[now][v[i]];
			node[now]++;
		}
		end[now] = true;
	}
	bool seach(const vector<int>&v) {
		int now = 0;
		rep(i, v.size()) {
			if (-1 == next[now][v[i]])return false;
			if (node[next[now][v[i]]] <= 0)return false;
			now = next[now][v[i]];
		}
		return end[now];
	}
	void erase(const vector<int>&v) {
		int now = 0;
		rep(i, v.size()) {
			now = next[now][v[i]];
			node[now]--;
		}
		end[now] = false;
	}
	// prefixも取得する
	vector<int> getIndexEx(const vector<int>&v) {
		vector<int>ret;
		int idx = 0, now = 0;
		rep(i, v.size()) {
			if (0 != now) {
				idx += node[now];
				rep(j, 2)if (-1 != next[now][j])idx -= node[next[now][j]];
			}
			if (0 == v[i]) {
				if (-1 == next[now][v[i]])break;
				now = next[now][v[i]];
			}
			else {
				if (-1 != next[now][0]) idx += node[next[now][0]];
				if (-1 == next[now][v[i]])break;
				now = next[now][v[i]];
			}
			ret.push_back(now);
		}
		return ret;
	}
};
vector<int> trans(string s) {
	vector<int>ret(s.size());
	rep(i, s.size()) ret[i] = s[i] - 'a';
	return ret;
}
struct HeavyLightDecomposition {
	HeavyLightDecomposition(vector<vector<int>> &_g, vector<long long>&_val) :g(_g), val(_val) {
		n = g.size();
		sz.resize(n);
		par.resize(n);
		in.resize(n);
		out.resize(n);
		rev.resize(n);
		head.resize(n);
	}
	int n;
	vector<vector<int>>g;
	vector<int>sz, par, in, out, rev, head;
	vector<long long>val;
	fenwick_tree<long long> fw;
	void build() {
		dfs_sz(0);
		int time = 0;
		dfs_hld(0, time);
		initSegmentTree();
	}
	void dfs_sz(int v, int p = -1) {
		par[v] = p;
		sz[v] = 1;
		if (g[v].size() && (p == g[v][0]))swap(g[v][0], g[v].back());
		for (auto &e : g[v]) {
			if (p == e)continue;
			dfs_sz(e, v);
			sz[v] += sz[e];
			if (sz[g[v][0]] < sz[e])swap(g[v][0], e);
		}
	}
	void dfs_hld(int v, int &time, int p = -1) {
		in[v] = time;
		time++;
		rev[in[v]] = v;
		for (auto &e : g[v]) {
			if (p == e)continue;
			if (e == g[v][0])head[e] = head[v];
			else head[e] = e;
			dfs_hld(e, time, v);
		}
		out[v] = time;
	}
	void initSegmentTree() {
		vector<long long>_val(n);
		rep(i, n)_val[i] = val[rev[i]];
		fenwick_tree<long long> _fw(n);
		rep(i, n)_fw.add(i, _val[i]);
		fw = _fw;
	}
	long long query(int u, int v) {
		long long ret = 0;
		for (;; v = par[head[v]]) {
			if (in[u] > in[v])swap(u, v);
			if (head[u] == head[v])break;
			ret += fw.sum(in[head[v]], in[v] + 1);
		}
		ret+= fw.sum(in[u], in[v] + 1);
		return ret;
	}
	int la(int v, int k) {
		while (1) {
			int u = head[v];
			if (in[v] - k > in[u])return rev[in[v] - k];
			k -= in[v] - in[u] + 1;
			v = par[u];
		}
	}
	int lca(int u, int v) {
		for (;; v = par[head[v]]) {
			if (in[u] > in[v])swap(u, v);
			if (head[u] == head[v])return u;
		}
	}
	// point update
	void update_point(int u, int x) {
		fw.add(in[u], x);
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<string>s(n);
	rep(i, n)cin >> s[i];
	int q; cin >> q;
	vector<int>t(q), x(q);
	vector<char>c(q);
	vector<string>s2 = s;
	rep(i, q) {
		cin >> t[i] >> x[i], x[i]--;
		if (1 == t[i]) {
			cin >> c[i];
			s2[x[i]] += c[i];
		}
	}
	trie tr(26);
	rep(i, n)tr.insert(trans(s2[i]));
	vector<vector<int>>idx(n);
	rep(i, n) idx[i] = tr.getIndexEx(trans(s2[i]));
	int sz = 0;
	rep(i, n) chmax(sz, idx[i].back() + 1);
	vector<long long>v(sz);
	rep(i, n)rep(j, s[i].size()) v[idx[i][j]]++;
	vector<vector<int>>g(sz);
	rep(i, n) rep(j, idx[i].size()) {
		int u = 0, v = 0;
		if (0 != j)u = idx[i][j - 1];
		v = idx[i][j];
		g[u].push_back(v);
		g[v].push_back(u);
	}
	rep(i, g.size()) {
		sort(all(g[i]));
		g[i].erase(unique(all(g[i])), g[i].end());
	}
	HeavyLightDecomposition hld(g, v);
	hld.build();
	vector<int>now(n);
	rep(i, n)now[i] = s[i].size();
	rep(i, q) {
		if (1 == t[i]) {
			hld.update_point(idx[x[i]][now[x[i]]], 1);
			now[x[i]]++;
		}
		else {
			long long ans = hld.query(0, idx[x[i]][now[x[i]] - 1]);
			cout << ans << endl;
		}
	}
	return 0;
}
0