题目链接:http://poj.org/problem?id=3468 http://poj.org/problem?id=3468 http://poj.org/problem?id=3468

思路:这是一个区间改动区间查询的题,因为题目中的给的数据比較大,那么用单个改动和查询肯定不行,所以。

。。。注意数据可能比較大,应该用__int64或long long存数据。

。。

code:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #define L(u) (u<<1)
  4. #define R(u) (u<<1|1)
  5.  
  6. const int M=100010;
  7.  
  8. struct Node
  9. {
  10. __int64 l,r;
  11. __int64 add;
  12. long long sum;
  13. }node[M*4];
  14.  
  15. __int64 a[M];
  16.  
  17. void pushup(__int64 u)
  18. {
  19. node[u].sum=node[L(u)].sum+node[R(u)].sum;
  20. return ;
  21. }
  22.  
  23. void pushdown(__int64 u)
  24. {
  25. node[L(u)].add+=node[u].add;
  26. node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add;
  27. node[R(u)].add+=node[u].add;
  28. node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
  29. node[u].add=0;
  30. }
  31.  
  32. void build(__int64 u,__int64 left,__int64 right)
  33. {
  34. node[u].l=left;
  35. node[u].r=right;
  36. node[u].add=0;
  37. if(left==right)
  38. {
  39. node[u].sum=a[left];
  40. return ;
  41. }
  42. __int64 mid=(node[u].l+node[u].r)/2;
  43. build(L(u),left,mid);
  44. build(R(u),mid+1,right);
  45. pushup(u);
  46.  
  47. }
  48.  
  49. void update(__int64 u,__int64 left,__int64 right,__int64 v)
  50. {
  51. if(left<=node[u].l&&node[u].r<=right)
  52. {
  53. node[u].add+=v;
  54. node[u].sum+=(node[u].r-node[u].l+1)*v;
  55. return ;
  56. }
  57. //node[u].sum+=(right-left+1)*v; //当前节点表示的区间不是查询区间的子区间
  58. if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
  59. __int64 mid=(node[u].l+node[u].r)/2;
  60. if(right<=mid) update(L(u),left,right,v);
  61. else if(left>mid) update(R(u),left,right,v);
  62. else
  63. {
  64. update(L(u),left,mid,v);
  65. update(R(u),mid+1,right,v);
  66. }
  67. node[u].sum=node[L(u)].sum+node[R(u)].sum;
  68. }
  69.  
  70. __int64 query(__int64 u,__int64 left,__int64 right)
  71. {
  72. if(left<=node[u].l&&node[u].r<=right)
  73. {
  74. return node[u].sum;
  75. }
  76. if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
  77. __int64 mid=(node[u].l+node[u].r)/2;
  78. if(right<=mid) return query(L(u),left,right);
  79. else if(left>mid) return query(R(u),left,right);
  80. else
  81. {
  82. return (query(L(u),left,mid)+query(R(u),mid+1,right));
  83. }
  84. }
  85.  
  86. int main()
  87. {
  88. __int64 n,m,i,x,y,z;
  89. while(scanf("%I64d%I64d",&n,&m)==2)
  90. {
  91. for(i=1;i<=n;i++)
  92. {
  93. scanf("%I64d",&a[i]);
  94. }
  95. build(1,1,n);
  96. char str[5];
  97. for(i=0;i<m;i++)
  98. {
  99. scanf("%s",str);
  100. if(str[0]=='C')
  101. {
  102. scanf("%I64d%I64d%I64d",&x,&y,&z);
  103. update(1,x,y,z);
  104. }
  105. else
  106. {
  107. scanf("%I64d%I64d",&x,&y);
  108. printf("%I64d\n",query(1,x,y));
  109. }
  110. }
  111. }
  112. return 0;
  113. }

poj 3466 A Simple Problem with Integers的更多相关文章

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

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

  6. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

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

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

  8. [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)

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

  9. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

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

随机推荐

  1. 告别IE给我们的web开发带来的困扰(使用chrome frame v8引擎)

    茶爸爸个人微信:benyzhous,公众号:cha-baba欢迎骚扰 由于客户所有机器必须使用IE6浏览器,导致我们在开发项目过程中遇到非常多的样式与性能问题,在偶然的一次使用360软件管家搜索chr ...

  2. C 语言中的变量为什么不能以数字打头

    C 语言中的变量为什么不能以数字打头? C 语言中的变量为什么不能以数字打头? 不要告诉我编译原理书上有.我暂时看不懂. 除了下面的解释外, “假如变量名允许以数字开头的话,那么语法分析器在解析一个全 ...

  3. IT第九天 - 包、访问修饰符、变量的内存分配、String类中常用方法

    IT第九天 上午 包 1.包的命名规则:域名.项目名称.模块名 2.如:Wfei.com.windows.login 访问限制符 1.四种访问限制符分别对应为: (1)default:默认的,默认为p ...

  4. IOS7 适配以及向下兼容问题

    1.所有的UIViewController加如下方法.     - (void) viewDidLayoutSubviews {         if ([[[UIDevice currentDevi ...

  5. Tomcat日志catalina.out文件过大的处理方法

    原文地址:http://lcbk.net/tomcat/1396.html 我们知道一般企业使用Tomcat 作为Web端时,它产生的日志会越来越大,特别是catalina.out这个日志文件,然而有 ...

  6. Activity中Menu相关的几个方法的调用时机

    用于创建菜单的常用的方法有如下两种: 1.onCreateOptionsMenu(Menu menu) 2.onPrepareOptionsMenu(Menu menu) MyDiaryActivit ...

  7. 【转】CentOS 6 服务器安全配置指南

    原文连接: CentOS 6 服务器安全配置指南(通用) Linux 是一个开放式系统,可以在网络上找到许多现成的程序和工具,这既方便了用户,也方便了黑客,因为他们也能很容易地找到程序和工具来潜入 L ...

  8. Main方法的执行过程(转)

    要运行一个 main 方法 , 首先要知道 main 方法所在的 Class, 在命令行中指定这个 Class 名 Class Lava{ Private int speed = 4; Void fl ...

  9. 数论(容斥原理)hdu-4509-The Boss on Mars

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个n,求1~n中与n互质的数的4次方的总和. 解题思路: 容斥原理.逆元.公式 ...

  10. 基于visual Studio2013解决C语言竞赛题之0520相邻元素

          题目