Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 115624   Accepted: 35897
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

Hint

The sums may exceed the range of 32-bit integers.

Source

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<deque>
  9. #include<iomanip>
  10. #include<vector>
  11. #include<cmath>
  12. #include<map>
  13. #include<stack>
  14. #include<set>
  15. #include<fstream>
  16. #include<memory>
  17. #include<list>
  18. #include<string>
  19. using namespace std;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. #define MAXN 100009
  23. #define L 31
  24. #define INF 1000000009
  25. #define eps 0.00000001
  26. /*
  27. 线段树 区间更新区间查询
  28. */
  29. LL a[MAXN],pre[MAXN];
  30. struct node
  31. {
  32. LL l, r;
  33. LL data, sum, laz;
  34. }T[MAXN*];
  35. void build(LL p, LL l, LL r)
  36. {
  37. T[p].data = T[p].sum = T[p].laz = ;
  38. T[p].l = l, T[p].r = r;
  39. if (l == r) return;
  40. LL mid = (l + r) / ;
  41. build(p * , l, mid);
  42. build(p * + , mid + , r);
  43. }
  44. void update(LL p, LL l, LL r, LL v)
  45. {
  46. //cout << p << ' ' << l << ' ' << r << ' ' << v << endl;
  47. if (T[p].l >= l && T[p].r <= r)
  48. {
  49. T[p].data += v;
  50. T[p].laz = ;
  51. T[p].sum += (T[p].r - T[p].l + ) * v;
  52. return;
  53. }
  54. LL mid = (T[p].l + T[p].r) / ;
  55. if (T[p].laz)
  56. {
  57. T[p].laz = ;
  58. update(p * , T[p].l, mid, T[p].data);
  59. update(p * + , mid + , T[p].r, T[p].data);
  60. T[p].data = ;
  61. }
  62. if (r <= mid)
  63. update(p * , l, r, v);
  64. else if (l > mid)
  65. update(p * + , l, r, v);
  66. else
  67. {
  68. update(p * , l, mid, v);
  69. update(p * + , mid + , r, v);
  70. }
  71. T[p].sum = T[p * ].sum + T[p * + ].sum;
  72. }
  73. LL query(LL p, LL l, LL r)
  74. {
  75. if (l == T[p].l&&r == T[p].r)
  76. return T[p].sum;
  77. LL mid = (T[p].l + T[p].r) / ;
  78. if (T[p].laz)
  79. {
  80. T[p].laz = ;
  81. update(p * , T[p].l, mid, T[p].data);
  82. update(p * + , mid + , T[p].r, T[p].data);
  83. T[p].data = ;
  84. }
  85. if (r <= mid)
  86. return query(p * , l, r);
  87. else if (l > mid)
  88. return query(p * + , l, r);
  89. else
  90. return query(p * , l, mid) + query(p * + , mid + , r);
  91. }
  92. LL n, q;
  93. int main()
  94. {
  95. scanf("%lld%lld", &n, &q);
  96. for (LL i = ; i <= n; i++)
  97. scanf("%lld", &a[i]), pre[i] = pre[i - ] + a[i];
  98. char c[];
  99. LL a, b, d;
  100. build(,,n);
  101. while (q--)
  102. {
  103. scanf("%s", c);
  104. if (c[] == 'Q')
  105. scanf("%lld%lld", &a, &b), printf("%lld\n", query(, a, b) + pre[b] - pre[a-]);
  106. else
  107. scanf("%lld%lld%lld", &a, &b, &d), update(, a, b, d);
  108. }
  109. }

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 , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

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

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

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

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

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

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

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

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

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

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

  8. A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

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

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

随机推荐

  1. Hadoop的数据采集框架

    问题导读: Hadoop数据采集框架都有哪些? Hadoop数据采集框架异同及适用场景? Hadoop提供了一个高度容错的分布式存储系统,帮助我们实现集中式的数据分析和数据共享.在日常应用中我们比如要 ...

  2. MySql数据库存储emoji表情报错解决办法

    异常:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1 解决: ...

  3. SpringMVC高级课程

    requestBody和responseBody requestBody把前台页面传递JSON格式数据强制转换JavaBean responseBody在后台把javabean转换成JSON格式的数据 ...

  4. jvm内存分区

    java内存是由jvm进行管理的,其内存简易模型如下图: jvm管理的内存大体上可分为方法区.堆.程序计数器.线程栈.本地方法区这几部分.方法区:主要存放类的元信息(包括类的名称.修饰符.静态变量.f ...

  5. HTML空格占位

      它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...

  6. Jmeter各组件介绍 及 使用

    本篇主要讲述Jmeter的各个组件及简单使用,其中包括以下内容: 一.线程组二.逻辑控制器三.配置元件四.定时器五.后置处理器六.断言七.监听器 八.参数化 网上大神整理的链接:http://blog ...

  7. java设计模式之策略模式总结

    策略模式的定义:(定义截自http://www.cnblogs.com/whgk/p/6087064.html) 1.策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立 ...

  8. StyleAI厚积薄发: Android网络图片数据传输

    在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...

  9. Jmeter的属性和变量

    jmeter的属性和变量可以简单理解为编程里面的全局变量和局部变量.属性是全局可见,可以跨线程组传递调用,而变量基本上只能存在于一个线程组中(在测试计划定义的变量也是可以跨线程组传递的).同线程组内的 ...

  10. JavaScript 中实现 sleep

    来自推特上 Windows 故障分析的笑话 图片来源:me.me 推上看到的笑话,Windows 故障分析的实现. 然后想起来 JavaScript 中如何实现这个 sleep() 函数让代码暂停指定 ...