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的更多相关文章

  1. CF547E Mike and Friends 后缀自动机+线段树合并

    裸题,敲完后没调就过了 ~ code: #include <bits/stdc++.h> using namespace std; #define ll long long #define ...

  2. CF547E Mike and Friends [AC自动机,离线树状数组]

    #include <cstdio> #include <queue> #include <vector> #define pb emplace_back using ...

  3. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. codeforces 361 A - Mike and Cellphone

    原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...

  8. codeforces 361 B - Mike and Shortcuts

    原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...

  9. CodeForces 689C Mike and Chocolate Thieves (二分)

    原题: Description Bad news came to Mike's village, some thieves stole a bunch of chocolates from the l ...

随机推荐

  1. Octavia 创建 Listener、Pool、Member、L7policy、L7 rule 与 Health Manager 的实现与分析

    目录 文章目录 目录 创建 Listener 创建 Pool 创建 Member CalculateDelta HandleNetworkDeltas AmphoraePostNetworkPlug ...

  2. spring-mvc 3.* 多视图解析配置实例 ContentNegotiatingViewResolver

    一.起因     从spring 3.1.0升级到spring 3.2.0时,配置文件servlet.xml中出错. 错误信息: java.lang.String cannot be cast to  ...

  3. 那些堪称神器的 Chrome 插件

    Chrome 的简洁快速以及丰富的插件种类使得它在国内日益盛行,帮助了我们很多 Chrome 用户提升了工作效率,而今天要给大家推荐8款实用甚至堪称神器的 Chrome 插件,希望对提升大家的工作效率 ...

  4. 知识图谱之图数据库Neo4j

    知识图谱中的知识是通过RDF结构来进行表示的,其基本单元是事实.每个事实是一个三元组(S, P, O),在实际系统中,按照存储方式的不同,知识图谱的存储可以分为基于表结构的存储和基于图结构的存储. 基 ...

  5. 【Linux开发】计算机底层是如何访问显卡的?

    1. 显卡驱动是怎么控制显卡的, 就是说, 使用那些指令控制显卡, 通过端口么? 2. DirectX 或 OpenGL 或 CUDA 或 OpenCL 怎么找到显卡驱动, 显卡驱动是不是要为他们提供 ...

  6. js swich

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. int快读

    昨天偶然间看到CJ_tony的快读,所以便决定学习一下. 这个快读的原理就是:读入单个字符要比读入读入数字快,先读入字符,然后再转化成数字.(原理的话大学再研究) 代码: #include<io ...

  8. 最少多少人说谎(dp)

    https://ac.nowcoder.com/acm/contest/1168/H 题意:n个学生,邓志聪想知道这些学生的考试情况,于是一个一个叫这些学生叫去办公室问他们,但是有些学生并没有讲真话, ...

  9. 1000行MySQL学习笔记

    /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...

  10. 在cmd下用cd怎么进不了其他的盘

    你当前就是在C盘目录下的,可以切换到别的盘比如D:,然后在切换E:!然后可以切换C:,然后可以用cd\回到根目录. cd是打开文件根目录里面文件夹的,比如C:目录下可以cd Windows打开Wind ...