CF547E Mike and Friends
upd:感谢评论里@spacevortex 指正 暴力链加复杂度是$O(\sum |s|)$的 也可以通过owo
子串看起来就很SuffixStructures
于是上SAM
本来想着直接LCT
后来发现没法串定位(暴力匹配复杂度不对)
然后就离线吧,先建出来然后链加子树和,树剖就odk。
其实更直接的套路是线段树合并right集合维护。
这个题很像BZOJ3881 那个题更复杂一点,需要树链的并(到时候看看这个题可不可以加强一下扔到校内模拟赛
当然还有正确的ACA写法,直接FAIL树上主席树。(就更像BZOJ3881了
(但感觉都很暴力,我的想法貌似还挺好的)
顺便学习了广义SAM的正确写法。(发现之前的写法死活改不对
//Love and Freedom.
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define inf 20021225
#define N 200010
#define pb push_back
using namespace std;
int read()
{
int s=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<='') s=s*+ch-'',ch=getchar();
return f*s;
}
struct node{int ch[],fa,len;}t[N<<];
struct edge{int to,lt;}e[N<<];
int lt,poi,cnt,rt,ed[N],in[N<<];
vector<int> endpos;
void insert(int c,int id)
{
if(t[lt].ch[c])
{
int p=lt,np=lt=t[p].ch[c];
if(t[np].len==t[p].len+) ed[id]=np,endpos.pb(np);
else
{
int nq=++poi; ed[id]=nq; endpos.pb(nq); t[nq].len=t[p].len+;
memcpy(t[nq].ch,t[np].ch,sizeof(t[np].ch));
t[nq].fa=t[np].fa; t[np].fa=nq;
for(;p&&t[p].ch[c]==np;p=t[p].fa) t[p].ch[c]=nq;
lt=nq;
}
return;
}
int p=lt,np=lt=++poi; endpos.pb(np); ed[id]=np; t[np].len=t[p].len+;
for(;p&&!t[p].ch[c];p=t[p].fa) t[p].ch[c]=np;
if(!p){t[np].fa=rt; return;} int q=t[p].ch[c];
if(t[q].len==t[p].len+){t[np].fa=q; return;}
int nq=++poi; t[nq].fa=t[q].fa; t[q].fa=t[np].fa=nq;
memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch)); t[nq].len=t[p].len+;
for(;p&&t[p].ch[c]==q;p=t[p].fa) t[p].ch[c]=nq;
}
#define lowbit(x) (x&-x)
int tr[N<<];
void add(int x,int v){while(x<=poi) tr[x]+=v,x+=lowbit(x);}
void modify(int l,int r,int v){add(l,v); add(r+,-v);}
int query(int x){int a=; while(x) a+=tr[x],x-=lowbit(x); return a;}
int dfn[N<<],tot,sz[N<<],son[N<<],idfn[N<<],top[N<<],fa[N<<];
void addedge(int x,int y){e[++cnt].to=y; e[cnt].lt=in[x]; in[x]=cnt;}
void build(){for(int i=;i<=poi;i++) addedge(t[i].fa,i);}
void dfs(int x,int fr)
{
fa[x]=fr; sz[x]=;
for(int i=in[x];i;i=e[i].lt)
{
int y=e[i].to; dfs(y,x);
sz[x]+=sz[y]; if(sz[y]>=sz[son[x]]) son[x]=y;
}
}
void dfs2(int x,int t)
{
top[x]=t; dfn[x]=++tot; idfn[tot]=x; if(!son[x]) return; dfs2(son[x],t);
for(int i=in[x];i;i=e[i].lt) if(e[i].to!=son[x]) dfs2(e[i].to,e[i].to);
}
void modify(int x,int v)
{
while(x) modify(dfn[top[x]],dfn[x],v), x=fa[top[x]];
}
int ask(int x){return query(dfn[x]);}
#define Q 500010
int n,q; char ch[N]; int ans[Q],pre[N];
struct qry{int id,opt,pos,x;}r[Q<<]; int qwq;
bool operator<(qry a,qry b){return a.pos<b.pos;}
int main()
{
n=read(),q=read(); poi=rt=lt=; int tot=;
for(int i=;i<=n;i++)
{
scanf("%s",ch+); lt=rt; int sn=strlen(ch+);
for(int j=;j<=sn;j++) ++tot,insert(ch[j]-'a',i);
pre[i]=tot;
}
int L,R,K; build(); dfs(,); dfs2(,);
for(int i=;i<=q;i++)
L=read(),R=read(),K=read(),
r[++qwq].id=i,r[qwq].opt=-,r[qwq].pos=pre[L-],r[qwq].x=ed[K],
r[++qwq].id=i,r[qwq].opt=,r[qwq].pos=pre[R],r[qwq].x=ed[K];
int p=; sort(r+,r+qwq+);
while(p<=qwq&&!r[p].pos) p++;
for(int i=;i<endpos.size();i++)
{
int pos=endpos[i]; modify(pos,);
while(p<=qwq&&r[p].pos==i+) ans[r[p].id]+=r[p].opt*ask(r[p].x),p++;
}
for(int i=;i<=q;i++) printf("%d\n",ans[i]);
return ;
}
CF547E Mike and Friends的更多相关文章
- CF547E Mike and Friends 后缀自动机+线段树合并
裸题,敲完后没调就过了 ~ code: #include <bits/stdc++.h> using namespace std; #define ll long long #define ...
- CF547E Mike and Friends [AC自动机,离线树状数组]
#include <cstdio> #include <queue> #include <vector> #define pb emplace_back using ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF #305(Div.2) D. Mike and Feet(数学推导)
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- codeforces 361 A - Mike and Cellphone
原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...
- codeforces 361 B - Mike and Shortcuts
原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...
- CodeForces 689C Mike and Chocolate Thieves (二分)
原题: Description Bad news came to Mike's village, some thieves stole a bunch of chocolates from the l ...
随机推荐
- DeepFaceLab 模型预训练参数Pretrain的使用!
Pretrain参数是20190501版本才加入的参数,作者加入这个参数的目的应该是提升模型的训练速度和增强适应性.具体有哪些提升,需要大家去摸索,我这里分享一下自己的使用过程. 这个参数仅针对S ...
- docker中 devicemapper驱动挂载容器镜像文件
详解docker中容器devicemapper设备的挂载流程
- mysql添加字段索引失败 BLOB/TEXT column 'col2' used in key specification without a key length
看了下该表的数据结构发现col2字段类型是text ,查询了下发现是:MySQL只能将BLOB/TEXT类型字段设置索引为BLOB/TEXT数据的钱N个字符.索引指定下col2的长度就可以了 : al ...
- 调用user32.dll显示其他窗口
/// 该函数设置由不同线程产生的窗口的显示状态 /// </summary> /// <param name="hWnd">窗口句柄</param& ...
- 阿里云 centos 部署 Django 可能遇到的问题
问题一:版本限制 File "/Users/icourt/Desktop/hf/venv/lib/python3.7/site-packages/django/db/backends/m ...
- python每日一练:0014题
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: { "1":["张三",150,120,100], &q ...
- 【组策略】1.组策略介绍group policy
组策略介绍group policy 高效学习法,念念不忘,必有回响. 分享一个高效学习思维,潜意识思考.就是在您没有大量时间的情况下,学习十分钟. 然后离开去完成别的事情的时候,大脑潜意识中还会继续思 ...
- LeetCode链表简单题
一.21合并两个有序链表 代码如下: class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNod ...
- 安装mysql5.6-centOs7
安装mysql mysql,下载地址:https://dev.mysql.com/downloads/mysql/ 安装参考链接:https://segmentfault.com/a/11900000 ...
- 自定义SpringBoot启动控制台图标
使用过SpringBoot的小伙伴众所周知,在启动的过程中,在控制台会首先打印spring的图标以及版本号(这里以IDEA为例) 如果需要更改这个打印图标的话, 需要以下步骤: 1.打开SpringB ...