Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of AaAa+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

Hint

The sums may exceed the range of 32-bit integers.

区间求和:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. typedef long long LL;
  7. using namespace std;
  8. #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
  9. #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
  10. #define CLEAR( a , x ) memset ( a , x , sizeof a )
  11. const int maxn=100000;
  12. int num[maxn];
  13. LL sum[maxn<<2],add[maxn<<2];
  14. int N,Q;
  15. void pushup(int rs)
  16. {
  17. sum[rs]=sum[rs<<1]+sum[rs<<1|1];
  18. }
  19. void pushdown(int rs,int l)
  20. {
  21. if(add[rs])
  22. {
  23. add[rs<<1]+=add[rs];
  24. add[rs<<1|1]+=add[rs];
  25. sum[rs<<1]+=add[rs]*(l-(l>>1));
  26. sum[rs<<1|1]+=add[rs]*(l>>1);
  27. add[rs]=0;
  28. }
  29. }
  30. void build(int rs,int l,int r)
  31. {
  32. if(l==r)
  33. {
  34. scanf("%I64d",&sum[rs]);
  35. return ;
  36. }
  37. int mid=(l+r)>>1;
  38. build(rs<<1,l,mid);
  39. build(rs<<1|1,mid+1,r);
  40. pushup(rs);
  41. }
  42. void update(int c,int x,int y,int l,int r,int rs)
  43. {
  44. if(l>=x&&r<=y)
  45. {
  46. add[rs]+=c;
  47. sum[rs]+=(LL)c*(r-l+1);
  48. return ;
  49. }
  50. pushdown(rs,r-l+1);
  51. int mid=(l+r)>>1;
  52. if(x<=mid) update(c,x,y,l,mid,rs<<1);
  53. if(y>mid) update(c,x,y,mid+1,r,rs<<1|1);
  54. pushup(rs);
  55. }
  56. LL query(int x,int y,int l,int r,int rs)
  57. {
  58. if(l>=x&&r<=y)
  59. return sum[rs];
  60. pushdown(rs,r-l+1);
  61. int mid=(l+r)>>1;
  62. LL ans=0;
  63. if(x<=mid) ans+=query(x,y,l,mid,rs<<1);
  64. if(y>mid) ans+=query(x,y,mid+1,r,rs<<1|1);
  65. return ans;
  66. }
  67. int main()
  68. {
  69. int x,y,z;
  70. std::ios::sync_with_stdio(false);
  71. while(~scanf("%d%d",&N,&Q))
  72. {
  73. CLEAR(sum,0);
  74. CLEAR(add,0);
  75. build(1,1,N);
  76. char str[2];
  77. while(Q--)
  78. {
  79. scanf("%s",str);
  80. if(str[0]=='C')
  81. {
  82. scanf("%d%d%d",&x,&y,&z);
  83. update(z,x,y,1,N,1);
  84. }
  85. else
  86. {
  87. scanf("%d%d",&x,&y);
  88. printf("%I64d\n",query(x,y,1,N,1));
  89. }
  90. }
  91. }
  92. return 0;
  93. }

POJ 3468 A Simple Problem with Integers(线段树区间求和)的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  5. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  6. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  10. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

    推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...

  2. Android - 使用Intent来启动Activity

    本文地址: http://blog.csdn.net/caroline_wendy/article/details/21455141 Intent 的用途是 绑定 应用程序组件, 并在应用程序之间进行 ...

  3. kaggle之旧金山犯罪

    kaggle地址 github地址 特点: 离散特征 离散特征二值化处理 数据概览 import pandas as pd import numpy as np # 载入数据 train = pd.r ...

  4. CentOS 6.5下Percona Xtrabackup的安装错误解决方案

    1.下载最新版的Xtracbackup 2.安装 yum install perl-DBIyum install perl-DBD-MySQLyum install perl-Time-HiResyu ...

  5. 面试前的准备---C#知识点回顾----01

    过完年来,准备找份新工作,虽然手里的工作不错,但树挪死,人挪活.咱不能一直在一个坑里生活一辈子,外面的世界毕竟是很美好的. 为了能正常的找到自己中意的工作,最近是将所有的基础知识拿出来复习了一次.仅作 ...

  6. 简单随笔——如何在gridview的页脚显示信息。。。。

    我是超级大菜鸟...哈哈 先上图看看是不是你需要的 第一步,右击gridview,在事件中,单击RowdataBond事件. 在这之前一定要记得在gridview属性中的ShowFooter设置为“t ...

  7. C盘扩容,超详细,史上最简单的扩容技术贴!

    http://ideapad.zol.com.cn/55/160_549015.html 很多朋友跟我一样,转到windows 7 64bit后,发现以前所谓的35GB理论不够用了,哪怕你不把任何程序 ...

  8. c# 异步调用简单例子(转载)

    首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...

  9. 纯Html+Ajax和JSP两者对比的个人理解

    最近写个人web,用jsp+servlet做,突然想到一个问题:html+ajax似乎和jsp实现效果一样:那么,两者到底有什么区别呢? 这里参考老猿的一段话: 全站ajax会维护大量的js代码,如何 ...

  10. linux常用命令--diff

    diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前的文件> <变动后的文件> ...