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 <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <list>
  13. #include <iomanip>
  14. #include <cstdlib>
  15. #include <sstream>
  16. using namespace std;
  17. typedef long long LL;
  18. const int INF=0x5fffffff;
  19. const double EXP=1e-;
  20. const int MS=;
  21.  
  22. struct node
  23. {
  24. int l,r;
  25. LL sum,inc; //特别注意这里
  26. int mid()
  27. {
  28. return (l+r)/;
  29. }
  30. }nodes[*MS];
  31.  
  32. void creat(int root,int l,int r)
  33. {
  34. nodes[root].l=l;
  35. nodes[root].r=r;
  36. nodes[root].sum=;
  37. nodes[root].inc=;
  38. if(l==r)
  39. return ;
  40. creat(root<<,l,(l+r)/);
  41. creat(root<<|,(l+r)/+,r);
  42. }
  43.  
  44. void insert(int root,int pos,int value)
  45. {
  46. if(nodes[root].l==nodes[root].r)
  47. {
  48. nodes[root].sum+=value;
  49. return ;
  50. }
  51. nodes[root].sum+=value;
  52. if(pos<=nodes[root].mid())
  53. insert(root<<,pos,value);
  54. else
  55. insert(root<<|,pos,value);
  56. }
  57.  
  58. void add(int root,int a,int b,int c)
  59. {
  60. if(nodes[root].l==a&&nodes[root].r==b)
  61. {
  62. nodes[root].inc+=c;
  63. return ;
  64. }
  65. nodes[root].sum+=c*(b-a+);
  66. if(b<=nodes[root].mid())
  67. add(root<<,a,b,c);
  68. else if(a>nodes[root].mid())
  69. add(root<<|,a,b,c);
  70. else
  71. {
  72. add(root<<,a,nodes[root].mid(),c);
  73. add(root<<|,nodes[root].mid()+,b,c);
  74. }
  75. }
  76.  
  77. LL query(int root,int a,int b)
  78. {
  79. if(nodes[root].l>=a&&nodes[root].r<=b)
  80. return nodes[root].sum+nodes[root].inc*(nodes[root].r-nodes[root].l+);
  81. nodes[root].sum+=nodes[root].inc*(nodes[root].r-nodes[root].l+);
  82. add(root<<,nodes[root].l,nodes[root].mid(),nodes[root].inc);
  83. add(root<<|,nodes[root].mid()+,nodes[root].r,nodes[root].inc);
  84. nodes[root].inc=;
  85. if(b<=nodes[root].mid())
  86. return query(root<<,a,b);
  87. else if(a>nodes[root].mid())
  88. return query(root<<|,a,b);
  89. else
  90. return query(root<<,a,nodes[root].mid())+query(root<<|,nodes[root].mid()+,b);
  91. }
  92.  
  93. int main()
  94. {
  95. int N,Q,x,y,z;
  96. scanf("%d%d",&N,&Q);
  97. creat(,,N);
  98. for(int i=;i<=N;i++)
  99. {
  100. scanf("%d",&x);
  101. insert(,i,x);
  102. }
  103. char cmd[MS];
  104. while(Q--)
  105. {
  106. scanf("%s",cmd);
  107. if(cmd[]=='Q')
  108. {
  109. scanf("%d%d",&x,&y);
  110. printf("%lld\n",query(,x,y));
  111. }
  112. else
  113. {
  114. scanf("%d%d%d",&x,&y,&z);
  115. add(,x,y,z);
  116. }
  117. }
  118. return ;
  119. }
 
 

A Simple Problem with Integers(线段树入门题)的更多相关文章

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

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

  2. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  3. 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 ...

  4. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

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

  6. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

  7. Poj 3468-A Simple Problem with Integers 线段树,树状数组

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

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

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

  9. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

随机推荐

  1. hdu 3038 How Many Answers Are Wrong

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...

  2. Codeforces Round #245 (Div. 1) B. Working out (简单DP)

    题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...

  3. Ubuntu12.04 使用中遇到的问题

    这个随笔回记录使用Ubuntu遇到的一些问题   不定期进行整理和分类 1.Question:   ubuntu 无法检测包或者源码包 Description:Ubuntu软件中心打开时报错  无法检 ...

  4. TypeScript学习笔记(五):接口

    使用接口 在前面的笔记中我们知道可以使用Object Type来指定参数的属性,如下: function printLabel(labelledObj: {label: string}) { cons ...

  5. Spring启动时加载数据

    程序中也许有会有许多常用的,不会经常更改的数据,我们可以在程序初始化的时候就把他们加载,就不用频繁的加载或者查询. 以下是几个常用的,有COPY收集的,也有自己弄. 1. 实现BeanPostProc ...

  6. SQLite本地事务处理

    private void toolStripButton1_Click(object sender, EventArgs e) { //判断新增的年度是否已经存在 if (HasYear()) { M ...

  7. Python魔术师--self

    (原文是 Python's Magical Self ,来自 http://concentricsky.com ) Python的self参数有时真让人抓狂,比如,你必须在每一个类的方法里显示定义se ...

  8. C#不错的扩展工具类

    FSLibExtension.NET https://github.com/iccfish/FSLib.Extension WebEssentials2013 https://github.com/i ...

  9. VIM复制粘贴大全!

    原文地址:http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.entry 内容: 用vim这么久 了,始终也不知道怎么在vim中使 ...

  10. cocos2d-x CCTableView

    转自:http://www.cnblogs.com/dcxing/archive/2013/01/16/2862068.html CCTableView在游戏中一般用在背包这样场景或层中,当然也不止这 ...