跳到正文

题解

题解:CF1801G A task for substrings

highkj约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年2月10日
收录
2026年11月12日
更新
2026年11月17日
篇幅
约 10 分钟
获赞
2
题目
CF1801G

版本

  1. v1当前

前言

复建来了。

思路

首先我们可以有一个粗略的思路就是用前缀 1∼r1\sim r 的答案减去 1∼l−11\sim l-1 的答案然后得到一个大于等于正确答案的东西,然后我们考虑一下哪些答案会被多算我们发现如果有一个串的 ll 在 1∼l−11\sim l-1 中然后 rr 在 l∼rl\sim r 中的话那么这个串我们无法减去所以我们考虑对于每一组询问算这样的数量然后剪掉即可。

细节讲一下这两个步骤如何实现。

  • 首先我们对于求一个前缀的答案只需要建一个关于 sis_i 的 ACAM 然后我们去走每一个前缀假设走到的点为 pp 然后去看他在 fail 树上的父亲中有多少个为 sjs_j 的末尾节点即可,这个用树状数组的子树加不难做到。
  • 然后就只剩下如何求出每一次询问给出的 l,rl,r 的多的答案了,我们先简单刻画一下这个串要满足什么,设这个串为 tt 中的 L∼RL\sim R 然后满足 1≤L<l,l≤R≤r1\leq L<l,l\leq R\leq r 那么我们在 ACAM 的体现就是存在一种把 sis_i 分开的方法让 1∼p1\sim p 为 1∼l−11\sim l-1 的一个后缀并且 p+1∼lenip+1\sim len_i 为 l∼rl\sim r 的一个前缀,对于前一个条件我们只需要暴力遍历每一个分界点那么要满足的就是 1∼l−11\sim l-1 走到的点在 fail 树上为 1∼p1\sim p 的点的子树中,但是我们发现前缀并不好搞,所以我们考虑建出反串的 ACAM,至于实现在下面细讲。
  • 建出了反串的 ACAM 之后,我们还是一样在上面走 p+1∼lenip+1\sim len_i(假设走到的点为 kk)然后我们需要满足的就是 n→ln\to l 的点在 kk 的子树内,但是我们发现会存在一个串的结尾超出了 rr 的情况所以我们需要从 n→1n\to 1 走到的点开始网上跳一段 fail 树直到跳到一个点的 dep≤ri−li+1dep\leq r_i-l_i+1 就停止,那么查这个点就能保证不会超出 rr 了,至于如何实现这个过程相信大家都会二维数点,直接数就行了。

实在不会的话只需要把充要条件写出来就行了假设 1→p1\to p 的点为 aa,p+1→lenip+1\to len_i 的点为 bb,1→l−11\to l-1 的点为 cc,l∼nl\sim n 的点为 dd,那么只需要满足。

dfna≤dfnc≤dfna+siza−1dfn1b≤dfn1d≤dfn1b+siz1b−1dfn_a\leq dfn_c\leq dfn_a+siz_a-1\\ dfn1_b\leq dfn1_d\leq dfn1_b+siz1_b-1

只需要注意是在两棵树上的 dfs 序就行了。

代码

写得有点史。

