在这冷漠的世界里光光哭哭
Posted solemntee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在这冷漠的世界里光光哭哭相关的知识,希望对你有一定的参考价值。
对于一个子串
a
b
c
abc
abc,可以容易的求出前
i
i
i位子序列个数,记为
d
p
[
i
]
[
a
]
[
b
]
[
c
]
dp[i][a][b][c]
dp[i][a][b][c]
对于一个子串
a
b
ab
ab,可以容易的求出前
i
i
i位子序列个数,记为
d
p
[
i
]
[
a
]
[
b
]
dp[i][a][b]
dp[i][a][b]
对于一个子串
a
a
a,可以容易的求出前
i
i
i位子序列个数,记为
d
p
[
i
]
[
a
]
dp[i][a]
dp[i][a]
首先考虑 d p [ r ] [ a ] [ b ] [ c ] − d p [ l − 1 ] [ a ] [ b ] [ c ] dp[r][a][b][c]-dp[l-1][a][b][c] dp[r][a][b][c]−dp[l−1][a][b][c],这样得到的序列数量多算了两类。
第一类是
b
c
bc
bc在
[
l
,
r
]
[l,r]
[l,r]内,
a
a
a在
[
1
,
l
−
1
]
[1,l-1]
[1,l−1]内
[
l
,
r
]
[l,r]
[l,r]内的
b
c
bc
bc序列的数量为
d
p
[
r
]
[
a
]
[
b
]
−
d
p
[
l
−
1
]
[
a
]
[
b
]
−
(
d
p
[
r
]
[
b
]
−
d
p
[
l
−
1
]
[
b
]
)
×
d
p
[
l
−
1
]
[
a
]
dp[r][a][b]-dp[l-1][a][b]-(dp[r][b]-dp[l-1][b])\\times dp[l-1][a]
dp[r][a][b]−dp[l−1][a][b]−(dp[r][b]−dp[l−1][b])×dp[l−1][a]
第一类的数量为
(
d
p
[
r
]
[
a
]
[
b
]
−
d
p
[
l
−
1
]
[
a
]
[
b
]
−
(
d
p
[
r
]
[
b
]
−
d
p
[
l
−
1
]
[
b
]
)
×
d
p
[
l
−
1
]
[
a
]
)
×
d
p
[
l
−
1
]
[
a
]
(dp[r][a][b]-dp[l-1][a][b]-(dp[r][b]-dp[l-1][b])\\times dp[l-1][a])\\times dp[l-1][a]
(dp[r][a][b]−dp[l−1][a][b]−(dp[r][b]−dp[l−1][b])×dp[l−1][a])×dp[l−1][a]
第二类是
c
c
c在
[
l
,
r
]
[l,r]
[l,r]内,
a
b
ab
ab在
[
1
,
l
−
1
]
[1,l-1]
[1,l−1]内
数量为
(
d
p
[
c
]
[
r
]
−
d
p
[
c
]
[
l
−
1
]
)
×
d
p
[
a
]
[
b
]
[
l
−
1
]
(dp[c][r]-dp[c][l-1])\\times dp[a][b][l-1]
(dp[c][r]−dp[c][l−1])×dp[a][b][l−1]
最后答案为
d
p
[
r
]
[
a
]
[
b
]
[
c
]
−
d
p
[
l
−
1
]
[
a
]
[
b
]
[
c
]
−
(
(
d
p
[
r
]
[
a
]
[
b
]
−
d
p
[
l
−
1
]
[
a
]
[
b
]
−
(
d
p
[
r
]
[
b
]
−
d
p
[
l
−
1
]
[
b
]
)
×
d
p
[
l
−
1
]
[
a
]
)
×
d
p
[
l
−
1
]
[
a
]
)
−
(
(
d
p
[
c
]
[
r
]
−
d
p
[
c
]
[
l
−
1
]
)
×
d
p
[
a
]
[
b
]
[
l
−
1
]
)
dp[r][a][b][c]-dp[l-1][a][b][c]-((dp[r][a][b]-dp[l-1][a][b]-(dp[r][b]-dp[l-1][b])\\times dp[l-1][a])\\times dp[l-1][a])-((dp[c][r]-dp[c][l-1])\\times dp[a][b][l-1])
dp[r][a][b][c]−dp[l−1][a][b][c]−((dp[r][a][b]−dp[l−1][a][b]−(dp[r][b]−dp[l−1][b])×dp[l−1][a])×dp[l−1][a])−((dp[c][r]−dp[c][l−1])×dp[a][b][l−1])
注意到
d
p
[
n
]
[
a
]
[
b
]
[
c
]
dp[n][a][b][c]
dp[n][a][b][c]可以用
O
(
n
×
2
6
2
)
O(n\\times26^2)
O(n×262)的复杂度完成(利用末端点优化掉一维),我们可以离线每组答案的询问
O
(
n
×
2
6
2
+
q
×
C
)
O(n\\times26^2+q\\times C)
O(n×262+q×C)完成
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
int pos;
ll val;
;
struct query
int len,pos,id,f,a,b,c;
;
vector<query>v[80005];
ll Q[500005][8];
ll presum[26][26][26];
ll dp[26][26][3];
char ss[4];
char s[80005];
int main()
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",s+1);
for(int i=1;i<=q;i++)
// printf("q=%d\\n",q);
int l,r;
scanf("%d%d",&l,&r);
scanf("%s",ss+1);
// printf("q=%d\\n",q);
/// 长度为3的前缀和
v[l-1].push_back(3,5,i,-1,ss[1]-'a',ss[2]-'a',ss[3]-'a');
v[r].push_back(3,5,i,1,ss[1]-'a',ss[2]-'a',ss[3]-'a');
///前面2 后面1
v[l-1].push_back(2,1,i,1,ss[1]-'a',ss[2]-'a',ss[3]-'a');
v[r].push_back(1,2,i,1,ss[3]-'a',0,0);
v[l-1].push_back(1,2,i,-1,ss[3]-'a',0,0);
///前面1 后面2
v[l-1].push_back(1,3,i,1,ss[1]-'a',ss[2]-'a',ss[3]-'a');
v[r].push_back(2,4,i,1,ss[2]-'a',ss[3]-'a',0);
v[l-1].push_back(2,4,i,-1,ss[2]-'a',ss[3]-'a',0);
v[r].push_back(1,6,i,12022牛客寒假算法基础集训营 4 全部题解