zoukankan      html  css  js  c++  java
  • 2019牛客暑期多校训练营(第二场)E.MAZE(线段树+dp)

    题意:给你一个n*m的矩阵 你只能向左向右相下走 有两种操作 q次询问 一种是把一个单位翻转(即可走变为不可走 不可走变为可走) 另一种是询问从(1,x) 走到 (n,y)有多少种方案

    思路:题目n为1e5 而m只有10 我们可以考虑在线段树上维护一个m*m的矩阵 当模拟矩阵乘法的时候等效于计算方案数(自己手动模拟一下) 修改操作就相当于单点更新 然后重新构造矩阵

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 5e4+7;
    const int inf = 0x3f3f3f3f;
    typedef long long ll;
    const ll mod = 1e9+7;
    ll a[N][12];
    int n,m,q;
    struct matrix{
        int l,r;
        ll ma[12][12];
    };
    matrix t[N<<2];
    void pushup(int p){
        memset(t[p].ma,0,sizeof(t[p].ma));
        for(int i=1;i<=m;i++)
            for(int j=1;j<=m;j++)
                for(int k=1;k<=m;k++){
                    t[p].ma[i][j]=(t[p].ma[i][j]+((t[p<<1].ma[i][k]%mod)*(t[p<<1|1].ma[k][j]%mod))%mod)%mod;   
                }
    }
    void work(int p,int l){
        memset(t[p].ma,0,sizeof(t[p].ma));
        for(int i=1;i<=m;i++){
            int pos=i;
            while(pos>=1&&a[l][pos]==0){
                t[p].ma[i][pos]=1;
                pos--;
            }
            pos=i;
            while(pos<=m&&a[l][pos]==0){
                t[p].ma[i][pos]=1;
                pos++;
            }
        }
    }
    void build(int p,int l,int r){
        t[p].l=l; t[p].r=r;
        if(l==r){
            work(p,l);
            return ;
        }
        int mid=(l+r)>>1;
        build(p<<1,l,mid);
        build(p<<1|1,mid+1,r);
        pushup(p);
    }
    void update(int p,int x){
        if(t[p].l==t[p].r){
            work(p,t[p].l);
            return ;
        }
        int mid=(t[p].l+t[p].r)>>1;
        if(x<=mid) update(p<<1,x);
        else update(p<<1|1,x);
        pushup(p);
    }
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cin>>n>>m>>q;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                char b; cin>>b;
                a[i][j]=(b!='0');
            }
        }   
        build(1,1,n);
        for(int i=1;i<=q;i++){
            int z,x,y; cin>>z>>x>>y;
            if(z==1){
                a[x][y]^=1;
                update(1,x);
            }else{
                cout<<t[1].ma[x][y]<<"
    ";
            }
        }
        return 0;
    }
  • 相关阅读:
    JS 数组总结
    JS 数据类型及其判断
    CSS 优先级
    正则表达式及其使用例子
    常见的图片格式
    React 箭头函数的使用
    手动搭建 react+webpack 开发环境
    JS 函数参数及其传递
    JS 中的 this 指向问题
    JS 中函数的 length 属性
  • 原文地址:https://www.cnblogs.com/wmj6/p/11274192.html
Copyright © 2011-2022 走看看