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 区间更新链即可 其实,在计 ...
随机推荐
- 洛谷P3943 星空
洛谷P3943 星空 题目背景 命运偷走如果只留下结果, 时间偷走初衷只留下了苦衷. 你来过,然后你走后,只留下星空. 题目描述 逃不掉的那一天还是来了,小 F 看着夜空发呆. 天上空荡荡的,没有一颗 ...
- linux c编程:Posix消息队列
Posix消息队列可以认为是一个消息链表. 有足够写权限的线程可以往队列中放置消息, 有足够读权限的线程可以从队列中取走消息 在某个进程往一个队列写入消息前, 并不需要另外某个进程在该队列上等待消息的 ...
- 遇到IIS configuration error错误的可以看看,不一定是权限问题
最近接手了别人的一个 DOT NET项目,编译.调试一切都OK(心里暗暗高兴),发布吧,结果放到服务器上一运行出现Configuration Error错误,提示:“Access to the pat ...
- Python赋值魔法技巧
实验环境: [root@localhost ~]# python -V Python 2.7.5 1.序列解包 多个赋值操作可以同时进行 >>> x,y,z = 1,2,3 > ...
- Yii2 关于电子商务的开源项目
https://github.com/samdark/yii2-shop https://github.com/omnilight/yii2-shopping-cart https://github. ...
- 手机端网页web开发要点
1.初始化 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- Spiral Matrix螺旋遍历矩阵
假定有: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 这样一个数组矩阵,现要求对其进行顺时针方向螺旋形从外至内地遍历,即输出: [1,2,3,6,9,8,7,4 ...
- 在webBrowser中取Cookie的方法
在很多情况下我们会使用间进程的webBrowser去实现一些网页的请求和抓去,这个时候有部分网页是取不到Cookie的,那怎么办呢?下面我提供一个方法,应该99%的都能取到, //取当前webBrow ...
- LINQ 学习路程 -- 开篇
Enumerable: Queryable:
- sqlite3简单教程整理
一.Ubuntu下安装sqlite3 1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装: 要安装sqlite3,可以在终端提示符后运行下列命令: sud ...