题目链接

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7.  
  8. using namespace std;
  9. const int N = ;
  10. typedef long long int LL;
  11. LL sum[N << ];
  12. LL add[N << ];
  13. struct node
  14. {
  15. int l, r;
  16. int mid()
  17. {
  18. return (l + r) >> ;
  19. }
  20. }tree[N << ];
  21. void PushUp(int rt)
  22. {
  23. sum[rt] = sum[rt << ] + sum[rt << | ];
  24. }
  25. void PushDown(int rt, int m)
  26. {
  27. if (add[rt]) {
  28. add[rt << ] += add[rt];
  29. add[rt << | ] += add[rt];
  30. sum[rt << ] += add[rt] * (m - (m >> ));
  31. sum[rt << | ] += add[rt] * (m >> );
  32. add[rt] = ;//更新后要还原,因为递归可能会多次用到这个
  33. }
  34. }
  35.  
  36. void build(int l,int r,int rt)
  37. {
  38. tree[rt].l = l;
  39. tree[rt].r = r;
  40. add[rt] = ;
  41. if (l == r) {
  42. scanf("%lld", &sum[rt]);
  43. return;
  44. }
  45. int m = tree[rt].mid();
  46. build(l, m, rt << );
  47. build(m + , r, rt << | );
  48. PushUp(rt);
  49. }
  50.  
  51. void updata(int c, int l, int r, int rt)
  52. {
  53. if (tree[rt].l == l && r == tree[rt].r) {
  54. add[rt] += c;
  55. sum[rt] += (LL)(r - l + )*c;
  56. return;//这里没有进行子区间更新,用到lazy标记
  57. }
  58. if (tree[rt].l == tree[rt].r) return;
  59. PushDown(rt, tree[rt].r - tree[rt].l + );
  60. int m = tree[rt].mid();
  61. if (r <= m) updata(c,l, r, rt << );
  62. else if (l > m) updata(c,l, r, rt << | );
  63. else {
  64. updata(c, l, m, rt << );
  65. updata(c, m + , r, rt << | );
  66. }
  67. PushUp(rt);
  68.  
  69. }
  70.  
  71. LL query(int l, int r, int rt)
  72. {
  73. if (l == tree[rt].l&&r == tree[rt].r) {
  74. return sum[rt];
  75. }
  76. PushDown(rt, tree[rt].r - tree[rt].l + );//标记的特点,用时才进行更新
  77. int m = tree[rt].mid();
  78. LL res = ;
  79. if (r <= m) res += query(l, r, rt << );
  80. else if (l > m) res += query(l, r, rt << | );
  81. else {
  82. res += query(l, m, rt << );
  83. res += query(m + , r, rt << | );
  84. }
  85. return res;
  86. }
  87. int main()
  88. {
  89. int n, m;
  90. while (scanf("%d %d", &n, &m) == ) {
  91. memset(sum, , sizeof(sum));
  92. memset(add, , sizeof(add));
  93. build(, n, );
  94. while (m--) {
  95. char ch[];
  96. scanf("%s", ch);
  97. if (ch[] == 'Q') {
  98. int a, b;
  99. scanf("%d %d", &a, &b);
  100. printf("%lld\n", query(a, b, ));
  101. }
  102. else {
  103. int a, b, c;
  104. scanf("%d %d %d", &a, &b, &c);
  105. updata(c, a, b, );
  106. }
  107. }
  108. }
  109.  
  110. return ;
  111. }

线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

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

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

  3. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

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

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

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

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

  6. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  7. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  8. CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询

    链接: I - 秋实大哥与花 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

  9. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

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

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

随机推荐

  1. 最终类object 和内部类

    Object 类 性质:[1]是所有类的根类.             [2]如果一个类没有显示继承另外一个类,那么该类一定继承于Object toString() 返回对象的字符串表示形式 特殊:[ ...

  2. python2.7 加密模块 解决各种坑

    1 Python27 安装crypto Windows安装 在Windows上安装的时候直接 pip install pycrypto会报错,参考:http://blog.csdn.net/teloy ...

  3. Ubuntu 12.04 the system is running in low-graphics mode

    1.出现问题如图所示: 2.解决方案: Ctrl + Alt + F1 df -h 输入密码,到了这一步,也是可以使用terminal,那么没有图形界面也是可以的 cd /etc/X11 sudo c ...

  4. C#操作Word,写数据,插入图片

    本篇介绍的是如何在C#中往word里面写入数据. 如何在线的操作文档:  c#在线操作文档 关于Aspose.Word控件的介绍,请戳→ 介绍 首先需要去下载这个dll文件,然后引用到你的项目当中.地 ...

  5. vscode + leetcode +github 同步

    1.用VScode打开本地leetcode文件夹 C:\Users\Administrator\.leetcode 2.上传到本地git库 3.打开github桌面,上传到远程库

  6. filebeat的安装及配置

    概述:Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasti ...

  7. css样式 body的font-size 为什么用625%

    浏览器的默认高度?一般为16px. 为什么用62.5%作为body的默认样式?16px62.5%=10px.* 那么为什么一般多是 16px  *625% = 100px; <响应式Web设计实 ...

  8. MySQL创建民族表的SQL语句

    MySQL创建民族表的SQL语句 CREATE TABLE `nation` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `nation` ) NOT NUL ...

  9. php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数

    // opcode处理器 --- ZEND_DO_FCALL_SPEC_CONST_HANDLER实现在 php-5.6.26\Zend\zend_vm_execute.h static int ZE ...

  10. 易语言制作的QQ聊天中常用的GIF图片【带源码下载】

    该软件调用网页实现表情包制作,使用了精益模块. 最近比较火的王境泽.张学友.切格瓦拉.为所欲为.今天星期五.黑人问号脸.偷电瓶车.诸葛孔明.金坷垃等都可以通过此软件在线制作属于你的表情包. 太困了懒得 ...