【题目链接】:http://codeforces.com/contest/719/problem/E

【题意】



给你一个数列,有两种操作1 l r x 给[l,r]区间上的数加上x, 2 l r 询问[l,r]区间fibonacci数列的和(f[l]+f[l+1]+……f[r])

【题解】



斐波那契数列有个矩阵乘法公式

f[n]=

  1. [0 1] ^n× [0 0]
  2. [1 1] [0 1]

最后得到的矩阵A

A[1][2]就是答案;(即第一行第二列)



写个线段树的成段更新;

用懒惰标记记录加上的数字x对应的A^x

维护区间的矩阵和就好;

新增加的A^x不要每次都重新算,不然会T





【Number Of WA】



9



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. typedef pair<int,int> pii;
  14. typedef pair<LL,LL> pll;
  15. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  16. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  17. const double pi = acos(-1.0);
  18. const int N = 1e5+100;
  19. const int G = 2; //�����С
  20. const int MOD = 1e9 + 7; //ģ��
  21. struct MX
  22. {
  23. LL v[G+1][G+1];
  24. void O() { ms(v, 0); }
  25. void E() { ms(v, 0); for (int i = 1; i <= G; ++i)v[i][i] = 1; }
  26. MX operator * (const MX &b) const
  27. {
  28. MX c; c.O();
  29. for (int k = 1; k <= G; ++k)
  30. {
  31. for (int i = 1; i <= G; ++i) if (v[i][k])
  32. {
  33. for (int j = 1; j <= G; ++j)
  34. {
  35. c.v[i][j] = (c.v[i][j] + (LL)v[i][k] * b.v[k][j]) % MOD;
  36. }
  37. }
  38. }
  39. return c;
  40. }
  41. MX operator + (const MX &b) const
  42. {
  43. MX c; c.O();
  44. for (int i = 1; i <= G; ++i)
  45. {
  46. for (int j = 1; j <= G; ++j)
  47. {
  48. c.v[i][j] = (v[i][j] + b.v[i][j]) % MOD;
  49. }
  50. }
  51. return c;
  52. }
  53. MX operator ^ (LL p) const
  54. {
  55. MX y; y.E();
  56. MX x; memcpy(x.v, v, sizeof(v));
  57. while (p)
  58. {
  59. if (p&1) y = y*x;
  60. x = x*x;
  61. p>>=1;
  62. }
  63. return y;
  64. }
  65. }A,v,cur;
  66. int n,m,flag[N<<2];
  67. MX sum[N<<2],lazy_tag[N<<2];
  68. LL a[N];
  69. inline void push_up(int rt)
  70. {
  71. sum[rt] = sum[rt<<1] + sum[rt<<1|1];
  72. }
  73. void build(int l,int r,int rt)
  74. {
  75. lazy_tag[rt].E();
  76. if (l==r)
  77. {
  78. sum[rt] = A^a[l];
  79. return;
  80. }
  81. int m = (l+r)>>1;
  82. build(lson),build(rson);
  83. push_up(rt);
  84. }
  85. inline void push_down(int rt)
  86. {
  87. if (!flag[rt]) return;
  88. flag[rt<<1] = flag[rt<<1|1] = 1;
  89. flag[rt] = 0;
  90. sum[rt<<1]=sum[rt<<1]*lazy_tag[rt];
  91. sum[rt<<1|1]=sum[rt<<1|1]*lazy_tag[rt];
  92. lazy_tag[rt<<1] = lazy_tag[rt<<1]*lazy_tag[rt];
  93. lazy_tag[rt<<1|1] = lazy_tag[rt<<1|1]*lazy_tag[rt];
  94. lazy_tag[rt].E();
  95. }
  96. void up_data(int L,int R,int x,int l,int r,int rt)
  97. {
  98. if (L<= l && r <= R)
  99. {
  100. lazy_tag[rt]=lazy_tag[rt]*cur;
  101. sum[rt] = sum[rt]*cur;
  102. flag[rt] = 1;
  103. return;
  104. }
  105. push_down(rt);
  106. int m = (l+r)>>1;
  107. if (L <= m) up_data(L,R,x,lson);
  108. if (m < R) up_data(L,R,x,rson);
  109. push_up(rt);
  110. }
  111. MX Q(int L,int R,int l,int r,int rt)
  112. {
  113. if (L <= l && r <= R)
  114. return sum[rt];
  115. push_down(rt);
  116. int m = (l+r)>>1;
  117. MX temp1,temp2;
  118. temp1.O(),temp2.O();
  119. if (L <= m) temp1 = Q(L,R,lson);
  120. if (m < R) temp2 = Q(L,R,rson);
  121. temp1 = temp1 + temp2;
  122. return temp1;
  123. }
  124. LL query(int l,int r)
  125. {
  126. MX temp = Q(l,r,1,n,1);
  127. temp = temp*v;
  128. return temp.v[1][2];
  129. }
  130. int main()
  131. {
  132. //freopen("F:\\rush.txt","r",stdin);
  133. ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
  134. cin >> n >> m;
  135. rep1(i,1,n) cin >> a[i];
  136. A.v[1][1] = 0,A.v[1][2] = A.v[2][1] = A.v[2][2] = 1;
  137. v.v[1][1] = v.v[1][2] = v.v[2][1] = 0,v.v[2][2] = 1;
  138. build(1,n,1);
  139. rep1(i,1,m)
  140. {
  141. int type;
  142. cin >> type;
  143. if (type==1)
  144. {
  145. int l,r,x;
  146. cin >> l >> r >> x;
  147. cur = A^x;
  148. up_data(l,r,x,1,n,1);
  149. }
  150. else
  151. {
  152. int l,r;
  153. cin >> l >> r;
  154. cout << query(l,r) << endl;
  155. }
  156. }
  157. return 0;
  158. }

