【题解】CF1252K Addition Robot

题目传送门

前言

第一次做线段树维护矩阵乘法的题目,算是为 ddp 打基础了吧。

分析

注意到 \[ \begin{bmatrix} A & B \end{bmatrix} \begin{bmatrix} 1 & 0\\ 1 & 1 \end{bmatrix} = \begin{bmatrix} A + B & B \end{bmatrix} \]

\[ \begin{bmatrix} A & B \end{bmatrix} \begin{bmatrix} 1 & 1\\ 0 & 1 \end{bmatrix} = \begin{bmatrix} A & A + B \end{bmatrix} \]

所以我们可以用线段树一个区间内矩阵乘法的答案,因为字母类型单一,所以我们可以顺便存一下反转字符后的答案,修改时交换即可,时间复杂度 \(O(2^3n\log n)\)。

代码

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
#define L(i, j, k, l) for (int i = (j); i <= (k); i += (l))
#define R(i, j, k, l) for (int i = (j); i >= (j); i -= (l))
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
struct Matrix {
int a[2][2];
Matrix(int p = 0, int q = 0, int x = 0, int y = 0) {a[0][0] = p, a[0][1] = q, a[1][0] = x, a[1][1] = y;}
Matrix operator*(Matrix b) {
Matrix c;
L(k, 0, 1, 1) L(i, 0, 1, 1) L(j, 0, 1, 1) c.a[i][j] = (c.a[i][j] + 1ll * a[i][k] * 1ll * b.a[k][j] % mod) % mod;
return c;
}
};
struct Sgt
{
int l, r, tag;
Matrix res0, res1;
} t[N << 2];
char s[N];
inline int read() {
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}
inline void pushup(int p) {
Sgt &u = t[p], &l = t[p << 1], &r = t[p << 1 | 1];
u.res0 = l.res0 * r.res0, u.res1 = l.res1 * r.res1;
}
inline void pushdown(int p) {
if (t[p].tag) swap(t[p << 1].res0, t[p << 1].res1), t[p << 1].tag ^= 1, swap(t[p << 1 | 1].res0, t[p << 1 | 1].res1), t[p << 1 | 1].tag ^= 1, t[p].tag = 0;
}
inline void build(int p, int l, int r) {
t[p].l = l, t[p].r = r;
if (l == r) {
t[p].res0 = Matrix(1, 0, 1, 1);
t[p].res1 = Matrix(1, 1, 0, 1);
if (s[l] == 'B')
swap(t[p].res0, t[p].res1);
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
pushup(p);
}
inline void modify(int p, int l, int r) {
if (l <= t[p].l && t[p].r <= r) {
swap(t[p].res0, t[p].res1), t[p].tag ^= 1;
return;
}
pushdown(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) modify(p << 1, l, r);
if (r > mid) modify(p << 1 | 1, l, r);
pushup(p);
}

inline Sgt merge(Sgt l, Sgt r) {
Sgt u;
u.res0 = l.res0 * r.res0, u.res1 = l.res1 * r.res1;
return u;
}

inline Sgt query(int p, int l, int r) {
if (l <= t[p].l && t[p].r <= r) return t[p];
pushdown(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l > mid) return query(p << 1 | 1, l, r);
if (r <= mid) return query(p << 1, l, r);
return merge(query(p << 1, l, r), query(p << 1 | 1, l, r));
}

int main() {
int n = read(), q = read();
cin >> s + 1;
build(1, 1, n);
while (q--) {
int op = read(), l = read(), r = read();
if (op == 1)
modify(1, l, r);
else {
Matrix a;
a.a[0][0] = read(), a.a[0][1] = read();
a = a * query(1, l, r).res0;
printf("%d %d\n", a.a[0][0], a.a[0][1]);
}
}
return 0;
}