Still ablaze

P4248。

关于后缀数组的定义什么的,这里不再赘述。

考虑考察题目的式子,\(len(T_i) + len(T_j)\) 是简单的,这里不作推导。我们只考虑 \(-2 \times lcp(T_i,T_j)\)。这个东西显然就是任意两个后缀的 \(LCP\) 长度之和,因此我们考虑怎么使用 \(height\) 数组解决。

根据结论,我们有 \(LCP(T_i,T_j) = \min_{p=rk_i}^{rk_j} height_p\),因此我们不难把问题转化为每一个 \(height\) 的贡献区间,显然其能产生贡献,当且仅当这是这个区间的最小值,我们可以使用单调栈维护这个东西。

Code

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define File(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define LL long long
#define fi first
#define se second
const int N = 5e5 + 10;
string s;
int n;
int sa[N],rk[N],oldrk[2*N],id[N],cnt[N],ht[N];
stack<int> sta;
int L[N],R[N];
int main(){
	IOS;
	cin >> s;
	n = s.size();
	s = " " + s;
	s = s + "#";
	int m = 'z';
	for(int i=1;i<=n;i++)
		cnt[rk[i]=s[i]]++;
	for(int i=1;i<=m;i++)
		cnt[i] += cnt[i-1];
	for(int i=n;i>=1;i--)
		sa[cnt[rk[i]]--] = i;
	memcpy(oldrk+1,rk+1,n*4);
	int p = 0;
	for(int i=1;i<=n;i++)
		if(oldrk[sa[i]] == oldrk[sa[i-1]])
			rk[sa[i]] = p;
		else
			rk[sa[i]] = ++p;
	m = p;
	for(int w=1;w<=n;w<<=1,m=p){
		int cur = 0;
		for(int i=n-w+1;i<=n;i++)
			id[++cur] = i;
		for(int i=1;i<=n;i++)
			if(sa[i] > w) id[++cur] = sa[i] - w;
		fill(cnt+1,cnt+1+m,0);
		for(int i=1;i<=n;i++)
			cnt[rk[id[i]]] ++ ;
		for(int i=1;i<=m;i++)
			cnt[i] += cnt[i-1];
		for(int i=n;i>=1;i--)
			sa[cnt[rk[id[i]]] -- ] = id[i];

		p =0;
		memcpy(oldrk+1,rk+1,n*4);
		for(int i=1;i<=n;i++)
			if(oldrk[sa[i]] == oldrk[sa[i-1]] && oldrk[sa[i] + w] == oldrk[sa[i-1] + w])
				rk[sa[i]] = p;
			else
				rk[sa[i]] = ++p;
	}

	ht[1] = 0;
	for(int i=1,k=0;i<=n;i++){
		if(k) k -- ;
		while(rk[i] > 1 && s[i+k] == s[sa[rk[i]-1] + k])
			k ++ ;
		if(rk[i] == 1) k = 0;
		ht[rk[i]] = k;
	}

	LL res = 0;
	res = 1ll * (1 + n) * n * n / 2;
	for(int i=1;i<=n;i++){
		while(!sta.empty() && ht[i] <= ht[sta.top()])
			sta.pop();
		if(sta.empty())
			L[i] = 0;
		else
			L[i] = sta.top();
		sta.push(i);
	}
	while(!sta.empty()) sta.pop();
	sta.push(n+1);
	for(int i=n;i>=1;i--){
		while(!sta.empty() && ht[i] < ht[sta.top()])
			sta.pop();
		R[i] = sta.top();
		sta.push(i);
	}
	for(int i=1;i<=n;i++)
		res -= 2ll * ht[i] * (i - L[i]) * (R[i] - i);
	cout << res - 1ll * n * (n + 1) / 2;
	return 0;
}

扩展

P7409 SvT

本质相同的做法,唯一不同的点在于我们这次只考虑部分后缀了。

实际上还是一样的,我们考虑使用ST表进行查询,然后可以得到两个 \(rk\) 相邻的后缀的 \(LCP\),然后再跑单调栈。

Code

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define File(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define LL long long
#define fi first
#define se second
const int N = 5e5 + 10;
const LL mod = 23333333333333333;
namespace SA{

int sa[N],rk[N],oldrk[N<<1],id[N],cnt[N],ht[N];
string s;
int n,m;
void build(){
	s = s + '#';
	for(int i=1;i<=n;i++)
		cnt[rk[i]=s[i]] ++ ;
	m = 'z';
	for(int i=1;i<=m;i++)
		cnt[i] += cnt[i-1];
	for(int i=n;i>=1;i--)
		sa[cnt[rk[i]]--] = i;
	memcpy(oldrk+1,rk+1,n*4);
	int p = 0;
	for(int i=1;i<=n;i++)
		if(oldrk[sa[i]] == oldrk[sa[i-1]])
			rk[sa[i]] = p;
		else
			rk[sa[i]] = ++p;
	for(int w=1;w<=n;w<<=1,m=p){
		int cur = 0;
		for(int i=n-w+1;i<=n;i++)
			id[++cur] = i;
		for(int i=1;i<=n;i++)
			if(sa[i] > w) id[++cur] = sa[i] - w;
		fill(cnt+1,cnt+1+m,0);
		for(int i=1;i<=n;i++)
			cnt[rk[id[i]]] ++ ;
		for(int i=1;i<=m;i++)
			cnt[i] += cnt[i-1];
		for(int i=n;i>=1;i--)
			sa[cnt[rk[id[i]]]--] = id[i];
		memcpy(oldrk+1,rk+1,n*4);
		p = 0;
		for(int i=1;i<=n;i++)
			if(oldrk[sa[i]] == oldrk[sa[i-1]] && oldrk[sa[i]+w] == oldrk[sa[i-1]+w])
				rk[sa[i]] = p;
			else
				rk[sa[i]] = ++p;
	}
	for(int i=1,k=0;i<=n;i++){
		if(k) k -- ;
		while(s[i+k] == s[sa[rk[i]-1] + k]) k ++ ;
		ht[rk[i]] = k;
	}
	ht[1] = 0;
}
int ST[N][20],lg[N];
void buildST(){
	for(int i=1;i<=n;i++)
		ST[i][0] = ht[i];
	lg[1] = 0;
	for(int i=2;i<=n;i++)
		lg[i] = lg[i>>1] + 1;
	for(int k=1;k<=lg[n];k++){
		for(int i=1;i<=n - (1 << k) + 1;i++)
			ST[i][k] = min(ST[i][k-1],ST[i+(1<<k-1)][k-1]);
	}
}
int query(int L,int R){
	int x = lg[R - L + 1];
	return min(ST[L][x],ST[R-(1<<x)+1][x]);
}
void init(const string &o){
	s = o;
	n = o.size() - 1;
	return ;
}
};

int query(int L,int R){
	return SA::query(L,R);
}
bool cmp(int x,int y){
	return SA::rk[x] < SA::rk[y];
}
int n;
string s;
stack<int> sta;
int main(){
	IOS;
	int Q;
	cin >> n >> Q;
	cin >> s;
	s = " " + s;
	SA::init(s); SA::build(); SA::buildST();
	while(Q -- ){
		int len;
		cin >> len;
		vector<int> vec(len);
		for(int i=0;i<len;i++)
			cin >> vec[i];
		sort(vec.begin(),vec.end());
		vec.erase(unique(vec.begin(),vec.end()),vec.end());
		sort(vec.begin(),vec.end(),cmp);
		len = vec.size();
		vector<int> vec2;
		for(int i=0;i<len-1;i++){
			int x,y;
			x = vec[i];
			y = vec[i+1];
			vec2.push_back(query(SA::rk[x]+1,SA::rk[y]));
		}
		len = vec2.size();
		vector<int> L(len),R(len);
		while(!sta.empty()) sta.pop();
		for(int i=0;i<len;i++){
			while(!sta.empty() && vec2[i] < vec2[sta.top()])
				sta.pop();
			if(sta.empty()) L[i] = -1;
			else L[i] = sta.top();
			sta.push(i);
		}
		while(!sta.empty()) sta.pop();
		for(int i=len-1;i>=0;i--){
			while(!sta.empty() && vec2[i] <= vec2[sta.top()])
				sta.pop();
			if(sta.empty()) R[i] = len;
			else R[i] = sta.top();
			sta.push(i);
		}
		LL res = 0;
		for(int i=0;i<len;i++){
			res += 1ll * vec2[i] * (i - L[i]) * (R[i] - i) % mod;
			res %= mod;
		}
		cout << res << "\n";
	}
	return 0;
}
posted @ 2026-09-28 21:13  WinterXorSnow  阅读(5)  评论(0)    收藏  举报