【codeforces 719E】Sasha and Array的更多相关文章

  1. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 754A】Lesha and array splitting

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【Codeforces 258B】 Sort the Array

    [题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即 ...

  4. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  5. 【44.19%】【codeforces 727C】Guess the Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【Codeforces 1042D】Petya and Array

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 把a[i]处理成前缀和 离散化. 枚举i从1..n假设a[i]是区间和的a[r] 显然我们需要找到a[r]-a[l]<t的l的个数 即a ...

  7. 【codeforces 1109B】Sasha and One More Name

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果这个回文串的左半部分,字母全是一样的. 那么显然不可能再分出来了,因为不管怎么分怎么排列,最后肯定都只能和原串一样. 所以无解 其他情况下 ...

  8. 【Codeforces 1114B】Yet Another Array Partitioning Task

    [链接] 我是链接,点我呀:) [题意] 让你把数组分成k个连续的部分 使得每个部分最大的m个数字的和最大 [题解] 把原数组降序排序 然后选取前m*k个数字打标记 然后对于原数组 一直贪心地取 直到 ...

  9. 【Codeforces 1109C 】Sasha and a Patient Friend

    Codeforces 1109 C 题意:现在有个碗,每时每刻其中的水量都会加一个整数(可以为负). 给\(n\)个询问,询问有\(3\)种类型: \(1\ t\ s\):将从第\(t\)秒开始水量增 ...

随机推荐

  1. ACM-并查集之小希的迷宫——hdu1272

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  2. 写一个类似淘宝的ios app需要用到哪些技术?

    写一个类似淘宝的ios app需要用到哪些技术? 让我想起了有人私信我,说不缺钱,做个类似知乎的东西,包括加运营,需要多少钱. 扯淡结束,正好最近看了一点这方面的东西,也许对题主来说有点帮助. 手机淘 ...

  3. 【Codeforces 258E】 Devu and Flowers

    [题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...

  4. 关于类和对象的进一步讨论 C++

    如果一个类中所有成员函数都是公用的,则可以在定义对象时对数据成员进行初始化: class  Time { public: hour; minute; sec; }; Time t1={14,15,23 ...

  5. [转载]H3C&nbsp;S3600&nbsp;DHCP-SERVER&nbsp;配置【原创】

    原文地址:H3C S3600 DHCP-SERVER 配置[原创]作者:旅行者萧 案例要求: 在H3C S3600-28TP-SI 的vlan 里配置DHCP server,使用vlan 里部分网段为 ...

  6. 【BZOJ2438】【中山市选2011】杀人游戏

    [问题描述] 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面, 查出谁是杀手.  警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是杀手, ...

  7. TreeSet中的排序问题——Comparable

    package com.etc.hashset; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  8. JAVA比较两张图相似度

    代码: package com.uiwho.com; import javax.imageio.*; import java.awt.image.*; import java.awt.*;//Colo ...

  9. CSS浮动相关解决办法

    浮动元素引起的问题: 1. 父元素的一级子元素全都为浮动元素的情况下,父元素的高度无法被撑开,影响与父元素同级的元素 2. 与浮动元素同级的非浮动元素(内容)会跟随其后 3. 若非第一个元素浮动,则该 ...

  10. Android App退出检测

    app的退出检测是很难的,但是获取app“要退出”的状态就容易多了,退出的瞬间并不是真的退出了,ActivityManager要销毁activity,也需要一些时间和资源的. 先见下面的运行效果:  ...