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<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. using namespace std;
  6. #define maxn 100005
  7. long long sum[maxn<<],add[maxn<<];
  8. void pushup(int rt) {
  9. sum[rt]=sum[rt<<]+sum[rt<<|];
  10. }
  11. void pushdown(int rt ,int l) {
  12. if (add[rt]) {
  13. add[rt<<]+=add[rt];
  14. add[rt<<|]+=add[rt];
  15. sum[rt<<]+=add[rt]*(l-(l>>));
  16. sum[rt<<|]+=add[rt]*(l>>);
  17. add[rt]=;
  18. }
  19. }
  20. void build(int l,int r,int rt) {
  21. if (l==r) {
  22. scanf("%lld",&sum[rt]);
  23. return ;
  24. }
  25. int m=(l+r)>>;
  26. build(l,m,rt<<);
  27. build(m+,r,rt<<|);
  28. pushup(rt);
  29. }
  30. void updata(int x,int y,int z,int l,int r,int rt) {
  31. if (x<=l && r<=y) {
  32. add[rt]+=z;
  33. sum[rt]+=(long long)z*(r-l+);
  34. return ;
  35. }
  36. pushdown(rt,r-l+);
  37. int m=(l+r)>>;
  38. if (x<=m) updata(x,y,z,l,m,rt<<);
  39. if (y>m) updata(x,y,z,m+,r,rt<<|);
  40. pushup(rt);
  41. }
  42. long long query(int x,int y,int l,int r,int rt) {
  43. long long ans=;
  44. if (x<=l && r<=y ) return sum[rt];
  45. pushdown(rt,r-l+);
  46. int m=(l+r)>>;
  47. if (x<=m) ans+=query(x,y,l,m,rt<<);
  48. if (y>m ) ans+=query(x,y,m+,r,rt<<|);
  49. return ans;
  50. }
  51. int main() {
  52. int n,q;
  53. while(scanf("%d%d",&n,&q)!=EOF) {
  54. build(,n,);
  55. char b[];
  56. int x,y,z;
  57. while(q--) {
  58. scanf("%s",b);
  59. if (b[]=='Q') {
  60. scanf("%d%d",&x,&y);
  61. printf("%lld\n",query(x,y,,n,));
  62. } else {
  63. scanf("%d%d%d",&x,&y,&z);
  64. updata(x,y,z,,n,);
  65. }
  66. }
  67. }
  68. return ;
  69. }

A Simple Problem with Integers~POJ - 3468的更多相关文章

  1. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

  2. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

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

    题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...

  4. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  5. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  6. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  7. C - A Simple Problem with Integers

    C - A Simple Problem with Integers POJ - 3468   思路:线段树区间修改区间查询.又出现了 C++ WA    G++ AC的尴尬局面. #include& ...

  8. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

随机推荐

  1. MOBA战斗服务器设计思路

    MOBA作为竞技类的游戏,游戏中实时高精度同步,或者又说延迟容错率的要求还算是比较高的一种. 如何做到这种同步机制呢? 常用的同步机制有两种类型:帧同步 / 指令同步 何谓帧同步? 保证双方客户端逻辑 ...

  2. BZOJ 3198: [Sdoi2013]spring [容斥原理 哈希表]

    3198: [Sdoi2013]spring 题意:n个物品6个属性,求有多少不同的年份i,j满足有k个属性对应相等 一开始读错题了,注意是对应相等 第i个属性只能和第i个属性对应 容斥一下 \[ 恰 ...

  3. Sql2012数据库还原

    Sql2012数据库还原(通过.bak数据库备份文件) 昨天系统挂了,那叫一个悲惨,重装了系统,但是sql2012的数据没有备份,同事帮忙发来备份文件(.bak),开始还原数据. 步骤:1 自己新建一 ...

  4. jira + confluence 安装和破解

    Window环境: 环境准备 安装JAVA1.8以上版本 安装SQL SERVER 或 MySQL: jira安装和破解 下载安装包 https://downloads.atlassian.com/s ...

  5. 利用Lua读写本地文件

    缘由 今天在使用Lua编写脚本时,需要用到读写文件的操作,很久没有使用Lua了,特写下此文来备忘一下. 简介 Lua对文件的操作与C对文件的操作基本一致,不管是参数还是方法.Lua中可以直接通过全局方 ...

  6. 游戏服务器设计之NPC系统

    游戏服务器设计之NPC系统 简介 NPC系统是游戏中非常重要的系统,设计的好坏很大程度上影响游戏的体验.NPC在游戏中有如下作用: 引导玩家体验游戏内容,一般游戏内有很多主线.支线任务,而任务的介绍. ...

  7. error: Autoconf version 2.67 or higher is required

    error: Autoconf version 2.67 or higher is required 今天linux下遇到这种错误,顺便记录下来. #rpm -qf /usr/bin/autoconf ...

  8. php 生成缩略图

    $src = '4.jpg'; list($width,$height) = getimagesize($src); $im = imagecreatefromjpeg($src); $panl = ...

  9. PHP执行Session与前端JS之间的关系

    <?php error_reporting(0); $path = './tmp/'; $sess_name = session_name(); echo $sess_name; $sess_i ...

  10. CentOS 6下编译安装MySQL 5.6

    一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e ...