2019牛客暑期多校训练营(第二场)E-MAZE(思维-矩阵乘法-线段树)

链接:https://ac.nowcoder.com/acm/contest/882/E
来源:牛客网

 

2019牛客暑期多校训练营(第二场)E-MAZE(思维-矩阵乘法-线段树)_第1张图片

输出描述:

For each qi=2, Output one line containing an integer representing the answer module 109+7(1000000007).

示例1

输入

2 2 3
00
00
2 1 2
1 1 2
2 1 2

输出

2
1

题目分析:

转载来自:Wisdom+.+

2019牛客暑期多校训练营(第二场)E-MAZE(思维-矩阵乘法-线段树)_第2张图片

代码: 

#include
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int MAXN = 50005;

typedef vector vec;
typedef vector mat;
mat mul(const mat& A, const mat& B) {
    mat C(A.size(), vec(B[0].size()));
    for(int i = 0; i < A.size(); i++)
        for(int k = 0; k < B.size(); k++)
            if(A[i][k])
                for(int j = 0; j < B[0].size(); j++)
                    C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
    return C;
}
mat Pow(mat A, ll n) {
    mat B(A.size(), vec(A.size()));
    for(int i = 0; i < A.size(); i++)
        B[i][i] = 1;
    for(; n; n >>= 1, A = mul(A, A))
        if(n & 1)
            B = mul(B, A);
    return B;
}

int n, m, q;
int a[MAXN];
mat base[1024];

mat T[MAXN << 2];
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u<<1|1)
void build(int u, int l, int r) {
    if(l == r) {
        T[u] = base[a[l]];
        return;
    }
    build(ls, l, mid);
    build(rs, mid + 1, r);
    T[u] = mul(T[ls], T[rs]);
}

void change(int u, int l, int r, int p, int x) {
    if(l == r) {
        T[u] = base[(a[l] ^= x)]; return;
    }
    if(p <= mid)
        change(ls, l, mid, p, x);
    else
        change(rs, mid + 1, r, p, x);
    T[u] = mul(T[ls], T[rs]);
}

mat query(int u, int l, int r, int L, int R) {
    if(L <= l && r <= R)
        return T[u];
    if(L <= mid && mid < R)
        return mul(query(ls, l, mid, L, R), query(rs, mid + 1, r, L, R));
    if(L <= mid)
        return query(ls, l, mid, L, R);
    else
        return query(rs, mid + 1, r, L, R);
}

void init() {
    for(int k = 0; k < (1 << m); k++) {
        base[k] = mat(m, vec(m, 0));
        for(int i = 0; i < m; i++)
            if(!((k >> i) & 1))
                for(int j = i; !((k >> j) & 1) && j < m; j++)
                    base[k][i][j] = base[k][j][i] = 1;
    }
}

int main() {
    scanf("%d%d%d", &n, &m, &q);
    init();
    for(int i = 1; i <= n; i++) {
        char tmps[15];
        scanf("%s", tmps);
        for(int j = 0; j < m; j++)
            if(tmps[j] == '1')
                a[i] += (1 << j);
    }
    build(1, 1, n);
    int op, x, y;
    while(q--) {
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1) {
            change(1, 1, n, x, 1 << (y - 1));
        } else {
            mat A(1, vec(m));
            A[0][x - 1] = 1;
            mat B = mul(A, T[1]);
            printf("%lld\n", B[0][y - 1]);
        }
    }
    return 0;
}

 

你可能感兴趣的:(矩阵,线段树)