0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 19:48:51 +00:00

#3733. 「COCI 2015.1」DIVLJAK

https://loj.ac/s/1704216
This commit is contained in:
Baoshuo Ren 2023-02-17 21:23:26 +08:00
parent e2eebd0fa6
commit b3f9c1da5b
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
31 changed files with 274 additions and 0 deletions

184
LibreOJ/3733/3733.cpp Normal file
View File

@ -0,0 +1,184 @@
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5,
M = 2e6 + 5;
int n, q, cnt, root, end[N], c[M];
std::vector<int> g[M];
int id[M], rid[M], fa[M], dep[M], siz[M], son[M], top[M];
struct node {
int val;
int child[26], fail;
node()
: val(0), fail(0) {
std::fill(std::begin(child), std::end(child), 0);
}
} tr[M];
void insert(std::string s, int id) {
int cur = root;
for (char c : s) {
if (!tr[cur].child[c - 'a']) {
tr[cur].child[c - 'a'] = ++cnt;
}
cur = tr[cur].child[c - 'a'];
}
end[id] = cur;
}
void build() {
std::queue<int> q;
for (int i = 0; i < 26; i++) {
if (tr[root].child[i]) {
q.emplace(tr[root].child[i]);
}
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 0; i < 26; i++) {
if (tr[cur].child[i]) {
tr[tr[cur].child[i]].fail = tr[tr[cur].fail].child[i];
q.emplace(tr[cur].child[i]);
} else {
tr[cur].child[i] = tr[tr[cur].fail].child[i];
}
}
}
}
void dfs1(int u, int f) {
fa[u] = f;
dep[u] = dep[f] + 1;
siz[u] = 1;
son[u] = -1;
for (int v : g[u]) {
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if (son[u] == -1 || siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u, int t) {
static int cnt = 0;
rid[id[u] = ++cnt] = u;
top[u] = t;
if (~son[u]) dfs2(son[u], t);
for (int v : g[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
int lca(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
int lowbit(int x) {
return x & -x;
}
void add(int x, int y) {
for (; x <= M; x += lowbit(x)) c[x] += y;
}
int sum(int x) {
int res = 0;
for (; x; x -= lowbit(x)) res += c[x];
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
std::string s;
cin >> s;
insert(s, i);
}
build();
for (int i = 1; i <= cnt; i++) {
g[tr[i].fail].emplace_back(i);
g[i].emplace_back(tr[i].fail);
}
dfs1(0, 0);
dfs2(0, 0);
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
std::string p;
std::vector<int> v;
cin >> p;
int cur = root;
for (char c : p) {
cur = tr[cur].child[c - 'a'];
v.emplace_back(cur);
}
std::sort(v.begin(), v.end(), [&](int x, int y) {
return id[x] < id[y];
});
for (int x : v) {
add(id[x], 1);
}
for (int i = 1; i < v.size(); i++) {
add(id[lca(v[i - 1], v[i])], -1);
}
} else { // op == 2
int x;
cin >> x;
cout << sum(id[end[x]] + siz[end[x]] - 1) - sum(id[end[x]] - 1) << endl;
}
}
return 0;
}

BIN
LibreOJ/3733/data/divljak1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak10a.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak10a.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak10b.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak10b.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak6a.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak6a.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak6b.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak6b.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak7a.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak7a.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak7b.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak7b.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak8a.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak8a.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak8b.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak8b.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak9a.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak9a.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak9b.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3733/data/divljak9b.out (Stored with Git LFS) Normal file

Binary file not shown.