题目连接:

  http://poj.org/problem?id=3468

题目大意:

  给出n个数,有两种操作:

    1:"C a b c",[a,b]中的每一个数都加上c。

    2:"Q a b",求[a,b]中每个数相加的和。

解题思路:

  线段树更新到每一个节点的话,由于节点数目和查询次数原因会tle,所以在每一个节点内定义一个标志变量表示当前节点的下一层为更新,每次查询时候有需要的话在更新到下一层。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = ;
  7. const int INF = 0x3f3f3f3f;
  8. #define LL __int64
  9. struct node
  10. {
  11. LL L, R;
  12. LL sum, add;
  13. LL Mid()
  14. {
  15. return (L + R) / ;
  16. }
  17. };
  18. node tree[maxn];
  19. LL res;
  20.  
  21. void build(LL root, LL l, LL r)
  22. {
  23. tree[root].L = l;
  24. tree[root].R = r;
  25. tree[root].sum = tree[root].add = ;
  26.  
  27. if (l == r)
  28. return ;
  29. build (*root+, l, tree[root].Mid());
  30. build (*root+, tree[root].Mid()+, r);
  31. }
  32. void insert (LL root, LL s, LL e, LL x)
  33. {
  34. tree[root].sum += x * (e - s + );
  35. if (tree[root].L == s && e == tree[root].R)//更新到区间
  36. {
  37. tree[root].add += x;
  38. return ;
  39. }
  40. if (e <= tree[root].Mid())
  41. insert (*root+, s, e, x);
  42. else if (tree[root].Mid() < s)
  43. insert (*root+, s, e, x);
  44. else
  45. {
  46. insert (*root+, s, tree[root].Mid(), x);
  47. insert (*root+, tree[root].Mid()+, e, x);
  48. }
  49. }
  50. void query (LL root, LL s, LL e)
  51. {
  52.  
  53. if (tree[root].L == s && e == tree[root].R)
  54. {
  55. res += tree[root].sum;
  56. return ;
  57. }
  58. if (tree[root].add)
  59. {//向下继续更新
  60. tree[*root+].add += tree[root].add;
  61. tree[*root+].add += tree[root].add;
  62. tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
  63. tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
  64. tree[root].add = ;
  65. }
  66. if (e <= tree[root].Mid())
  67. query (*root+, s, e);
  68. else if (tree[root].Mid() < s)
  69. query (*root+, s, e);
  70. else
  71. {
  72. query (*root+, s, tree[root].Mid());
  73. query (*root+, tree[root].Mid()+, e);
  74. }
  75. }
  76. int main ()
  77. {
  78. LL n, m, num;
  79. while (scanf ("%I64d %I64d", &n, &m) != EOF)
  80. {
  81. build (, , n);
  82. for (int i=; i<=n; i++)
  83. {
  84. scanf ("%I64d", &num);
  85. insert (, i, i, num);
  86. }
  87. char str[];
  88. LL s, e;
  89. while (m --)
  90. {
  91. scanf ("%s %I64d %I64d", str, &s, &e);
  92. if (str[] == 'Q')
  93. {
  94. res = ;
  95. query (, s, e);
  96. printf ("%I64d\n", res);
  97. }
  98. else
  99. {
  100. scanf ("%I64d", &num);
  101. insert (, s, e, num);
  102. }
  103. }
  104. }
  105. return ;
  106. }

暑期训练狂刷系列——poj 3468 A 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 [线段树区间更新求和]

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

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

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

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

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

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

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

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

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

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

  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 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. MongoDB使用入门

    1.MongoDB的安装 步骤一:下载MongoDB 下载安装包:http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz 步骤二:设置 ...

  2. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  3. 谁是性能杀手?Kafka多Topic下启用SSL时延增大问题分析

    问题背景 项目中将Kafka接口进行RESTful封装,在使用RESTful接口进行性能测试时,发现Topic数增多后,开启SSL与非SSL进行测试,发现开启SSL后性能下降得厉害.例如600个Top ...

  4. c中常用的关键字static const volatile

    在C语言中,关键字static有三个明显的作用:1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.2). 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数 ...

  5. 2016/2/25 html+css学习资源

    html+css学习资源 1.Position is Everything,一个描述和展示在各种浏览器中发现的bug,并提供css解决方法的网站,顶! 2.一个国外的网页设计论坛 3.http://c ...

  6. Google Gson使用简介

    1.Google Gson在android studio的使用 gradle:compile 'com.google.code.gson:gson:2.2.4' 2.Gson 注解 @Expose 注 ...

  7. Delphi的goto语法

    今天第一次主要到Delphi也有goto语法,特别是其奇怪的label声明.估计主要是用来跳出多重循环,而且还真有人使用这种方式.记个笔记: procedure TForm1.btn3Click(Se ...

  8. Swift入门(十)——循环引用、弱引用和无主引用

    近期看到swift里面不仅有循环引用和弱引用(weak),还加入了无主引用(unowned),于是写了一些demo,这里总结一下. 和OC一样.Swfit默认也是基于ARC进行内存管理的,因此尽管简单 ...

  9. java语法基础(三)

    类和对象 面向对象语言概述 java是一种面向对象的语言,什么是面向对象的语言? 要搞清楚什么是面向对象语言,我们需要相对的了解一下面向过程的语言. java入门阶段,我们又给大家说过一些语言的分类, ...

  10. html5--项目实战-仿360囧图

    html5--项目实战-仿360囧图 实例: 代码 <!doctype html> <html> <head> <meta charset="utf ...