2022ICPC网络赛2


L Quadruple

给长为$n(n \le 2\times 10^6)$的只包含$’I’, ‘C’, ‘P’$的字符串以及$q(q \le 2 \times 10^6)$ 个询问, 询问由给出的$x, a, b, p$生成, 每次询问问一个区间的子串中有多少个$ICPC$的子序列

SOLUTION

用前缀和维护到当前位置$i$为止的$I$, $IC$, $ICP$, $ICPC$, $CP$, $CPC$, $PC$, $P$, $C$的子序列个数, 然后对于询问区间只需要简单容斥一下减去不属于这个区间所产生的$ICPC$个数即可.

赛中一眼线段树的做法, 结果被卡成傻逼, 后来本地随便造了一组都跑了12s时才意识到不对劲, 在最后十分钟想到了正解但是没有写完……

时间复杂度: $O(n)$

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e6 + 10, mod = 998244353;

//icpc
int si[N], sc[N], sp[N];
int sic[N], scp[N], spc[N];
int sicp[N], scpc[N];
int sicpc[N], u[N], v[N];
char s[N];

void solve() {
int n, q;
scanf("%d%d%s", &n, &q, s);
ll x, a, b, p;
scanf("%lld%lld%lld%lld", &x, &a, &b, &p);
for(int i = 1; i <= q; i++){
x = (a * x + b) % p;
u[i] = x % n;
}
for(int i = 1; i <= q; i++){
x = (a * x + b) % p;
v[i] = x % n;
if(u[i] > v[i]) swap(u[i], v[i]);
u[i]++, v[i]++;
}
for(int i = 1; i <= n; i++){
(si[i] = si[i - 1] + (s[i - 1] == 'I')) %= mod;
(sc[i] = sc[i - 1] + (s[i - 1] == 'C')) %= mod;
(sp[i] = sp[i - 1] + (s[i - 1] == 'P')) %= mod;
(sic[i] = sic[i - 1] + si[i - 1] * (s[i - 1] == 'C')) %= mod;
(scp[i] = scp[i - 1] + sc[i - 1] * (s[i - 1] == 'P')) %= mod;
(spc[i] = spc[i - 1] + sp[i - 1] * (s[i - 1] == 'C')) %= mod;
(sicp[i] = sicp[i - 1] + sic[i - 1] * (s[i - 1] == 'P')) %= mod;
(scpc[i] = scpc[i - 1] + scp[i - 1] * (s[i - 1] == 'C')) %= mod;
(sicpc[i] = sicpc[i - 1] + sicp[i - 1] * (s[i - 1] == 'C')) %= mod;
}
int ans = 0;
for(int i = 1; i <= q; i++){
int l = u[i], r = v[i];
ll res = sicpc[r] - sicpc[l - 1];
ll c = (sc[r] - sc[l - 1]);
res -= c * sicp[l - 1] % mod;
res %= mod;
ll pc = 1ll * (spc[r] - spc[l - 1]) - c * sp[l - 1] % mod;
pc %= mod;
res = (res - pc * sic[l - 1] % mod) % mod;
ll cpc = 1ll * (scpc[r] - scpc[l - 1]) - c * scp[l - 1] % mod - pc * sc[l - 1] % mod;
cpc %= mod;
res = (res - cpc * si[l - 1] % mod) % mod;
if(res < 0) res += mod;
ans = (res + ans) % mod;
}
printf("%d", ans);
}

int main() {
int T = 1;
while (T--) solve();
return 0;
}