BZOJ 4545
bzoj 4545
给定一个踹树,支持几种操作。
- 本质不同子串询问
- 加入子树
- 询问字符串\(S\) 在树上的出现次数。
好码好码
重点就是维护\(parent\) 树,考虑用\(LCT\)维护此树。
第三问就是匹配点的\(right\)集合大小,算一算就可以了。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int read () {
int q=0,f=1;char ch=getchar();
while(!isdigit(ch)) {
if(ch=='-')f=-1;ch=getchar();
}
while(isdigit(ch)){
q=q*10+ch-'0';ch=getchar();
}
return q*f;
}
long long ans;
int opt,x,y,n,q;
struct LCT {
int son[MAXN][2];
int fa[MAXN];
int v[MAXN];
int tag[MAXN];
int rev[MAXN];
int getson(int x) {
return son[fa[x]][1] == x;
}
int isroot(int x) {
return son[fa[x]][0] != x and son[fa[x]][1] != x;
}
void down(int now) {
swap(son[now][0],son[now][1]);
rev[now] ^= 1;
}
void Add_val(int x,int k) {
if(x) {
v[x] += k;
tag[x] += k;
}
}
void pushdown(int now) {
if(rev[now]) {
down(son[now][0]);
down(son[now][1]);
rev[now] = 0;
}
if(tag[now]) {
Add_val(son[now][0],tag[now]);
Add_val(son[now][1],tag[now]);
tag[now] = 0;
}
}
void dfs(int now) {
if(!isroot(now)) {
dfs(fa[now]);
}
pushdown(now);
}
void rotate(int now) {
int y = fa[now];
int z = fa[y];
int wht = getson(now);
fa[now] = z;
if(!isroot(y)) {
son[z][y == son[z][1]] = now;
}
fa[son[now][wht ^ 1]] = y;
son[y][wht] = son[now][wht ^ 1];
fa[y] = now;
son[now][wht ^ 1] = y;
}
void splay(int now) {
dfs(now);
for(int i = fa[now]; !isroot(now) ; rotate(now),i = fa[now]) {
if(!isroot(i)) {
rotate(getson(now) == getson(i) ? i : now);
}
}
}
void access(int now) {
for(int i = 0;now;i = now,now = fa[now]) {
splay(now);
son[now][1] = i;
}
}
void makeroot(int now) {
access(now);
splay(now);
down(now);
}
void link(int x,int y) {
makeroot(x);
fa[x] = y;
}
void cut(int x,int y) {
makeroot(x);
access(y);
splay(y);
son[y][0] = fa[x] = 0;
}
void Add(int x,int y,int k) {
makeroot(x);
access(y);
splay(y);
Add_val(y,k);
}
int query(int now) {
splay(now);
return v[now];
}
}lct;
struct SAM {
int ch[MAXN][3];
int len[MAXN];
int fail[MAXN];
int Cnt;
SAM() {
Cnt = 1;
}
int Copy(int c) {
int now = ++Cnt;
for(int i = 0;i <= 2; ++i) {
ch[now][i] = ch[c][i];
}
return now;
}
void link(int x,int y) {
fail[x] = y;
lct.link(x,y);
ans += len[x] - len[y];
}
int work(int p,int c) {
int q = ch[p][c];
int nq = Copy(q);
len[nq] = len[p] + 1;
lct.v[nq] = lct.query(q);
lct.cut(q,fail[q]);
ans -= len[q] - len[fail[q]];
link(nq,fail[q]);
link(q,nq);
for( ; ch[p][c] == q ; p = fail[p]) {
ch[p][c] = nq;
}
return nq;
}
int insert(int p,int c) {
int cur;
if(ch[p][c]) {
cur = len[ch[p][c]] == len[p] + 1 ? ch[p][c] : work(p,c);
}
else {
cur = ++Cnt;
len[cur] = len[p] + 1;
for( ; p and !ch[p][c] ; p = fail[p]) {
ch[p][c] = cur;
}
if(!p) {
link(cur,1);
}
else if(len[ch[p][c]] == len[p] + 1) {
link(cur,ch[p][c]);
}
else {
link(cur,work(p,c));
}
}
lct.Add(cur,1,1);
return cur;
}
}sam;
struct graph {
int head[MAXN];
int cnt;
graph () {
cnt = 0;
}
int ver[MAXN];
int nxt[MAXN];
int val[MAXN];
int vis[MAXN];
int pos[MAXN];
void add(int u,int v,int w) {
ver[++cnt] = v;
nxt[cnt] = head[u];
head[u] = cnt;
val[cnt] = w;
}
void Add(int u,int v,int w) {
add(u,v,w);
add(v,u,w);
}
void dfs(int now) {
vis[now] = 1;
for(int i = head[now];i;i=nxt[i]) {
int y = ver[i];
if(vis[y]) {
continue;
}
pos[y] = sam.insert(pos[now],val[i]);
dfs(y);
}
}
}G;
char s[MAXN];
int main () {
x = read(),n = read();
for(int i = 1;i < n; ++i) {
x = read(),y = read();
scanf("%s",s);
G.Add(x,y,s[0] - 'a');
}
G.pos[1] = 1;
G.dfs(1);
q = read();
while(q--) {
opt = read();
if(opt == 1) {
cout<<ans<<endl;
}
else if(opt == 2) {
x = read(),y = read();
for(int i = 1;i < y; ++i) {
int X = read(),Y = read();
scanf("%s",s);
G.Add(X,Y,s[0] - 'a');
}
G.dfs(x);
}
else {
scanf("%s",s);int len = strlen(s);
int p = 1;
int tag = 1;
for(int i = 0;i < len; ++i) {
if(!sam.ch[p][s[i] - 'a']) {
cout<<0<<endl;
tag = 0;
break;
}
p = sam.ch[p][s[i] - 'a'];
}
if(tag) {
cout<<lct.query(p)<<endl;
}
}
}
return 0;
}
BZOJ 4545的更多相关文章
- bzoj 4545: DQS的trie
Description DQS的自家阳台上种着一棵颗粒饱满.颜色纯正的trie. DQS的trie非常的奇特,它初始有n0个节点,n0-1条边,每条边上有一个字符.并且,它拥有极强的生长力:某个i时刻 ...
- bzoj 4545 DQS 的 Trie
老年选手不会 SAM 也不会 LCT 系列 我的数据结构好菜啊 qnq 一颗 Trie 树,$q$ 次询问,每次可以是: 1.求这棵树上本质不同的子串数量 2.插入一个子树,保证总大小不超过 $100 ...
- BZOJ 2127: happiness [最小割]
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1815 Solved: 878[Submit][Status][Di ...
- BZOJ 3275: Number
3275: Number Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 874 Solved: 371[Submit][Status][Discus ...
- BZOJ 2879: [Noi2012]美食节
2879: [Noi2012]美食节 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1834 Solved: 969[Submit][Status] ...
- bzoj 4610 Ceiling Functi
bzoj 4610 Ceiling Functi Description bzoj上的描述有问题 给出\(n\)个长度为\(k\)的数列,将每个数列构成一个二叉搜索树,问有多少颗形态不同的树. Inp ...
- BZOJ 题目整理
bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份 ...
- 【sdoi2013】森林 BZOJ 3123
Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负整数 ...
- 【清华集训】楼房重建 BZOJ 2957
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
随机推荐
- Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)
题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...
- git分支回退以及目录回退
分支回退 git checkout - 目录回退 cd -
- python学习笔记之数据类型、字符编码、文件处理
1.数据类型 1.数字(int,float) 整形(int):定义 age=20 #本质age=int(20) 浮点类型:salary=3000.3 #本质salary=float(3000.3) ...
- Cocos2d-x之Array
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Array是一个列表类容器,是一种线性序列结构:列表容器中的元素是有序的,可以通过下标来访问,就和数组一样.其中Vector也是一种列表容 ...
- 为什么学python
一.什么是Python Python [1](英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum发明. ...
- Flink分布式缓存Distributed Cache
1 分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程 ...
- 51-python基础-python3-列表-常用列表方法- index()方法
index()方法 1-可以传入一个值,如果该值存在于列表中,就返回它的下标. 实例1: 2-如果该值不在列表中,Python 就报 ValueError. 实例2: 3-如果列表中存在重复的值,就返 ...
- 【转】Pandas速查手册中文版
本文翻译自文章:Pandas Cheat Sheet - Python for Data Science,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重 ...
- Python基础篇(格式化输出,运算符,编码):
Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s 替换字符串 %d 替换整体数字 ...
- vue规范规则
vue组件,数据通信,样式,JS的规范规则.对vue官方风格指南的总结归类并加入自己的规范,在团队小组中使用. 1.项目名/文件目录命名: kebab-case(- 连接): 项目名:vue-admi ...