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 区间更新链即可 其实,在计 ...
随机推荐
- Redis 配置和使用
Redis的配置 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者me ...
- redis持久化RDB详细操作步骤
1.xshell远程登录服务器ssh root@192.168.142.130 2.切换到redis目录 3.创建一个配置文件s2-redis.conf 4.编辑文件 vi s2-redis.conf ...
- C语言程序设计50例(经典收藏)之1
题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. #includ ...
- C#聚合运算方法
Aggregate 对集合值执行自定义聚合运算 Average 计算集合平均值 Count 对集合的元素惊醒计数,还可以仅对满足某一谓词函数的元素进行计数 LongCount 对大型集合中的元素进行计 ...
- wap网站即手机端网页SEO优化注意事项及方法
定位和页面设计: 无论是PC端还是移动端,网站 都要考虑清楚消费群体的定位问题.虽然智能手机用户数量非常普及,但是要明白中国的大部分手机用户使用的还是2G网络,一直高 喊的3G.4G手机用户只有大约1 ...
- C#调用大漠插件
大漠插件是一个很不错的东西,在按键精灵和易语言里面用得很多,可以后台找图找字,写游戏自动脚本用得特别多.前面写一个微信的自动脚本,查了一些资料,易语言不太熟悉,按键精灵功能上可能不好实现,就找了些资料 ...
- LintCode:链表操作(合并与反转)
描述: (1)翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null ********************** ...
- hd acm1017
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- etcd -> Highly-avaliable key value store for shared configuration and service discovery
The name "etcd" originated from two ideas, the unix "/etc" folder and "d&qu ...
- BestCoder Round #4 之 Miaomiao's Geometry(2014/8/10)
最后收到邮件说注意小数的问题!此代码并没有过所有数据,请读者参考算法, 自己再去修改一下吧!注意小数问题! Miaomiao's Geometry Time Limit: 2000/1000 MS ( ...