Codeforces Round #373 (Div. 2) E. Sasha and Array
来源:互联网 发布:linux 统计登录次数 编辑:程序博客网 时间:2024/05/01 05:56
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 to r 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.
【题意】给你n个数,两个操作,1是区间增加x,2是查询区间fib(a[i])的和
【解题方法】参考题解http://www.cnblogs.com/qscqesze/p/5902547.html
【AC 代码】
////Filename E. Sasha and Array//Created by just_sort 2016/9/27 19:12//Copyright (c) 2016 just_sort.All Rights Reserved//#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;struct FastIO{ static const int S = 1310720; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int xchar() { static char buf[S]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, S, stdin); if (pos == len) return -1; return buf[pos ++]; } inline int xuint() { int c = xchar(), x = 0; while (c <= 32) c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x; } inline int xint() { int s = 1, c = xchar(), x = 0; while (c <= 32) c = xchar(); if (c == '-') s = -1, c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x * s; } inline void xstring(char *s) { int c = xchar(); while (c <= 32) c = xchar(); for (; c > 32; c = xchar()) * s++ = c; *s = 0; } inline void wchar(int x) { if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0; wbuf[wpos ++] = x; } inline void wchar(ll x) { if (x < 0) wchar('-'), x = -x; char s[60]; int n = 0; while (x || !n) s[n ++] = '0' + x % 10, x /= 10; while (n--) wchar(s[n]); } inline void wint(int x) { if (x < 0) wchar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n ++] = '0' + x % 10, x /= 10; while (n--) wchar(s[n]); } inline void wstring(const char *s) { while (*s) wchar(*s++); } ~FastIO() { if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0; }} io;const int mod = 1e9+7;const int maxn = 1e5+7;struct node{ ll a[2][2]; void init(){ memset(a,0,sizeof(a)); } void one(){ init(); a[0][0] = a[1][1] = 1; }};node add(node A,node B){ node C; C.init(); for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ C.a[i][j] = (A.a[i][j] + B.a[i][j]) % mod; } } return C;}node mul(node A,node B){ node C; C.init(); for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ C.a[i][j] = (C.a[i][j] + A.a[i][k]*B.a[k][j]) % mod; } } } return C;}node pow(int p){ node A; A.a[0][0] = 0; A.a[0][1] = 1; A.a[1][0] = 1; A.a[1][1] = 1; node res; res.one(); while(p){ if(p%2) res = mul(res,A); A = mul(A,A); p >>= 1; } return res;}int a[maxn];struct SegmentTree{ int l,r,flag; node sum,lazy; void update(node v) { sum = mul(sum,v); lazy = mul(lazy,v); flag = 1; }}Tree[maxn<<2];void pushup(int rt){ Tree[rt].sum = add(Tree[rt*2].sum,Tree[rt*2+1].sum);}void pushdown(int rt){ if(Tree[rt].flag) { Tree[rt*2].update(Tree[rt].lazy); Tree[rt*2+1].update(Tree[rt].lazy); Tree[rt].flag = 0; Tree[rt].lazy.one(); }}void Build(int l,int r,int rt){ Tree[rt].l = l,Tree[rt].r = r,Tree[rt].sum.init(),Tree[rt].lazy.one(),Tree[rt].flag = 0; if(l == r){ Tree[rt].sum = pow(a[l]); return ; } int mid = (l+r)/2; Build(l,mid,rt*2); Build(mid+1,r,rt*2+1); pushup(rt);}void update(int L,int R,node v,int rt){ if(L <= Tree[rt].l && Tree[rt].r <= R){ Tree[rt].update(v); return ; } else{ pushdown(rt); int mid = (Tree[rt].l + Tree[rt].r)/2; if(L <= mid) update(L,R,v,rt*2); if(mid < R) update(L,R,v,rt*2+1); pushup(rt); }}node queryans(int L,int R,int rt){ if(L <= Tree[rt].l && Tree[rt].r <= R){ return Tree[rt].sum; } else{ pushdown(rt); int mid = (Tree[rt].l + Tree[rt].r)/2; node res; res.init(); if(L <= mid) res = add(res,queryans(L,R,rt*2)); if(mid < R) res = add(res,queryans(L,R,rt*2+1)); pushup(rt); return res; }}int n,q;int main(){ n = io.xint(); q = io.xuint(); for(int i = 1; i <= n; i++) a[i] = io.xint(); Build(1,n,1); for(int i = 1; i <= q; i++){ int op; op = io.xint(); if(op == 2){ int l,r; l = io.xint(); r = io.xint(); printf("%I64d\n",queryans(l,r,1).a[1][0]); } else{ int l,r,v; l = io.xint(); r = io.xint(); v = io.xint(); update(l,r,pow(v),1); } } return 0;}
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
- Codeforces Round #373 (Div. 1) C.Sasha and Array
- [Codeforces Round #373 DIV1C (CF718C)] Sasha and Array
- Codeforces Round #425 (Div. 2)A. Sasha and Sticks
- Codeforces Round #425 (Div. 2) A.Sasha and Sticks
- Codeforces Round #425 (Div. 2) A. Sasha and Sticks
- Codeforces Round #425 (Div. 2) A. Sasha and Sticks
- cf/Codeforces Round #373 div1-C/div2-E Sasha and Array 线段树 + 维护矩阵快速幂
- Codeforces Round #253 (Div. 2) E. Artem and Array (思维)
- Codeforces Round #163 (Div. 2) E. More Queries to Array...
- codeforces 373div1 Sasha and Array 矩阵+线段树
- Codeforces 718C C. Sasha and Array
- codeforces 718 C.Sasha and Array
- codeforces 718C - Sasha and Array
- [题解]codeforces 718c Sasha and Array
- [Codeforces 719 E. Sasha and Array] 矩阵快速幂+线段树
- 常见的编译错误
- Android 从源码角度分析消息处理机制(Handler,Looper,Message)
- 单链表-用尾插法创建单链表
- msysGit 安装后的配置
- VS2008 F5或Debuging Run 重新编译整个工程
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- Sql基础练习03
- ubuntu下php5.6安装pcntl扩展
- 去哪儿网笔试题分析
- 编写saltstack 扩展模块
- 树形dp学习
- myeclipse 怎么安装SVN插件
- JSTL标签库与自定义标签
- 2016年头条校招笔试题