Codeforces Round 718C (矩阵线段树)
来源:互联网 发布:linux c代码创建目录 编辑:程序博客网 时间:2024/05/22 17:33
Sasha has an array of integers a1, a2, ..., an. You have to performm queries. There might be queries of two types:
- 1 l r x — increase all integers on the segment froml tor by valuesx;
- 2 l r — find , wheref(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo109 + 7.
In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for allx > 2.
Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?
The first line of the input contains two integers n andm (1 ≤ n ≤ 100 000,1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Then follow m lines with queries descriptions. Each of them contains integerstpi,li,ri and may bexi (1 ≤ tpi ≤ 2,1 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Heretpi = 1 corresponds to the queries of the first type andtpi corresponds to the queries of the second type.
It's guaranteed that the input will contains at least one query of the second type.
For each query of the second type print the answer modulo 109 + 7.
5 41 1 2 1 12 1 51 2 4 22 2 42 1 5
579
Initially, array a is equal to 1, 1, 2, 1, 1.
The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5.
After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1.
The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.
The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9.
题意:让你用线段树维护一个下标序列,每次区间加或者查询以这个区间所有数为下标的菲波那切数列和。
分析:线段树中每个节点存放一个矩阵就可以了。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <set>#include <map>#include <vector>#include <stack>#include <cmath>#include <queue>#include <cstring>#define MOD 1000000007#define N 200005using namespace std;typedef long long ll;int T,n,m,k,num,a[N];struct Matrix{ ll val[3][3]; int x ,y; void init() { memset(val,0,sizeof(val)); val[1][1] = val[2][2] = 1; } Matrix(int w,int v) { x = w,y = v; memset(val,0,sizeof(val)); } Matrix(){memset(val,0,sizeof(val));} friend Matrix operator *(const Matrix a,const Matrix b) { Matrix c; c.x = a.x,c.y = b.y; for(int i = 1;i <= a.x;i++) for(int j = 1;j <= b.y;j++) for(int k = 1;k <= b.x;k++) c.val[i][j] = (c.val[i][j] + a.val[i][k]*b.val[k][j]) % MOD; return c; } friend Matrix operator +(const Matrix a,const Matrix b) { Matrix c; c.x = a.x,c.y = b.y; for(int i = 1;i <= a.x;i++) for(int j = 1;j <= b.y;j++) c.val[i][j] = (a.val[i][j] + b.val[i][j]) % MOD; return c; }};Matrix ksm(Matrix a,ll b){ Matrix c = a; b--; while(b) { if(b & 1) c = c * a; a = a * a; b >>= 1; } return c;}struct Segtree{ int l,r; bool down; Matrix dow = Matrix(2,2); Matrix sta = Matrix(2,2);}tree[4*N];void push_up(int i){ tree[i].sta = tree[2*i].sta + tree[2*i+1].sta;}void build(int i,int l,int r){ tree[i].l = l; tree[i].r = r; tree[i].down = 0; tree[i].dow.init(); if(l == r) { Matrix temp = Matrix(2,2); temp.val[1][1] = temp.val[1][2] = temp.val[2][1] = 1; if(a[l] != 1) tree[i].sta = ksm(temp,a[l] - 1); else tree[i].sta.init(); return; } int mid = (l+r)>>1; build(2*i,l,mid); build(2*i+1,mid+1,r); push_up(i);}void push_down(int i){ if(!tree[i].down) return; tree[2*i].down = tree[2*i+1].down = true; tree[2*i].dow = tree[2*i].dow * tree[i].dow; tree[2*i+1].dow = tree[2*i+1].dow * tree[i].dow; tree[2*i].sta = tree[2*i].sta * tree[i].dow; tree[2*i+1].sta = tree[2*i+1].sta * tree[i].dow; tree[i].down = false; tree[i].dow.init();}void deal_add(int i,int x,int y,Matrix val){ int l = tree[i].l,r = tree[i].r; if(l == x && r == y) { tree[i].sta = tree[i].sta * val; tree[i].dow = tree[i].dow * val; tree[i].down = true; return; } push_down(i); int mid = (l+r)/2; if(y <= mid) deal_add(2*i,x,y,val); else if(x <= mid) { deal_add(2*i,x,mid,val); deal_add(2*i+1,mid+1,y,val); } else deal_add(2*i+1,x,y,val); push_up(i);}ll Find(int i,int x,int y){ int l = tree[i].l,r = tree[i].r; if(l == x && r == y) return tree[i].sta.val[1][1]; push_down(i); int mid = (l+r)/2; if(y <= mid) return Find(2*i,x,y); else if(x <= mid) return (Find(2*i,x,mid) + Find(2*i+1,mid+1,y)) % MOD; else return Find(2*i+1,x,y);}int main(){ 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",&k); if(k == 1) { int l,r,x; scanf("%d%d%d",&l,&r,&x); Matrix temp = Matrix(2,2); temp.val[1][1] = temp.val[1][2] = temp.val[2][1] = 1; temp = ksm(temp,x); deal_add(1,l,r,temp); } else { int l,r; scanf("%d%d",&l,&r); printf("%I64d\n",Find(1,l,r)); } }}
- Codeforces Round 718C (矩阵线段树)
- Codeforces Round 718C(维护矩阵的线段树)
- Codeforces Round #373 (Div. 1)C(线段树维护矩阵,矩阵快速幂)
- Codeforces 718C Sasha and Array(线段树维护矩阵)
- cf/Codeforces Round #373 div1-C/div2-E Sasha and Array 线段树 + 维护矩阵快速幂
- 线段树Codeforces Beta Round #99 (Div. 1)C
- Codeforces Round #225 (Div. 1) C 树状数组 || 线段树
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Codeforces Round #316 (Div. 2) C. Replacement(线段树)
- Educational Codeforces Round 7(C)线段树
- Codeforces Round #352 div1 C D (智商+线段树)
- Codeforces Round #225 (Div. 1)-C. Propagating tree(线段树)
- Codeforces Round #413 C. Fountains(RMQ线段树)
- CodeForces 718C Sasha and Array(矩阵类+线段树)
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
- Codeforces Round #318(Div. 1) 573 D. Bear and Cavalry【dp+矩阵+线段树优化】
- CF - 718C 线段树+矩阵
- 线段树Codeforces Round #136 (Div. 2)
- React生成组件类
- hibernate的查询2个方式
- 用Xposed框架抓取微信朋友圈数据
- uml类图(Class Diagram)中类与类之间的关系及表示方式
- Struts2-内置类型转换
- Codeforces Round 718C (矩阵线段树)
- Android Adapter原理解析
- Mybatis用like进行模糊查询问题
- React-Native打包发布
- 使用Flask开发web碰到 MySQL Connection not available
- activemq-all.jar包引入导致slf4j冲突的解决办法
- AbstractFactory_Level4
- Java 面试锦集
- 《Asp.Net Web API》-----webApi的简单使用