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} \rceil $ 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} \rceil $ 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} \rceil ) $ — 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 \times B + A \times C = A \times (B+C) $

  • 在求斐波那契数列中,是 $ A \times 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\n",(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
*/

转载于:https://www.cnblogs.com/PotremZ/p/CF718C.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值