1. 程式人生 > >codeforces CF718C Sasha and Array 線段樹維護矩陣

codeforces CF718C Sasha and Array 線段樹維護矩陣

read ctu his memory long long ORC efi end complex

$ \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
*/

codeforces CF718C Sasha and Array 線段樹維護矩陣