C++
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define fire signed
#define ll long long
#define il inline
template<class T> il void print(T x) {
	if(x<0) printf("-"),x=-x;
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
}
int T=1;
const int N=1e6+10,M=5e5+10;
struct nn{
	struct node{
		int s[30];
		int lst;
	}tr[N];
	int idx;
	int len[N];
	void modify(string s) {
		int p=0;
		for(auto to:s) {
			if(!tr[p].s[to-'a']) {
				tr[p].s[to-'a']=++idx;
				len[idx]=len[p]+1;
			}
			p=tr[p].s[to-'a'];
		}
	}
	void build() {
		queue<int>q;
		rep(i,0,25) if(tr[0].s[i]) q.push(tr[0].s[i]);
		while(q.size()) {
			int x=q.front();
			q.pop();
			rep(i,0,25) {
				int &to=tr[x].s[i];
				if(to) {
					tr[to].lst=tr[tr[x].lst].s[i];
					q.push(to);
				}else {
					tr[x].s[i]=tr[tr[x].lst].s[i];
				}
			}
		}
	}
	vector<int>v[N];
	int dfn[N],tot,siz[N];
	int f[N][20];
	void dfs(int x,int fa) {
		dfn[x]=++tot;
		siz[x]=1;
		f[x][0]=fa;
		for(auto to:v[x]) {
			if(to==fa) continue;
			dfs(to,x);
			siz[x]+=siz[to];
		}
	}
	void ii() {
		rep(j,1,19) {
			rep(i,1,tot) f[i][j]=f[f[i][j-1]][j-1];
		}
	}
	void init() {
		rep(i,0,idx) v[tr[i].lst].pb(i);
	}
}t1,t2;
struct nnn{
	int a,b,c;
};
vector<nnn>pl[N];
ll tr[N*5];
int lowbit(int x) {
	return x&-x;
}
void add(int x,int k,int mx) {
	for(;x<=mx;x+=lowbit(x)) {
		tr[x]+=k;
	}
}
ll Ans(int x) {
	ll res=0;
	for(;x;x-=lowbit(x)) res+=tr[x];
	return res;
}
ll ans[M];
int n,m;
vector<int>qy[N];
string t,s[M];
ll sum[N*5];
int ll1[M],rr[M];
vector<int>idd[N*5];
int ip[M];
vector<int>id1[N*5];
void solve() {
	cin>>n>>m;
	cin>>t;
	rep(i,1,n) cin>>s[i],t1.modify(s[i]);
	rep(i,1,n) {
		reverse(s[i].begin(),s[i].end());
		t2.modify(s[i]);
		reverse(s[i].begin(),s[i].end());
	}
	t1.build();
	t2.build();
	t1.init();
	t2.init();
	t1.dfs(0,0);
	t2.dfs(0,0);
	t2.ii();
	int lt=t.size()-1;
	int now=0;
	rep(i,1,n) {
		int p=0;
		for(auto to:s[i]) {
			p=t1.tr[p].s[to-'a'];
		}
		add(t1.dfn[p],1,t1.tot);
		add(t1.dfn[p]+t1.siz[p],-1,t1.tot);
	}
	rep(i,0,lt) {
		now=t1.tr[now].s[t[i]-'a'];
		sum[i+1]=Ans(t1.dfn[now]);
	}
	rep(i,1,lt+1) sum[i]+=sum[i-1];
	rep(i,1,m) {
		int l,r;
		cin>>l>>r;
		ll1[i]=l,rr[i]=r;
		idd[l-1].pb(i);
		id1[l].pb(i);
		ans[i]=sum[r]-sum[l-1];
	}
	now=0;
	rep(i,1,lt+1) {
		now=t1.tr[now].s[t[i-1]-'a'];
		for(auto to:idd[i]) {
			qy[t1.dfn[now]].pb(to);
		}
	}
	rep(i,1,n) {
		vector<int>v,v1;
		v.clear(),v1.clear();
		int p=0;
		for(auto to:s[i]) {
			p=t1.tr[p].s[to-'a'];
			v.pb(p);
		}
		p=0;
		reverse(s[i].begin(),s[i].end());
		for(auto to:s[i]) {
			p=t2.tr[p].s[to-'a'];
			v1.pb(p);
		}
		int ll11=v.size()-1;
		rep(j,0,ll11-1) {
			int a=t1.dfn[v[j]],b=t1.dfn[v[j]]+t1.siz[v[j]]-1;
			int c=t2.dfn[v1[ll11-1-j]],d=t2.dfn[v1[ll11-1-j]]+t2.siz[v1[ll11-1-j]]-1;
			pl[a].pb({c,d,1});
			pl[b+1].pb({c,d,-1});
		}
	}
	now=0;
	rep1(i,lt+1,1) {
		now=t2.tr[now].s[t[i-1]-'a'];
		for(auto to:id1[i]) {
			ip[to]=now;
		}
	}
	memset(tr,0,sizeof tr);
	rep(i,1,t1.tot) {
		for(auto to:pl[i]) {
			add(to.a,to.c,t2.tot);
			add(to.b+1,-to.c,t2.tot);
		}
		for(auto id:qy[i]) {
			int now=ip[id];
			int len=rr[id]-ll1[id]+1;
			rep1(j,19,0) {
				if(t2.f[now][j]&&t2.len[t2.f[now][j]]>len) {
					now=t2.f[now][j];
				}
			}
			if(len<t2.len[now]) now=t2.f[now][0];
			ans[id]-=Ans(t2.dfn[now]);
		}
	}
	rep(i,1,m) printf("%lld ",ans[i]);
}
fire main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0); 
	while(T--) {
		solve();
	}
	return false;
}
原文