• A Simple Problem with Integers (线段树模板)


     3468 -- A Simple Problem with Integers

    A Simple Problem with Integers

    Time Limit: 5000MSMemory Limit: 131072K
    Total Submissions: 204955Accepted: 63007
    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 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

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    

    Sample Output

    4
    55
    9
    15
    
    1. #include
    2. #include
    3. using namespace std;
    4. typedef long long ll;
    5. const int inf = 100010;
    6. int n , m ;
    7. ll ly[inf<<2] , tree[inf<<2] ;
    8. //用longlong类型,数较大时相加的和较大
    9. //tree[] 用来实现完全二叉树 需要四倍空间
    10. void pushup(int u)
    11. {
    12. tree[u] = tree[u<<1] + tree[u<<1 | 1]; //更新结点 左右儿子的和
    13. }
    14. void buildtree(int l , int r , int u ) //用完全二叉树建一个线段树
    15. {
    16. //从根开始往下建树
    17. ly[u]=0;
    18. if( l == r ) //最底层叶子
    19. {
    20. cin>>tree[u];
    21. return ;
    22. }
    23. int mid = (l + r) >>1 ;
    24. buildtree(l , mid , u<<1); //左子树
    25. //右子树
    26. buildtree(mid + 1 , r , u<<1 | 1); //u<<1|1 <--> u*2+1 位运算计算更快
    27. //更新父节点
    28. pushup(u);
    29. }
    30. void pushdown(int u,int len)//下传lazy
    31. {
    32. if(ly[u]){
    33. ly[u<<1] += ly[u];//父节点lazy下传到左儿子
    34. ly[u<<1 | 1] += ly[u];//到右儿子
    35. tree[u<<1] += (len - (len>>1)) * ly[u]; //左根节点加上(儿子每个数加上c的和)
    36. tree[u<<1 | 1] += (len>>1) * ly[u];
    37. ly[u] = 0;
    38. }
    39. }
    40. void update (int a , int b , int c , int l , int r , int u)//更新区间
    41. {
    42. int len = r - l + 1;
    43. if( a <= l && b >= r){//区间已覆盖,直接计算
    44. tree[u] += len* c;
    45. ly[u] += c; //lazy标记加上c
    46. return ;
    47. }
    48. pushdown(u , len);//lazy下传
    49. int mid = (l + r) >>1 ;
    50. if(mid >= a) update(a , b , c , l , mid , u<<1);//更新左子树
    51. if(mid < b) update(a , b , c , mid+1 , r , u<<1 | 1);//更新右子树
    52. pushup(u);//更新父节点
    53. }
    54. ll query(int a, int b, int l, int r, int u)//计算区间和
    55. {
    56. if(a <= l && b >= r) return tree[u];//满足lazy , 直接返回值
    57. int len = r - l + 1;
    58. pushdown(u , len);//下传
    59. int mid = (l + r) >>1 ;
    60. ll sum =0;
    61. if(mid >= a ) sum += query(a, b, l , mid , u<<1);
    62. if(mid < b) sum += query(a , b , mid+1 , r , u<<1 | 1);
    63. return sum;
    64. }
    65. int main()
    66. {
    67. ios::sync_with_stdio(false);
    68. cin>>n>>m;
    69. buildtree(1,n,1);
    70. while(m--)
    71. {
    72. char str;
    73. int a , b , c;
    74. cin>>str;
    75. if(str == 'C'){
    76. cin>>a>>b>>c;
    77. update(a,b,c,1,n,1);
    78. }else if(str == 'Q'){
    79. cin>>a>>b;
    80. cout<<query(a,b,1,n,1)<
    81. }
    82. }
    83. return 0;
    84. }
  • 相关阅读:
    【Leetcode】189. 轮转数组
    关于linux系统can收发,以及jetson系列can收发的说明,以及SN65HVD230 CAN board和MCP2515和TJA1050的区别是什么?
    《痞子衡嵌入式半月刊》 第 62 期
    回溯算法(3)--n皇后问题及回溯法相关习题
    19 | 多线程1
    【LeetCode】54、螺旋矩阵
    基于JAVA水果食品顺溯源系统计算机毕业设计源码+数据库+lw文档+系统+部署
    从Redis分布式缓存实战入手到底层原理分析、覆盖大厂面试考点
    软件测试---产品需求文档测试
    【深入理解java虚拟机】 - JVM垃圾回收器
  • 原文地址:https://blog.csdn.net/weixin_53439401/article/details/126693813