线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

Calculate the Function

Problem‘s Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772



Mean:

analyse:

简单的线段树维护矩阵。

矩阵乘法的结合律(a * b * c == a * (b * c)),注意矩阵乘法不满足分配率(a *b != b * a)。

令 M[x] = [1 A[x]]
              [1     0 ] ,
那么有 [ F[R] ] = M[R] * M[R-1] * ... * M[L+2] * [F[L+1]]
          [F[R-1]]                                                   [ F[L] ]

线段树节点维护上边等式右边前n - 1项的乘值(假设等式右边有n项)。每次询问O(log(n))。

Time complexity: O(n*logn)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-25-20.57
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;

const int MAXN = 100100;
const int MOD = 1000000007;
struct Mat
{
    long long m[2][2];
    Mat()
    {
            memset(m, 0, sizeof(m));
    }
    Mat operator * (const Mat &b)
    {
        Mat temp;
        for(int i = 0; i < 2; i++)
            for(int j = 0; j < 2; j++)
                for(int k = 0; k < 2; k++)
                    temp.m[i][j] = ((m[i][k] * b.m[k][j]) + temp.m[i][j]) % MOD;
        return temp;
    }
};

struct Node
{
    int l, r;
    Mat mat;
};
Node node[MAXN << 2];
int a[MAXN];

void Build(int rt, int l, int r)
{
    int m;
    node[rt].l = l;
    node[rt].r = r;
    if(l == r)
    {
        node[rt].mat.m[0][0] = 1;
        node[rt].mat.m[0][1] = a[l];
        node[rt].mat.m[1][0] = 1;
        node[rt].mat.m[1][1] = 0;
    }
    else
    {
        m = (l + r) >> 1;
        Build(rt << 1, l, m);
        Build(rt << 1 | 1, m + 1, r);
        node[rt].mat = node[rt << 1 | 1].mat * node[rt << 1].mat;//注意顺序
    }

}
Mat Query(int rt, int ql, int qr)
{
    int m;
    if(ql == node[rt].l && node[rt].r == qr)
        return node[rt].mat;
    else
    {
        m = (node[rt].l + node[rt].r) >> 1;
        if(qr <= m)
            return Query(rt << 1, ql, qr);
        else if(ql > m)
            return Query(rt << 1 | 1, ql, qr);
        else
            return Query(rt << 1 | 1, m + 1, qr) * Query(rt << 1, ql, m);//注意顺序
    }
}
int T, n, m, ql, qr;

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        Mat res, f;
        scanf("%d%d",&n, &m);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        Build(1, 1, n);
        for(int i = 1; i<= m; i++)
        {
            scanf("%d%d", &ql, &qr);
            if(qr - ql >= 2)
            {
                f.m[0][0] = a[ql + 1];
                f.m[1][0] = a[ql];
                res = Query(1, ql + 2, qr) * f;
                printf("%d\n", res.m[0][0]);
            }
            else
                printf("%d\n", a[qr]);
        }
    }
    return 0;
}

时间: 2024-10-24 14:59:55

线段树 + 矩阵 --- ZOJ 3772 Calculate the Function的相关文章

ZOJ 3772 Calculate the Function 线段树+矩阵

Calculate the FunctionTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-09) Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query

[矩阵+线段树] zoj 3772 Calculate the Function

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters

zoj 3772 Calculate the Function

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为F(X)*A(X+2)+F(X+1)=F(X+2),然后在构造矩阵 {1, A[x]}  {F(x+1)}  {F(X+2)} {1,    0 }*{F(X)    }={F(X+1)} 然后用线段数维护乘号左边的乘积: 1 #include <cstdio> 2 #include <cs

zoj 3772 Calculate the Function(线段树+矩阵乘法)

Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters Li and Ri. The query will define a function Fi(x) on the domain [Li,

ZOJ 3772 Calculate the Function(矩阵线段树)

Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query: The query has two parameters Li and Ri. The query will define a function Fi(x) on the domain [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li + 1) = A(Li + 1) for all x >= Li +

zoj3772【线段树+矩阵相乘】

Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters Li and Ri. The query will define a function Fi(x) on the domain [Li,

Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All

hdu 5068(线段树+矩阵乘法)

矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 326    Accepted Submission(s): 89 Problem Description As we all know, Harry Porter

CF719E(线段树+矩阵快速幂)

题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这个数组 因为矩阵的可加性,所以可以进行lazy操作 我最开始的想法是每个节点lazy表示该区间下标加了多少,add表示该区间已经加的下标对应的矩阵乘积,这样更新lazy是O(1)的,算add是O(logn)的 但是这样每次pushdown的时候,add下传总要多个log,会TLE 更好的办法是laz