bzoj4545
lct+SAM
bzoj4516+bzoj2555
这道题唯一的用处就是教会了我真正的广义SAM
dfs时保留当前节点在后缀自动机中的位置,每个点接着父亲建
lct动态维护right集合大小,用lct维护子树就行了
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + ;
struct edge {
int nxt, to, w;
} e[N << ];
int n, m, cnt = ;
int h[N], pos[N];
char s[N];
long long sum;
namespace lct
{
struct node {
int ch[];
int f, tag, reg;
} t[N << ];
bool isr(int x) {
return !t[x].f || (t[t[x].f].ch[] != x && t[t[x].f].ch[] != x);
}
int wh(int x) {
return x == t[t[x].f].ch[];
}
void paint(int x, int d) {
t[x].tag += d;
t[x].reg += d;
}
void pushdown(int x) {
if(t[x].tag) {
paint(t[x].ch[], t[x].tag);
paint(t[x].ch[], t[x].tag);
t[x].tag = ;
}
}
void pd(int x) {
if(!isr(x)) {
pd(t[x].f);
}
pushdown(x);
}
void rotate(int x) {
int y = t[x].f, z = t[y].f, w = wh(x);
if(!isr(y)) {
t[z].ch[wh(y)] = x;
}
t[x].f = z;
t[y].ch[w] = t[x].ch[w ^ ];
t[t[x].ch[w ^ ]].f = y;
t[y].f = x;
t[x].ch[w ^ ] = y;
}
void splay(int x) {
pd(x);
for(; !isr(x); rotate(x)) {
if(!isr(t[x].f)) {
rotate(wh(t[x].f) == wh(x) ? t[x].f : x);
}
}
}
void access(int x) {
for(int y = ; x; y = x, x = t[x].f) {
splay(x);
t[x].ch[] = y;
}
}
void link(int u, int v) {
access(u);
splay(u);
access(v);
splay(v);
paint(v, t[u].reg);
t[u].f = v;
}
void cut(int u, int v) {
access(u);
splay(u);
t[u].ch[] = ;
t[v].f = ;
paint(v, -t[u].reg);
}
}
namespace SAM
{
struct node {
int ch[];
int par, val;
} t[N << ];
int root = , sz = ;
int nw(int x) {
t[++sz].val = x;
return sz;
}
int extend(int last, int c) {
int p = last, np = nw(t[p].val + );
lct::t[np].reg = ;
while(p && !t[p].ch[c]) {
t[p].ch[c] = np;
p = t[p].par;
}
if(!p) {
t[np].par = root;
lct::link(np, root);
} else {
int q = t[p].ch[c];
if(t[q].val == t[p].val + ) {
t[np].par = q;
lct::link(np, q);
} else {
int nq = nw(t[p].val + );
lct::link(nq, t[q].par);
lct::cut(q, t[q].par);
lct::link(np, nq);
lct::link(q, nq);
t[nq].par = t[q].par;
t[np].par = t[q].par = nq;
memcpy(t[nq].ch, t[q].ch, sizeof(t[q].ch));
while(p && t[p].ch[c] == q) {
t[p].ch[c] = nq;
p = t[p].par;
}
}
}
sum += t[np].val - t[t[np].par].val;
return np;
}
void solve(char *s) {
int len = strlen(s), now = root;
for(int i = ; i < len; ++i) {
if(!t[now].ch[s[i] - 'a']) {
puts("");
return;
}
now = t[now].ch[s[i] - 'a'];
}
lct::pd(now);
printf("%d\n", lct::t[now].reg);
lct::splay(now);
}
}
void link(int u, int v, int w) {
e[++cnt].nxt = h[u];
h[u] = cnt;
e[cnt].to = v;
e[cnt].w = w;
}
void dfs(int u, int last) {
for(int i = h[u]; i; i = e[i].nxt) {
if(e[i].to == last) {
continue;
}
pos[e[i].to] = SAM::extend(pos[u], e[i].w);
dfs(e[i].to, u);
}
h[u] = ;
}
int main() {
// freopen("1.out", "w", stdout);
int laji;
scanf("%d%d", &laji, &n);
for(int i = ; i < n; ++i) {
int u, v;
scanf("%d%d%s", &u, &v, s);
link(u, v, s[] - 'a');
link(v, u, s[] - 'a');
}
pos[] = ;
dfs(, );
scanf("%d", &m);
while(m--) {
int opt;
scanf("%d", &opt);
if(opt == ) {
printf("%lld\n", sum);
} else if(opt == ) {
int rt, sz;
scanf("%d%d", &rt, &sz);
while(--sz) {
int u, v;
scanf("%d%d%s", &u, &v, s);
link(u, v, s[] - 'a');
link(v, u, s[] - 'a');
}
dfs(rt, );
} else {
scanf("%s", s);
SAM::solve(s);
}
}
return ;
}
bzoj4545的更多相关文章
- 【BZOJ4545】DQS的trie 后缀自动机+LCT
[BZOJ4545]DQS的trie Description DQS的自家阳台上种着一棵颗粒饱满.颜色纯正的trie. DQS的trie非常的奇特,它初始有n0个节点,n0-1条边,每条边上有一个字符 ...
- BZOJ4545: DQS的trie
BZOJ4545: DQS的trie https://lydsy.com/JudgeOnline/problem.php?id=4545 分析: 对trie用dfs建sam复杂度是\(O(n^2)\) ...
- BZOJ4545: DQS的trie 广义后缀自动机_LCT
特别鸣神犇 fcwww 替我调出了无数个错误(没他的话我都快自闭了),祝大佬省选rp++ 板子题,给我写了一天QAQ...... 用 LCT 维护后缀树,暴力更新用 LCT 区间更新链即可 其实,在计 ...
随机推荐
- 我的Android进阶之旅------>Android关于ImageSpan和SpannableString的初步了解
最近要实现一个类似QQ聊天输入框,在输入框中可以同时输入文字和表情图像的功能.如下图所示的效果: 为了实现这个效果,先去了解了一下ImageSpan和SpannableString的用法.下面用一个小 ...
- You are using pip version 8.1.2, however version 9.0.1 is available.
[root@localhost ~]# pip install virtualenvmapperCollecting virtualenvmapper Could not find a versio ...
- VOFM 例程
SAP ERP 实施中,经常会用到例程开发(TCODE:VOFM).这个开发目前我用到的是影响SD和MM的定价过程.创建例程需要ACCESS KEY,这个可以通过申请得到,创建后例程会被包含在一个RE ...
- nc传文件
nc传文件 先启动接收方 nc -l -p 9999 > index.lua 后启动发送方 nc 192.168.1.1 9999 < index.lua
- 从硬盘设计思想到RAID改良之道
监控硬盘的前生今世关于桌面硬盘.企业级近线硬盘(NL-SAS/SATA)和监控硬盘的差别,我们在前文中已经讲得很详细,这里再换一个角度来看看. "监控硬盘是希捷和西数为视频监控定制的,典型的 ...
- ubuntu14.04允许root远程链接、修改主机名
1.设置root密码 sudo passwd root 2.修改主机名 第一步:ubuntu主机名位于/etc/hostname里,将其修改为自己需要的名称. 第二步:修改/etc/hosts文件,将 ...
- iOS UITableView滚动头图 拉伸放大效果 (头部弹性效果) 增加iOS11支持 附有demo
今天修改日期为2017年11月25日 两个月前做了iOS11的bug修复,才对博客进行更新,见谅. 在iOS11上需要注意两个问题 1.使用UIScrollview,UITableView,UIWeb ...
- Android 双u盘方案【转】
本文转载自:https://blog.csdn.net/kc58236582/article/details/49618445 1 L1813系统上双U盘设计方案——系统设计 1.1 ...
- MySQL服务器调优思路
1.mysqladmin -uroot ext|awk '/Queries/{q=$4}/Threads_connected/{c=$4}/Threads_running/{r=$4}END{prin ...
- org.eclipse.core.resources.bak文件导致MyEclipse每次关闭时无法保存文件
MyEclipse关闭时提示如下信息 Problems occurred while trying to save the state of the workbench. Internal Error ...