A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 112228   Accepted: 34905
Case Time Limit: 2000MS

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
  5.  
  6. 题意就是如题,对线段树的区间进行更新,可增可减可修改,并查询区间和,此题就是增,具体实现看代码吧.
    AC代码:
  1. Source Code
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. #define maxsize 100005
  8. #define LL long long
  9. LL num[maxsize];
  10. struct node
  11. {
  12. LL l,r;
  13. LL maxn,add;
  14. int mid()
  15. {
  16. return (l+r)/;
  17. }
  18. } tree[maxsize<<];
  19. void Build_Tree(LL root,LL l,LL r)
  20. {
  21. tree[root].l=l;
  22. tree[root].r=r;
  23. tree[root].add=;
  24. if(l==r)
  25. {
  26. tree[root].maxn=num[l];
  27. return ;
  28. }
  29. Build_Tree(root*,l,tree[root].mid());
  30. Build_Tree(root*+,tree[root].mid()+,r);
  31. tree[root].maxn=tree[root*].maxn+tree[root*+].maxn;
  32. }
  33. void Update(LL root,LL l,LL r,LL czp)
  34. {
  35. if(tree[root].l==l&&tree[root].r==r)
  36. {
  37. tree[root].add+=czp;
  38. return ;
  39. }
  40. tree[root].maxn+=((r-l+)*czp);
  41. if(tree[root].mid()>=r) Update(root<<,l,r,czp);
  42. else if(tree[root].mid()<l) Update(root<<|,l,r,czp);
  43. else
  44. {
  45. Update(root<<,l,tree[root].mid(),czp);
  46. Update(root<<|,tree[root].mid()+,r,czp);
  47. }
  48. }
  49. long long Query(LL root,LL l,LL r)
  50. {
  51. if(tree[root].l==l&&tree[root].r==r)
  52. {
  53. return tree[root].maxn+(r-l+)*tree[root].add;
  54. }
  55. tree[root].maxn+=(tree[root].r-tree[root].l+)*tree[root].add;
  56. Update(root<<,tree[root].l,tree[root].mid(),tree[root].add);
  57. Update(root<<|,tree[root].mid()+,tree[root].r,tree[root].add);
  58. tree[root].add=;
  59. if(tree[root].mid()>=r) return Query(root*,l,r);
  60. else if(tree[root].mid()<l) return Query(root*+,l,r);
  61. else
  62. {
  63. LL Lans=Query(root*,l,tree[root].mid());
  64. LL Rans=Query(root*+,tree[root].mid()+,r);
  65. return Lans+Rans;
  66. }
  67. }
  68. int main()
  69. {
  70. /*ios::sync_with_stdio(false);*/
  71. char str[];
  72. LL m,n,a,b,c;
  73. while(scanf("%lld%lld",&m,&n)!=EOF)
  74. {
  75. for(LL i=; i<=m; i++) scanf("%I64d",&num[i]);
  76. Build_Tree(,,m);
  77. for(LL i=; i<=n; i++)
  78. {
  79. scanf("%s%I64d%I64d",str,&a,&b);
  80. if(str[]=='Q') printf("%I64d\n",Query(,a,b));
  81. else if(str[]=='C')
  82. {
  83. scanf("%I64d",&c);
  84. Update(,a,b,c);
  85. }
  86. }
  87. }
  88. return ;
  89. }
  1.  

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

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

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. PAT 1052 卖个萌 (20)(代码+思路)

    1052 卖个萌 (20)(20 分) 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们假设一个表情符号是按下列格 ...

  2. linux 操作笔记

    1.设置防火墙,允许用户使用http访问本机 [root@localhost geoserver]# systemctl enable httpdCreated symlink from /etc/s ...

  3. array_column()提取二维数组中某个值

    <?php $multipleCommodity = array( =>array(), =>array() ); $arr1=array_column($multipleCommo ...

  4. 摹客项目在2018年工信部"创客中国"名列10强并荣获二等奖

    2018“创客中国”互联网+大数据创新创业大赛(暨2018创客中国产业投资峰会)8月19日在厦门进行了总决赛.大赛由国家工业和信息化部.厦门市人民政府主办,厦门文广集团等承办.工信部信息中心领导.厦门 ...

  5. 《JavaScript高级程序设计》笔记

    1. 当在函数内部定义了其他函数时,就创建了闭包.闭包有权访问包含函数内部的所有变量. 2. 闭包可以分隔变量空间,不会占用全局空间而造成相互间的干拢.使用闭包可以在JavaScript中模仿块级作用 ...

  6. DevExpress VCL 13.1.2 发布

    DevExpress VCL 的2013 第一个公开版发布, 基本上就是一些维护,没有大的变化,也没有FM 的支持. What's New in DevExpress VCL 13.1.2   Rel ...

  7. 46 What Is Real Happiness ? 什么是真正的幸福 ?

    46 What Is Real Happiness ? 什么是真正的幸福 ? ①The way people hold to the belief that a fun-filled, pain-fr ...

  8. hbase使用MapReduce操作3(实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中)

    Runner类 实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中. package com.yjsj.hbase_mr; import org.apache.hadoo ...

  9. hdu 4888 最大流慢板

    http://acm.hdu.edu.cn/showproblem.php?pid=4888 添加一个源点与汇点,建图如下: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对 ...

  10. The MATLAB Profiler

    function a = myFunc(a,b,c,d,e) : a = a-b + c^d*log(e); end end >> profile on; myFunc(a,b,c,d,e ...