You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

  1. 10 5
  2. 1 2 3 4 5 6 7 8 9 10
  3. Q 4 4
  4. Q 1 10
  5. Q 2 4
  6. C 3 6 3
  7. Q 2 4

Sample Output

  1. 4
  2. 55
  3. 9
  4. 15
  5.  
  6. 区间修改 区间查询 注意pushdown的操作就可以了
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <stack>
  14. using namespace std;
  15. #define FO freopen("in.txt","r",stdin);
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  18. #define pb push_back
  19. #define mp make_pair
  20. #define all(x) (x).begin(),(x).end()
  21. #define fi first
  22. #define se second
  23. #define SZ(x) ((int)(x).size())
  24. #define debug(x) cout << "&&" << x << "&&" << endl;
  25. #define lowbit(x) (x&-x)
  26. #define mem(a,b) memset(a, b, sizeof(a));
  27. typedef vector<int> VI;
  28. typedef long long ll;
  29. typedef pair<int,int> PII;
  30. const ll mod=;
  31. const int inf = 0x3f3f3f3f;
  32. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  33. ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
  34. //head
  35.  
  36. //区间修改 区间查询
  37. const int maxx=;
  38. ll sum[maxx<<],lazy[maxx<<],val;
  39. int n,q;
  40.  
  41. void Pushup(int rt) {
  42. sum[rt]=sum[rt<<]+sum[rt<<|];
  43. }
  44.  
  45. void build(int rt,int L,int R) {
  46. lazy[rt]=;
  47. if(L==R) {
  48. scanf("%lld",&sum[rt]);
  49. return;
  50. }
  51. int mid=(L+R)>>;
  52. build(rt<<,L,mid);
  53. build(rt<<|,mid+,R);
  54. Pushup(rt);
  55. }
  56.  
  57. void Pushdown(int rt,int x) {
  58. if(lazy[rt]) {
  59. lazy[rt<<]+=lazy[rt];
  60. lazy[rt<<|]+=lazy[rt];
  61. sum[rt<<]+=(x-(x>>))*lazy[rt];//左子树加左边一半的值
  62. sum[rt<<|]+=(x>>)*lazy[rt];//右子树同理
  63. lazy[rt]=;//清空
  64. }
  65. }
  66.  
  67. void Updata(int rt,int L,int R,int l,int r) {
  68. if(L>=l&&R<=r) {
  69. lazy[rt]+=val;//累加标记
  70. sum[rt]+=(R-L+)*val;//更新值
  71. return;
  72. }
  73. int mid=(L+R)>>;
  74. Pushdown(rt,R-L+);//这里多了一个参数 L R区间的个数
  75. if(l<=mid) Updata(rt<<,L,mid,l,r);
  76. if(r>mid) Updata(rt<<|,mid+,R,l,r);
  77. Pushup(rt);
  78. }
  79.  
  80. ll Query(int rt,int L,int R,int l,int r) {
  81. if(L>=l&&R<=r)
  82. return sum[rt];
  83. ll ans=;
  84. int mid=(L+R)>>;
  85. Pushdown(rt,R-L+);
  86. if(l<=mid) ans+=Query(rt<<,L,mid,l,r);
  87. if(r>mid) ans+=Query(rt<<|,mid+,R,l,r);
  88. Pushup(rt);
  89. return ans;
  90. }
  91.  
  92. int main() {
  93. while(~scanf("%d%d",&n,&q)) {
  94. build(,,n);
  95. char s[];
  96. while(q--) {
  97. scanf("%s",s);
  98. int l,r;
  99. if(s[]=='Q') {
  100. scanf("%d%d",&l,&r);
  101. printf("%lld\n",Query(,,n,l,r));
  102. } else {
  103. scanf("%d%d%lld",&l,&r,&val);
  104. Updata(,,n,l,r);
  105. }
  106. }
  107. }
  108. }

借此机会学习了一波树状数组,有点难理解。

单点修改 区间查询  Updata(l,val)

区间修改 单点查询  引入差分数组 c[i]=d[i]-d[i-1]; Updata(l,val);  Updata(r+1,-val); ----明白树状数组就好理解了。把多加的减去

区间修改 区间查询  公式可以推到一下,sum[n]=n*(c[1]+c[2]+...c[n])-(0*c[1]+1*c[2]+2*c[3]+...+(n-1)*c[n]);

