A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 97196   Accepted: 30348
Case Time Limit: 2000MS

Description

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

Hint

The sums may exceed the range of 32-bit integers.
题意:
一串数,有两种操作,在区间a,b内每一个数加c,求区间a,b数的和。
线段树模板题。
代码:
  1. #include<iostream>
  2. #include<string>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. int n,q;
  8. long long sum[];//数组多乘4倍。
  9. long long add[];
  10. void pushup(int rt)
  11. {
  12. sum[rt]=sum[rt<<]+sum[rt<<|];
  13. }
  14. void pushdown(int rt,int lne)
  15. {
  16. if(add[rt])
  17. {
  18. add[rt<<]+=add[rt];
  19. add[rt<<|]+=add[rt];
  20. sum[rt<<]+=add[rt]*(lne-(lne>>));
  21. sum[rt<<|]+=add[rt]*(lne>>);
  22. add[rt]=;
  23. }
  24. }
  25. void build(int l,int r,int rt)
  26. {
  27. add[rt]=;
  28. if(l==r)
  29. {
  30. scanf("%lld",&sum[rt]);
  31. return;
  32. }
  33. int mid=(l+r)>>;
  34. build(l,mid,rt<<);
  35. build(mid+,r,rt<<|);
  36. pushup(rt);
  37. }
  38. void update(int L,int R,int c,int l,int r,int rt)
  39. {
  40. if(L<=l&&R>=r)
  41. {
  42. add[rt]+=c;
  43. sum[rt]+=(long long)c*(r-l+);
  44. return;
  45. }
  46. pushdown(rt,r-l+);
  47. int mid=(l+r)>>;
  48. if(L<=mid)
  49. update(L,R,c,l,mid,rt<<);
  50. if(R>mid)
  51. update(L,R,c,mid+,r,rt<<|);
  52. pushup(rt);
  53. }
  54. long long query(int L,int R,int l,int r,int rt)
  55. {
  56. if(L<=l&&R>=r)
  57. {
  58. return sum[rt];
  59. }
  60. pushdown(rt,r-l+);
  61. int mid=(l+r)>>;
  62. long long ans=;
  63. if(L<=mid)
  64. ans+=query(L,R,l,mid,rt<<);
  65. if(R>mid)
  66. ans+=query(L,R,mid+,r,rt<<|);
  67. return ans;
  68. }
  69. int main()
  70. {
  71. int a,b,c;
  72. scanf("%d%d",&n,&q);
  73. build(,n,);
  74. while(q--){
  75. char ch[];
  76. scanf("%s",ch); //不能用scanf("%c".....),字符后有空格,可以scanf一个字符数组/cin。
  77. if(ch[]=='Q')
  78. {
  79. scanf("%d%d",&a,&b);
  80. printf("%lld\n",query(a,b,,n,));
  81. }
  82. else if(ch[]=='C')
  83. {
  84. scanf("%d%d%d",&a,&b,&c);
  85. update(a,b,c,,n,);
  86. }
  87. }
  88. return ;
  89. }

POJ3468 线段树(区间更新,区间求和,延迟标记)的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  6. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  7. HDU 1166 敌兵布阵(线段树点更新区间求和裸题)

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

  8. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  9. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  10. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

随机推荐

  1. loj 1036(dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25913 思路:易证存在一条从左上角到右下角的折线,沿着格子边缘的. ...

  2. HashMap合并相同key的value

    Map<String, String> map1 = new HashMap<>(); map1.put("x", "y"); map1 ...

  3. jvm运行机制与内存管理

    http://blog.csdn.net/lengyuhong/article/details/5953544 http://www.cnblogs.com/nexiyi/p/java_memory_ ...

  4. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  5. 系统表达式(z变换、DTFT、差分方程)之间的关系

  6. express-15 与生产相关的问题

    执行环境 Express支持执行环境的概念,它是一种在生产.开发或测试模式中运行应用程序的方法.实际上你可以按自己的想法创建很多种不同的环境. 要记住,开发.生产和测试是"标准"环 ...

  7. spring 架构学习

    学习目的用于抽象业务逻辑,因spring本身就是抽象业务逻辑的框架,如做业务架构网面的工作 spring为不二之选. 一些好的网址 http://www.ibm.com/developerworks/ ...

  8. appium定位元素java篇【转】

    1.关于没有name,没有ID的元素的定位---通用篇解题思路:因为没有name,id:其实剩下的选择已不多,要么xpath,要么className.xpath木有好印象(稳定性不高,加之1.0x后需 ...

  9. 转载:python发送HTTP请求

    1. [代码]GET 方法 import httplib #----------------------------- conn = httplib.HTTPConnection("www. ...

  10. ajax请求 json格式和数组格式总结

    php echo json_encode($data); $.ajax({ url:APP+"?a=total&c=collection", //请求的页面 type:&q ...