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
解题思路:利用懒惰标记更新区间并求区间和
代码如下:
  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4.  
  5. const int MAXN = ;
  6. int a[MAXN];
  7. long long int tree[MAXN*];
  8. long long int lazy[MAXN*];
  9. int N,Q;
  10. string s;
  11. int x , y, z;
  12. void push_down(int l ,int r ,int rt)
  13. {
  14. int m = (l+r)/;
  15.  
  16. if(lazy[rt])
  17. {
  18. tree[rt*] += lazy[rt]*(m-l+);
  19. tree[rt*+] += lazy[rt]*(r-m);
  20. lazy[rt*] += lazy[rt];
  21. lazy[rt*+] += lazy[rt];
  22. lazy[rt] = ;
  23. }
  24. }
  25. void bulid_tree(int l ,int r ,int rt)
  26. {
  27. if(l==r)
  28. {
  29. tree[rt] = a[l];
  30. return ;
  31. }
  32. int m = (l+r)/;
  33.  
  34. bulid_tree(l,m,rt*);
  35. bulid_tree(m+,r,rt*+);
  36. tree[rt] = tree[rt*]+tree[rt*+];
  37. }
  38.  
  39. long long int Query(int x ,int y ,int l ,int r ,int rt)
  40. {
  41. long long sum = ;
  42. if(x<=l&&r<=y)
  43. {
  44. return tree[rt];
  45. }
  46. int m = (l+r)/;
  47. push_down(l,r,rt);
  48. if(x<=m)
  49. {
  50. sum += Query(x,y,l,m,rt*);
  51. }
  52. if(m<y)
  53. {
  54. sum += Query(x,y,m+,r,rt*+);
  55. }
  56. return sum;
  57. }
  58. void Update(int x ,int y ,int k ,int l ,int r ,int rt)
  59. {
  60. if(x<=l&&y>=r)
  61. {
  62. tree[rt] += k*(r-l+);
  63. lazy[rt] += k;
  64. return ;
  65. }
  66. push_down(l,r,rt);
  67. int m = (l+r)/;
  68. if(x<=m)
  69. {
  70. Update(x,y,k,l,m,rt*);
  71. }
  72. if(y>m)
  73. {
  74. Update(x,y,k,m+,r,rt*+);
  75. }
  76. tree[rt] = tree[rt*]+tree[rt*+];
  77. }
  78. int main()
  79. {
  80. scanf("%d%d",&N,&Q);
  81. for(int i = ; i <= N;i++)
  82. {
  83. scanf("%d",&a[i]);
  84. }
  85. bulid_tree(,N,);
  86. while(Q--)
  87. {
  88. cin>>s;
  89. if(s[]=='Q')
  90. {
  91. scanf("%d%d",&x,&y);
  92. printf("%lld\n",Query(x,y,,N,));
  93.  
  94. }
  95. else
  96. if(s[]=='C')
  97. {
  98. scanf("%d%d%d",&x,&y,&z);
  99. Update(x,y,z,,N,);
  100. }
  101. }
  102. return ;
  103. }

POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)的更多相关文章

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

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

  2. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

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

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

  4. (简单) 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 ...

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

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

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

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

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

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

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

  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. FMDB是iOS平台的SQLite数据库框架

    1.FMDB简介 什么是FMDBFMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 为什么使用FMDB使用起来更加面向对象,省去了很多麻烦.冗余的C语言 ...

  2. Oracle中REGEXP_SUBSTR函数(字符串转多行)

    Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...

  3. 问题:Oracle 树形遍历;结果:使用oracle进行遍历树操作

    使用oracle进行遍历树操作   1:首先数据库中表必须是树形结构的 2:super_department_id 为 department_id 的父节点编号 3:以下语句的执行结果是:depart ...

  4. linux命令-vim

    vim是vi的升级版 //////////////////////////////////////////////////////////////////////////////// 首先安装vim ...

  5. Android CTS

    1.什么是CTS CTS是google制定的兼容性测试包(Compatibility Test Suite),只有通过CTS测试的设备才有可能获得Android的商标和享受Android Market ...

  6. Android LRUCache

    package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...

  7. ListView---复杂的listview显示

    1 . 初始化数据 private void fillData() { ll_loading.setVisibility(View.VISIBLE); // 显示进度 new Thread() { p ...

  8. day70-oracle PLSQL_01基本语法

    PLSQL是一种程序,和java一样都是一种程序. sql developer是基于java的jdbc连接数据库.根据java的jdbc,只要有数据库的驱动,就可以连接这个数据库.这个工具默认不需要任 ...

  9. Ros问题汇总

    1.ImportError: No module named beginner_tutorials.srv 解决: cd ~/catkin_ws $ source devel/setup.bash $ ...

  10. 一堵墙IFC数据-wall.ifc

    这是一面墙的IFC数据内容 =====================================文档内容======================================= ISO-1 ...