zoukankan      html  css  js  c++  java
  • codeforces CF718C Sasha and Array 线段树维护矩阵

    $ Rightarrow $ 戳我进CF原题

    C. Underground Lab


    time limit per test: 1 second
    memory limit per test: 256 megabytes
    input: standard input
    output: standard output

     

    The evil Bumbershoot corporation produces clones for gruesome experiments in a vast underground lab.
    On one occasion, the corp cloned a boy Andryusha who was smarter than his comrades.
    Immediately Andryusha understood that something fishy was going on there.
    He rallied fellow clones to go on a feud against the evil corp, and they set out to find an exit from the lab.
    The corp had to reduce to destroy the lab complex.
     
    The lab can be pictured as a connected graph with $ n $ vertices and $ m $ edges.
    $ k $ clones of Andryusha start looking for an exit in some of the vertices.
    Each clone can traverse any edge once per second. Any number of clones are allowed to be at any vertex simultaneously.
    Each clone is allowed to stop looking at any time moment, but he must look at his starting vertex at least.
    The exit can be located at any vertex of the lab, hence each vertex must be visited by at least one clone.
     
    Each clone can visit at most $ lceil frac{2n}{k} ceil $ vertices before the lab explodes.
     
    Your task is to choose starting vertices and searching routes for the clones. Each route can have at most $ lceil frac{2n}{k} ceil $ vertices.
     

    Input

    The first line contains three integers $ n, m,$ and $ k (1 ≤ n ≤ 2·10^5, n - 1 ≤ m ≤ 2·10^5, 1 ≤ k ≤ n) $
    — the number of vertices and edges in the lab, and the number of clones.
     
    Each of the next $ m $ lines contains two integers $ x_i $ and $ y_i (1 ≤ x_i, y_i ≤ n) $
    — indices of vertices connected by the respective edge. The graph is allowed to have self-loops and multiple edges.
     
    The graph is guaranteed to be connected.
     

    Output

    You should print $ k $ lines.
    $ i $-th of these lines must start with an integer $ c_i (1 le c_i le lceil frac{2n}{k} ceil ) $ — the number of vertices visited by $ i $-th clone,
    followed by $ c_i $ integers — indices of vertices visited by this clone in the order of visiting.
    You have to print each vertex every time it is visited, regardless if it was visited earlier or not.
     
    It is guaranteed that a valid answer exists.
     

    Examples

    input1

     3 2 1
     2 1
     3 1
    

    output1

     3 2 1 3
    

    input2

     5 4 2
     1 2
     1 3
     1 4
     1 5
    

    output2

     3 2 1 3
     3 4 1 5
    

     

    Note

    In the first sample case there is only one clone who may visit vertices in order $ (2, 1, 3) $ ,
    which fits the constraint of $ 6 $ vertices per clone.
     
    In the second sample case the two clones can visited vertices in order $ (2, 1, 3) $ and $ (4, 1, 5) $ ,
    which fits the constraint of $ 5 $ vertices per clone.
     

    题目大意

    • 维护一个序列,支持两种操作:
    1. 区间 $ [l,r] $ 的权值 $ +x $

    2. 询问区间 $ [l,r] $ 的函数和,即 $ sum fib(x) $ 这里的函数即斐波那契函数

    • 数据范围: $ 1 le n,q le 10^5 $
       

    思路

    • 一般求斐波那契函数的方法可以考虑矩阵乘法,这里也是这样的。

    • 我们不用线段树维护权值,我们用线段树维护区间矩阵和。

    • 有一个矩阵乘法的性质:$ A imes B + A imes C = A imes (B+C) $

    • 在求斐波那契数列中,是 $ A imes F $ ,$ A $ 是变换矩阵, $ F $ 是列矩阵

    • 那么我们用线段树的 $ lazy $ 标记维护 $ A $ 矩阵,然后用 $ sum $ 维护 $ F $ 矩阵

    • 之后在线段树上,就变成了区间更新乘以 $ x $
       

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define int long long
    #define N 100005
    #define Mod 1000000007
    inline int read() {
        register char ch;
        while(!isdigit(ch=getchar()));
        register int x=ch^'0';
        while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
        return x;
    }
    struct Matrix{
    	int x[3][3];
    	inline void clear(){
    		for(int i=1;i<=2;++i)
    			for(int j=1;j<=2;++j)
    				x[i][j]=0;
    	}
    	inline void init(){
    		for(int i=1;i<=2;++i)
    			x[i][i]=1;
    	}
    	inline bool empty(){
    		if(x[1][1]!=1||x[1][2]!=0) return 0;
    		if(x[1][2]!=0||x[2][2]!=1) return 0;
    		return 1;
    	}
    	inline void print(){
            for(int i=1;i<=2;i++){
                for(int j=1;j<=2;j++)
                    printf("%lld ",x[i][j]);
                cout<<endl;
            }
        }
    	inline Matrix operator * (const Matrix &y) const{
    		Matrix c; c.clear();
    		for(int i=1;i<=2;++i)
    			for(int j=1;j<=2;++j)
    				for(int k=1;k<=2;++k)
    					c.x[i][j]=(c.x[i][j]+x[i][k]*y.x[k][j]%Mod)%Mod;
    		return c;
    	}
    	inline Matrix operator + (const Matrix &y) const{
    		Matrix c; c.clear();
    		for(int i=1;i<=2;++i)
    			for(int j=1;j<=2;++j)
    				c.x[i][j]=(x[i][j]+y.x[i][j])%Mod;
    		return c;
    	}
    }st,ss,sum[N<<2],lzy[N<<2];
    Matrix qpow(Matrix x,int k){
    	Matrix res; res.clear(); res.init(); 
    	while(k>0){
    		if(k&1) res=res*x;
    		x=x*x;
    		k>>=1;
    	}
    	return res;
    }
    int n,m;
    void build(int o,int l,int r){
    	lzy[o].init();
    	if(l==r){
    		sum[o]=ss*qpow(st,read()-1);
    		return;
    	}
    	int mid=l+r>>1;
    	build(o<<1,l,mid); build(o<<1|1,mid+1,r);
    	sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    inline void pushdown(int o){
    	sum[o<<1]=sum[o<<1]*lzy[o];
    	sum[o<<1|1]=sum[o<<1|1]*lzy[o];
    	lzy[o<<1]=lzy[o<<1]*lzy[o];
    	lzy[o<<1|1]=lzy[o<<1|1]*lzy[o];
    	lzy[o].clear();
    	lzy[o].init();
    }
    void updata(int o,int l,int r,int L,int R,Matrix k){
    	if(L<=l&&r<=R){
    		sum[o]=sum[o]*k;
    		lzy[o]=lzy[o]*k;
    		return;
    	}
    	if(!lzy[o].empty()) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) updata(o<<1|1,mid+1,r,L,R,k);
    	else if(R<=mid) updata(o<<1,l,mid,L,R,k);
    	else {
    		updata(o<<1,l,mid,L,R,k);
    		updata(o<<1|1,mid+1,r,L,R,k);
    	}
    	sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    Matrix query(int o,int l,int r,int L,int R){
    	if(L<=l&&r<=R) return sum[o];
    	if(!lzy[o].empty()) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) return query(o<<1|1,mid+1,r,L,R);
    	else if(R<=mid) return query(o<<1,l,mid,L,R);
    	else return query(o<<1,l,mid,L,R)+query(o<<1|1,mid+1,r,L,R);
    	sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    signed main(){
    	st.x[1][1]=0; st.x[1][2]=1;
    	st.x[2][1]=1; st.x[2][2]=1;
    	
    	ss.x[1][1]=0; ss.x[1][2]=1;
    	ss.x[1][2]=0; ss.x[2][2]=1;
    	
    	n=read(); m=read();
    	build(1,1,n);
    	while(m--){
    		int opt=read(),L=read(),R=read();
    		if(opt==1){
    			updata(1,1,n,L,R,qpow(st,read()));
    		} 
    		else{
    			Matrix ans=query(1,1,n,L,R);
    			printf("%lld
    ",(ans.x[1][2]+ans.x[2][2])%Mod);
    		}
    	}
    	return 0;
    }
    /*
    #         42721582 
    When      2018-09-10 05:12:59  
    Who       PotremZ 
    Problem   C - Sasha and Array
    Lang      GNU C++11 
    Verdict   Accepted 
    Time      3541 ms
    Memory    56400 KB
    */
    
  • 相关阅读:
    pm2 配置
    添加项目到远程服务器(git)
    psql 命令行使用
    SQL
    iOS AFNetworking 打印从服务器返回的错误提示信息
    iOS 获取网络图片的大小
    iOS 10 常见配置的问题
    LGLTagsView
    xcode8 关闭控制台打印不用信息
    LGLProgressHUD
  • 原文地址:https://www.cnblogs.com/PotremZ/p/CF718C.html
Copyright © 2011-2022 走看看