結果

問題 No.2020 Sum of Common Prefix Length
ユーザー kwm_tkwm_t
提出日時 2022-07-23 18:07:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 5,368 bytes
コンパイル時間 5,185 ms
コンパイル使用メモリ 283,196 KB
実行使用メモリ 118,736 KB
最終ジャッジ日時 2023-09-18 17:49:02
合計ジャッジ時間 12,558 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 87 ms
12,636 KB
testcase_08 AC 89 ms
12,752 KB
testcase_09 AC 86 ms
12,636 KB
testcase_10 AC 88 ms
12,612 KB
testcase_11 AC 93 ms
12,888 KB
testcase_12 AC 90 ms
12,648 KB
testcase_13 AC 91 ms
12,796 KB
testcase_14 AC 95 ms
12,952 KB
testcase_15 AC 148 ms
32,048 KB
testcase_16 AC 147 ms
32,104 KB
testcase_17 AC 150 ms
51,448 KB
testcase_18 AC 130 ms
39,328 KB
testcase_19 AC 134 ms
42,096 KB
testcase_20 AC 215 ms
111,708 KB
testcase_21 AC 249 ms
101,372 KB
testcase_22 AC 244 ms
93,048 KB
testcase_23 AC 243 ms
91,676 KB
testcase_24 AC 249 ms
110,628 KB
testcase_25 AC 253 ms
109,776 KB
testcase_26 AC 140 ms
69,964 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 119 ms
18,860 KB
testcase_29 AC 119 ms
19,168 KB
testcase_30 AC 119 ms
18,636 KB
testcase_31 AC 207 ms
109,820 KB
testcase_32 AC 210 ms
118,736 KB
testcase_33 AC 156 ms
70,752 KB
testcase_34 AC 153 ms
38,480 KB
testcase_35 AC 150 ms
38,436 KB
testcase_36 AC 175 ms
76,536 KB
testcase_37 AC 174 ms
53,824 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;
}
using  S = long long;
S op(S a, S b) { return a + b; }
S e() { return 0; }
struct HeavyLightDecomposition {
	HeavyLightDecomposition(vector<vector<int>> &_g, vector<S>&_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<S>val;
	segtree<S, op, e>seg;
	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<S>_val(n);
		rep(i, n)_val[i] = val[rev[i]];
		segtree<S, op, e> _seg(_val);
		seg = _seg;
	}
	S query(int u, int v) {
		S ret = e();
		for (;; v = par[head[v]]) {
			if (in[u] > in[v])swap(u, v);
			if (head[u] == head[v])break;
			auto get = seg.prod(in[head[v]], in[v] + 1);
			ret = op(ret, get);
		}
		auto get = seg.prod(in[u], in[v] + 1);
		ret = op(ret, get);
		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, S x) {
		auto get = seg.get(in[u]);
		seg.set(in[u], get + 1);
	}
	void debug() {
		rep(i, n) cout << seg.prod(i, i + 1) << " ";
		cout << endl;
	}
};
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<S>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 {
			S ans = hld.query(0, idx[x[i]][now[x[i]] - 1]);
			cout << ans << endl;
		}
	}
	return 0;
}
0