所以再维护一个 (i-1)*(a[i]-a[i-1]) , a为原数组。

Updata(l,(l-1)*val); Updata(r+1,-r*val);   原理同上。多加的减去。

(纯属个人理解。关键还是公式的推导,和树状数组的理解)

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. #define lowbit(x) (x&-x)
  7.  
  8. int n,q;
  9. const int maxn=;
  10. ll a[maxn],val,c1[maxn],c2[maxn];//维护两个树状数组
  11.  
  12. void Updata(ll c[],int pos,ll val) {
  13. while(pos<=n) {
  14. c[pos]+=val;
  15. pos+=lowbit(pos);
  16. }
  17. }
  18.  
  19. ll getsum(ll c[],int pos) {
  20. ll ans=;
  21. while(pos>) {
  22. ans+=c[pos];
  23. pos-=lowbit(pos);
  24. }
  25. return ans;
  26. }
  27.  
  28. ll sigma(int r) {//公式推导
  29. ll sum1=r*getsum(c1,r);
  30. ll sum2=getsum(c2,r);
  31. return sum1-sum2;
  32. }
  33.  
  34. ll Query(int l,int r) {//区间求和
  35. return sigma(r)-sigma(l-);
  36. }
  37.  
  38. int main() {
  39. while(~scanf("%d%d",&n,&q)) {
  40. memset(c1,,sizeof(c1));memset(c2,,sizeof(c2));
  41. for(int i=;i<=n;i++) {
  42. scanf("%lld",&a[i]);
  43. Updata(c1,i,a[i]-a[i-]);//维护a[i]-a[i-1]
  44. Updata(c2,i,(i-)*(a[i]-a[i-]));//维护(i-1)*(a[i]-a[i-1])
  45. }
  46. char s[];
  47. while(q--) {
  48. int l,r;
  49. scanf("%s",s);
  50. if(s[]=='Q') {
  51. scanf("%d%d",&l,&r);
  52. printf("%lld\n",Query(l,r));
  53. } else {
  54. scanf("%d%d%lld",&l,&r,&val);
  55. //更新操作有点难理解
  56. Updata(c1,l,val);Updata(c1,r+,-val);
  57. Updata(c2,l,(l-)*val);Updata(c2,r+,-r*val);
  58. }
  59. }
  60. }
  61. }

kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  3. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  4. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  5. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  6. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  7. poj------(3468)A Simple Problem with Integers(区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...

  8. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   ...

  9. [poj3468]A Simple Problem with Integers_线段树

    A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...

随机推荐

  1. Vue指令学习

    # new Vue({ vue所有的数据都是放到data里面的 # data:{ vue对象的数据 # a:1,对象 # b:[] , # } # methods:{vue对象的方法 # dosomt ...

  2. Linux 正文处理命令及tar vi 编辑器 homework

    作业一: 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group >/1.txt 2) 将用户信息数据库文件和用 ...

  3. jQuery的AJax异步加载

    主要用到load()方法以及getScript()方法,具体以一个例子说明: 在现有html文件中加载一个拟好的片段,以及在片段加载完成之前阻止用户进一步操作的弹出框. 首先是现有html代码,无任何 ...

  4. Velocity根据模版生成静态html

    新公司的一个CMS项目要用到,这里记录下 一.项目文件图 二.springmvc-servlet.xml 添加 <!-- 定义环境变量文件 --> <bean id="pr ...

  5. apache2不识别php

    sudo apt-get install libapache2-mod-php7.0 sudo a2enmod php7.0 sudo service apache2 restart 注意:Apach ...

  6. HTML5的头部、拨号、短信、邮件(转)

    HTML5[语法要点] 一.头部设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!--页面窗口自动调整到设备 ...

  7. ROS探索总结(五)——创建简单的机器人模型smartcar

    前面我们使用的是已有的机器人模型进行仿真,这一节我们将建立一个简单的智能车机器人smartcar,为后面建立复杂机器人打下基础. 一.创建硬件描述包 roscreat-pkg  smartcar_de ...

  8. Blender 软件 四视图布局的创建方法

    Blender 软件 四视图布局的创建方法 我使用的Blender版本:Blender V 2.77 Q: 为什么要创建一个:四视图布局场景? A: 原因1:四视图布局最适合模型建模.原因2:在 Bl ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  10. JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

    一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...