前情提示: 此题极度卡常!!!,否则你就会像我这个蒟蒻一样卡题\(3h\):
前置知识:
2.基本的矩阵乘法:Fibonacci
题解部分
对于题目给出的6种操作,我们可以用线段树与矩阵乘法来维护
思路
维护一个四元组,并转化成矩阵: [\(A\) \(i\) \(B\)\(i\) \(C\)\(i\) \(V\)] ,每一个操作也可以看成一个矩阵
对于操作1:
\[\begin{equation} \left[ \begin{array}{cccc} 1& 0 &0 & 0\\ 1& 1 &0&0\\ 0 &0 &1 &0\\ 0&0 &0&1 \end{array} \right ] \end{equation} \]以此类推,对于操作2:
\[\begin{equation}\left[ \begin{array}{cccc} 1& 0 &0 & 0\\ 0& 1 &0&0\\ 0 &1 &1 &0\\ 0&0 &0&1\end{array}\right ]\end{equation} \]那么对于操作3:
\[\begin{equation}\left[ \begin{array}{cccc} 1& 0 &1 & 0\\ 0& 1 &0&0\\ 0 &0 &1 &0\\ 0&0 &0&1\end{array}\right ]\end{equation} \]对于操作4,就需要使用第4维了:
\[\begin{equation}\left[ \begin{array}{cccc} 1& 0 &0 & 0\\ 0& 1 &0&0\\ 0 &0 &1 &0\\ v&0 &0&1\end{array}\right ]\end{equation} \]操作5类似:
\[\begin{equation}\left[ \begin{array}{cccc} 1& 0 &0 & 0\\ 0& 1 &0&0\\ 0 &0 &1 &0\\ 0&0 &v&1\end{array}\right ]\end{equation} \]操作6也就出来了:
\[\begin{equation}\left[ \begin{array}{cccc} 1& 0 &0 & 0\\ 0& 1 &0&0\\ 0 &0 &0 &0\\ 0&0 &v&1\end{array}\right ]\end{equation} \]做到这里,只需要写一颗线段树,维护区间矩阵乘/加即可。
30 Pts 代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct matrix{
int x[4][4];
}g[1000010], tree[1000100], lazy[1000100];
ll n, m, opt, l, r, v, mod = 998244353;
inline ll read(){
ll f = 1, sum = 0; char ch = getchar();
while(ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
while(ch >= '0' && ch <= '9') sum = (sum << 1) + (sum << 3) + ch - '0', ch = getchar();
return f * sum;
}
matrix matrix_add(matrix a, matrix b){
matrix ans;
memset(ans.x, 0, sizeof(ans.x));
for(ll i = 0; i <= 3; i++)
ans.x[0][i] = (a.x[0][i] + b.x[0][i]) % mod;
return ans;
}
matrix matrix_plus(matrix a, matrix b){
matrix ans;
memset(ans.x, 0, sizeof(ans.x));
for(ll i = 0; i <= 3; i++){
for(ll j = 0; j <= 3; j++){
for(ll k = 0; k <= 3; k++){
ans.x[i][j] = (ans.x[i][j] + a.x[i][k] * b.x[k][j] % mod) % mod;
}
}
}
return ans;
}
void pushdown(ll k){
tree[k * 2] = matrix_plus(tree[k * 2], lazy[k]);
tree[k * 2 + 1] = matrix_plus(tree[k * 2 + 1], lazy[k]);
lazy[k * 2] = matrix_plus(lazy[k * 2], lazy[k]);
lazy[k * 2 + 1] = matrix_plus(lazy[k * 2 + 1], lazy[k]);
memset(lazy[k].x, 0, sizeof(lazy[k].x));
lazy[k].x[0][0] = lazy[k].x[1][1] = lazy[k].x[2][2] = lazy[k].x[3][3] = 1;
}
void build(ll k, ll l, ll r){
memset(lazy[k].x, 0, sizeof(lazy[k].x));
lazy[k].x[0][0] = lazy[k].x[1][1] = lazy[k].x[2][2] = lazy[k].x[3][3] = 1;
if(l == r){
tree[k] = g[l];
return;
}
ll mid = (l + r) >> 1;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
tree[k] = matrix_add(tree[k * 2], tree[k * 2 + 1]);
}
void ctree(ll k, ll l, ll r, ll x, ll y, ll vv, ll opt){
if(x <= l && r <= y){
matrix ne;
memset(ne.x, 0, sizeof(ne.x));
ne.x[0][0] = ne.x[1][1] = ne.x[2][2] = ne.x[3][3] = 1;
if(opt == 1) ne.x[1][0] = 1;
else if(opt == 2) ne.x[2][1] = 1;
else if(opt == 3) ne.x[0][2] = 1;
else if(opt == 4) ne.x[3][0] = vv;
else if(opt == 5) ne.x[1][1] = vv;
else if(opt == 6) ne.x[2][2] = 0, ne.x[3][2] = vv;
tree[k] = matrix_plus(tree[k], ne);
lazy[k] = matrix_plus(lazy[k], ne);
return;
}
pushdown(k);
ll mid = (l + r) >> 1;
if(x <= mid) ctree(k * 2, l, mid, x, y, vv, opt);
if(y > mid) ctree(k * 2 + 1, mid + 1, r, x, y, vv, opt);
tree[k] = matrix_add(tree[k * 2], tree[k * 2 + 1]);
}
matrix query(ll k, ll l, ll r, ll x, ll y){
if(x <= l && r <= y) return tree[k];
pushdown(k);
matrix nex;
memset(nex.x, 0, sizeof(nex.x));
ll mid = (l + r) >> 1;
if(x <= mid) nex = matrix_add(nex, query(k * 2, l, mid, x, y));
if(y > mid) nex = matrix_add(nex, query(k * 2 + 1, mid + 1, r, x, y));
return nex;
}
int main(){
n = read();
for(ll i = 1; i <= n; i++){
for(ll j = 0; j <= 2; j++) g[i].x[0][j] = read();
g[i].x[0][3] = 1;
}
build(1, 1, n);
m = read();
while(m--){
opt = read(), l = read(), r = read();
if(opt == 7){
matrix ans = query(1, 1, n, l, r);
for(ll i = 0; i <= 2; i++) printf("%lld ", ans.x[0][i]);
putchar('\n');
}
else{
if(opt >= 4) v = read();
ctree(1, 1, n, l, r, v, opt);
}
v = 0;
}
return 0;
}
But
因为这份代码的常数极高,你会得到满屏的TLE
至此,拉开了卡常的序幕
First
我们可以发现,取模的常数是极高的,那我们可以将%模数,改成减去模数。
同时,memset
对long long赋值时,所需要的常数是很大的,我们其实可以将long long改成int,在做乘法时强制转换成 long long即可
修改部分代码:
inline int add(int a){
return a > mod ? a - mod : a;
}
荣获45分
Second
接下来,我们现将\(*2\)全部改成了\(<<1\)
很好,快了100毫秒
Third
通过BDFS得知,循环是很耗时的。那么我们可以进行如下优化:
如果两个相乘的数,有一个为0,那么其实就可以不用算了:
修改部分代码:
inline matrix matrix_plus(matrix a, matrix b){
matrix ans;
memset(ans.x, 0, sizeof(ans.x));
for(register int i = 0; i <= 3; i++){
for(register int k = 0; k <= 3; k++){
if(!a.x[i][k]) continue;
for(register int j = 0; j <= 3; j++){
if(!b.x[k][j]) continue; /*相等直接跳过*/
ans.x[i][j] = add(ans.x[i][j] + 1ll * a.x[i][k] * b.x[k][j] % mod);
}
}
}
return ans;
}
。。。。在卡题2h后,我A了...
最后,还有点小优化,就直接看最终AC代码吧:
我觉得我的代码还是非常简洁易懂的吧
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct matrix{
int x[4][4];
}g[1000010], tree[1000100], lazy[1000100];
int n, m, opt, l, r, v, mod = 998244353;
inline int add(int a){
return a > mod ? a - mod : a;
}
inline int read(){
int f = 1, sum = 0; char ch = getchar();
while(ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
while(ch >= '0' && ch <= '9') sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar();
return f * sum;
}
inline matrix matrix_add(matrix a, matrix b){
matrix ans;
memset(ans.x, 0, sizeof(ans.x));
ans.x[0][0] = (a.x[0][0] + b.x[0][0]) % mod;
ans.x[0][1] = (a.x[0][1] + b.x[0][1]) % mod;
ans.x[0][2] = (a.x[0][2] + b.x[0][2]) % mod;
ans.x[0][3] = (a.x[0][3] + b.x[0][3]) % mod;
return ans;
}
inline matrix matrix_plus(matrix a, matrix b){
matrix ans;
memset(ans.x, 0, sizeof(ans.x));
for(register int i = 0; i <= 3; i++){
for(register int k = 0; k <= 3; k++){
if(!a.x[i][k]) continue;
for(register int j = 0; j <= 3; j++){
if(!b.x[k][j]) continue;
ans.x[i][j] = add(ans.x[i][j] + 1ll * a.x[i][k] * b.x[k][j] % mod);
}
}
}
return ans;
}
inline void pushdown(int k){
tree[k << 1] = matrix_plus(tree[k << 1], lazy[k]);
tree[(k << 1) | 1] = matrix_plus(tree[(k << 1) | 1], lazy[k]);
lazy[k << 1] = matrix_plus(lazy[k << 1], lazy[k]);
lazy[(k << 1) | 1] = matrix_plus(lazy[(k << 1) | 1], lazy[k]);
memset(lazy[k].x, 0, sizeof(lazy[k].x));
lazy[k].x[0][0] = lazy[k].x[1][1] = lazy[k].x[2][2] = lazy[k].x[3][3] = 1;
}
inline void build(int k, int l, int r){
memset(lazy[k].x, 0, sizeof(lazy[k].x));
lazy[k].x[0][0] = lazy[k].x[1][1] = lazy[k].x[2][2] = lazy[k].x[3][3] = 1;
if(l == r){
tree[k] = g[l];
return;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build((k << 1) | 1, mid + 1, r);
tree[k] = matrix_add(tree[k << 1], tree[(k << 1) | 1]);
}
inline void ctree(int k, int l, int r, int x, int y, int vv, int opt){
if(x <= l && r <= y){
matrix ne;
memset(ne.x, 0, sizeof(ne.x));
ne.x[0][0] = ne.x[1][1] = ne.x[2][2] = ne.x[3][3] = 1;
if(opt == 1) ne.x[1][0] = 1;
else if(opt == 2) ne.x[2][1] = 1;
else if(opt == 3) ne.x[0][2] = 1;
else if(opt == 4) ne.x[3][0] = vv;
else if(opt == 5) ne.x[1][1] = vv;
else if(opt == 6) ne.x[2][2] = 0, ne.x[3][2] = vv;
tree[k] = matrix_plus(tree[k], ne);
lazy[k] = matrix_plus(lazy[k], ne);
return;
}
pushdown(k);
int mid = (l + r) >> 1;
if(x <= mid) ctree(k << 1, l, mid, x, y, vv, opt);
if(y > mid) ctree((k << 1) | 1, mid + 1, r, x, y, vv, opt);
tree[k] = matrix_add(tree[k << 1], tree[(k << 1) | 1]);
}
inline matrix query(int k, int l, int r, int x, int y){
if(x <= l && r <= y) return tree[k];
pushdown(k);
matrix nex;
memset(nex.x, 0, sizeof(nex.x));
int mid = (l + r) >> 1;
if(x <= mid) nex = matrix_add(nex, query(k << 1, l, mid, x, y));
if(y > mid) nex = matrix_add(nex, query((k << 1) | 1, mid + 1, r, x, y));
return nex;
}
int main(){
n = read();
for(register int i = 1; i <= n; i++){
g[i].x[0][0] = read(), g[i].x[0][1] = read(), g[i].x[0][2] = read();
g[i].x[0][3] = 1;
}
build(1, 1, n);
m = read();
while(m--){
opt = read(), l = read(), r = read();
if(opt == 7){
matrix ans = query(1, 1, n, l, r);
printf("%d %d %d\n", ans.x[0][0], ans.x[0][1], ans.x[0][2]);
}
else{
if(opt >= 4) v = read();
ctree(1, 1, n, l, r, v, opt);
}
}
return 0;
}
都看到这了,那就。。。