已經菜爆了只會做簡單題…

Description

每個位置有三種屬性, 要支持把一個屬性加到另一個上, 或者屬性加, 屬性乘, 屬性賦值, 都是區間操作, 還有查詢區間和.

Solution

維護一個$4 \times 4$的矩陣, 分別是三種屬性和外加的單位, 第4項在單點設置爲1就好了.

然後看情況轉移一下.

順便這題卡常, 實現要注意細節.

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#pragma GCC optimize(3)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define mod 998244353

using namespace std;
typedef long long ll;

const int maxn = 25e+4 + 5;

inline void add(int &x, int y) {
x += y;
if (x >= mod)
x -= mod;
}

struct matrix {
int a[4][4];
matrix() {
}
int* operator[](int x) {
return a[x];
}
inline void empty() {
memset(a, 0, sizeof a);
}
inline matrix operator*(matrix rhs) {
matrix ret;
ret.empty();
for (int k = 0; k < 4; ++k)
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
add(ret[i][j], 1ll * a[i][k] * rhs[k][j] % mod);
return ret;
}
inline matrix operator*=(matrix rhs) {
matrix ret;
ret.empty();
for (int i = 0; i < 4; ++i)
for (int k = 0; k < 4; ++k)
if (a[i][k])
for (int j = 0; j < 4; ++j)
add(ret[i][j], 1ll * a[i][k] * rhs[k][j] % mod);
return (*this) = ret;
}
inline bool E() {
/*for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
if (i == j && a[i][j] != 1) return false;
else if (i != j && a[i][j]) return false;
}*/
if (! (a[0][0] == 1 && a[1][1] == 1 && a[2][2] == 1 && a[3][3] == 1))
return false;
if (a[0][1] || a[0][2] || a[0][3] || a[1][0] || a[1][2] || a[1][3] ||
a[2][0] || a[2][1] || a[2][3] || a[3][0] || a[3][1] || a[3][2])
return false;
return true;
}
inline void e() {
memset(a, 0, sizeof a);
a[0][0] = a[1][1] = a[2][2] = a[3][3] = 1;
}
};
matrix trans, tag[maxn << 2];
int n, m, sum[maxn << 2][4];
int v[maxn][3], tmp[4], res[4];

inline int rd() {
register int x = 0, c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
inline void puttag(int x, matrix t) {
memset(tmp, 0, sizeof tmp);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
add(tmp[j], 1ll * sum[x][i] * t[i][j] % mod);
//for (int i = 0; i < 4; ++i)
// sum[x][i] = tmp[i];
sum[x][0] = tmp[0];
sum[x][1] = tmp[1];
sum[x][2] = tmp[2];
sum[x][3] = tmp[3];
}
inline void tagon(int cur, matrix t) {
puttag(cur, t);
tag[cur] *= t;
}
inline void pushdown(int cur) {
if (tag[cur].E()) return;
tagon(cur << 1, tag[cur]);
tagon(cur << 1|1, tag[cur]);
tag[cur].e();
}
inline void pushup(int cur) {
sum[cur][0] = (sum[cur << 1][0] + sum[cur << 1|1][0]) % mod;
sum[cur][1] = (sum[cur << 1][1] + sum[cur << 1|1][1]) % mod;
sum[cur][2] = (sum[cur << 1][2] + sum[cur << 1|1][2]) % mod;
sum[cur][3] = (sum[cur << 1][3] + sum[cur << 1|1][3]) % mod;
}
void build(int cur, int l, int r) {
tag[cur].e();
if (l == r) {
sum[cur][0] = v[l][0];
sum[cur][1] = v[l][1];
sum[cur][2] = v[l][2];
sum[cur][3] = 1;
return;
}
int mid = (l + r) >> 1;
build(cur << 1, l, mid);
build(cur << 1|1, mid + 1, r);
pushup(cur);
}
void update(int cur, int l, int r, int L, int R, matrix t) {
if (L <= l && r <= R) {
tagon(cur, t);
return;
}
int mid = (l + r) >> 1; pushdown(cur);
if (L <= mid) update(cur << 1, l, mid, L, R, t);
if (R > mid) update(cur << 1|1, mid + 1, r, L, R, t);
pushup(cur);
}
void query(int cur, int l, int r, int L, int R) {
if (L <= l && r <= R) {
add(res[0], sum[cur][0]);
add(res[1], sum[cur][1]);
add(res[2], sum[cur][2]);
return;
}
int mid = (l + r) >> 1; pushdown(cur);
if (L <= mid) query(cur << 1, l, mid, L, R);
if (R > mid) query(cur << 1|1, mid + 1, r, L, R);
}

int main() {
n = rd();
for (int i = 1; i <= n; ++i)
v[i][0] = rd(),
v[i][1] = rd(),
v[i][2] = rd();
build(1, 1, n);
m = rd();
int opt, l, r, x;
while (m--) {
opt = rd(); l = rd(); r = rd();
trans.e();
if (opt == 1) {
trans[1][0]++;
} else if (opt == 2) {
trans[2][1]++;
} else if (opt == 3) {
trans[0][2]++;
} else if (opt == 4) {
x = rd();
trans[3][0] = (trans[3][0] + x) % mod;
} else if (opt == 5) {
x = rd();
trans[1][1] = x;
} else if (opt == 6) {
x = rd();
trans[2][2] = 0; trans[3][2] = x;
}

if (opt == 7) {
memset(res, 0, sizeof res);
query(1, 1, n, l, r);
printf("%d %d %d\n", res[0], res[1], res[2]);
} else {
update(1, 1, n, l, r, trans);
}
}
return 0;
}