题目链接

题目

题目描述

给定数列 \(a[1],a[2], \dots,a[n]\) ,你需要依次进行q个操作,操作有两类:

1 l r x:给定l,r,x,对于所有 \(i \in[l,r]\) ,将a[i]加上x(换言之,将 \(a[l],a[l+1], \dots,a[r]\) 分别加上x);

2 l r:给定l,r,求 \(\sum_{i=l}^ra[i]\) 的值(换言之,求 \(a[l]+a[l+1]+ \dots+a[r]\) 的值)。

输入描述

第一行包含2个正整数n,q,表示数列长度和询问个数。保证 \(1 \le n,q \le10^6\) 。

第二行n个整数 \(a[1],a[2], \dots,a[n]\) ,表示初始数列。保证 \(|a[i]| \le10^6\) 。

接下来q行,每行一个操作,为以下两种之一:

1 l r x:对于所有 \(i \in[l,r]\) ,将a[i]加上x;

2 l r:输出 \(\sum_{i=l}^ra[i]\) 的值。保证 \(1 \le l \le r \le n\) , \(|x| \le10^6\) 。

输出描述

对于每个2lr操作,输出一行,每行有一个整数,表示所求的结果。

示例1

输入

  1. 5 10
  2. 2 6 6 1 1
  3. 2 1 4
  4. 1 2 5 10
  5. 2 1 3
  6. 2 2 3
  7. 1 2 2 8
  8. 1 2 3 7
  9. 1 4 4 10
  10. 2 1 2
  11. 1 4 5 6
  12. 2 3 4

输出

  1. 15
  2. 34
  3. 32
  4. 33
  5. 50

备注

对于所有数据,\(1 \le n,q \le10^6\) , \(|a[i]| \le10^6\) , \(1 \le l \le r \le n\) , \(|x| \le10^6\) 。

题解

方法一

知识点:线段树。

可以用线段树,不过因为线段树空间常数是四倍,容易炸空间,但还是有概率能过。

时间复杂度 \(O((n+q)\log n)\)

空间复杂度 \(O(n)\)

方法二

知识点:树状数组。

本题正解,是用树状数组维护区间和、区间加,是个板子题。

设原数组为 \(a\) ,差分数组为 \(d\) ,有如下公式:

\[\begin{aligned}
sum_{1,x} &= \sum_{i=1}^{x} a_i \\
&= \sum_{i=1}^{x} \sum_{j=1}^{i} d_j \\
&= \sum_{j=1}^{x} \sum_{i=j}^{x} d_j \\
&= \sum_{j=1}^{x} (x-j+1)d_j \\
&= (x+1) \sum_{j=1}^{x} d_j - \sum_{j=1}^{x} jd_j \\
\end{aligned}
\]

因此只需要维护 \(d_j\) 和 \(jd_j\) 的前缀和即可。同时,在差分意义下,区间加转化为两次单点加。

时间复杂度 \(O((n+q)\log n)\)

空间复杂度 \(O(n)\)

代码

方法一

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. struct T {
  5. int len;
  6. ll sum;
  7. static T e() { return { 0,0 }; }
  8. friend T operator+(const T &a, const T &b) { return { a.len + b.len, a.sum + b.sum }; }
  9. };
  10. struct F {
  11. ll add;
  12. static F e() { return { 0 }; }
  13. T operator()(const T &x) { return { x.len,x.sum + add * x.len }; }
  14. F operator()(const F &g) { return { g.add + add }; }
  15. };
  16. template<class T, class F>
  17. class SegmentTreeLazy {
  18. int n;
  19. vector<T> node;
  20. vector<F> lazy;
  21. void push_down(int rt) {
  22. node[rt << 1] = lazy[rt](node[rt << 1]);
  23. lazy[rt << 1] = lazy[rt](lazy[rt << 1]);
  24. node[rt << 1 | 1] = lazy[rt](node[rt << 1 | 1]);
  25. lazy[rt << 1 | 1] = lazy[rt](lazy[rt << 1 | 1]);
  26. lazy[rt] = F::e();
  27. }
  28. void update(int rt, int l, int r, int x, int y, F f) {
  29. if (r < x || y < l) return;
  30. if (x <= l && r <= y) return node[rt] = f(node[rt]), lazy[rt] = f(lazy[rt]), void();
  31. push_down(rt);
  32. int mid = l + r >> 1;
  33. update(rt << 1, l, mid, x, y, f);
  34. update(rt << 1 | 1, mid + 1, r, x, y, f);
  35. node[rt] = node[rt << 1] + node[rt << 1 | 1];
  36. }
  37. T query(int rt, int l, int r, int x, int y) {
  38. if (r < x || y < l)return T::e();
  39. if (x <= l && r <= y) return node[rt];
  40. push_down(rt);
  41. int mid = l + r >> 1;
  42. return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
  43. }
  44. public:
  45. SegmentTreeLazy(int _n = 0) { init(_n); }
  46. SegmentTreeLazy(const vector<T> &src) { init(src); }
  47. void init(int _n) {
  48. n = _n;
  49. node.assign(n << 2, T::e());
  50. lazy.assign(n << 2, F::e());
  51. }
  52. void init(const vector<T> &src) {
  53. assert(src.size());
  54. init(src.size() - 1);
  55. function<void(int, int, int)> build = [&](int rt, int l, int r) {
  56. if (l == r) return node[rt] = src[l], void();
  57. int mid = l + r >> 1;
  58. build(rt << 1, l, mid);
  59. build(rt << 1 | 1, mid + 1, r);
  60. node[rt] = node[rt << 1] + node[rt << 1 | 1];
  61. };
  62. build(1, 1, n);
  63. }
  64. void update(int x, int y, F f) { update(1, 1, n, x, y, f); }
  65. T query(int x, int y) { return query(1, 1, n, x, y); }
  66. };
  67. int main() {
  68. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  69. int n, q;
  70. cin >> n >> q;
  71. vector<T> a(n + 1);
  72. for (int i = 1;i <= n;i++) {
  73. int x;
  74. cin >> x;
  75. a[i] = { 1,x };
  76. }
  77. SegmentTreeLazy<T, F> sgt(a);
  78. while (q--) {
  79. int op, l, r;
  80. cin >> op >> l >> r;
  81. if (op == 1) {
  82. int x;
  83. cin >> x;
  84. sgt.update(l, r, { x });
  85. }
  86. else cout << sgt.query(l, r).sum << '\n';
  87. }
  88. return 0;
  89. }

方法二

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. struct T {
  5. ll sum;
  6. static T e() { return { 0 }; }
  7. T &operator+=(const T &x) { return sum += x.sum, *this; }
  8. };
  9. template<class T>
  10. class Fenwick {
  11. int n;
  12. vector<T> node;
  13. public:
  14. Fenwick(int _n = 0) { init(_n); }
  15. void init(int _n) {
  16. n = _n;
  17. node.assign(n + 1, T::e());
  18. }
  19. void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; }
  20. T query(int x) {
  21. T ans = T::e();
  22. for (int i = x;i >= 1;i -= i & -i) ans += node[i];
  23. return ans;
  24. }
  25. };
  26. int main() {
  27. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  28. int n, q;
  29. cin >> n >> q;
  30. Fenwick<T> d(n + 1), di(n + 1);
  31. auto update = [&](int l, int r, T val) {
  32. d.update(l, { val.sum }), d.update(r + 1, { -val.sum });
  33. di.update(l, { l * val.sum }), di.update(r + 1, { -(r + 1) * val.sum });
  34. };
  35. auto query = [&](int l, int r) {
  36. return ((r + 1) * d.query(r).sum - di.query(r).sum) -
  37. (l * d.query(l - 1).sum - di.query(l - 1).sum);
  38. };
  39. for (int i = 1;i <= n;i++) {
  40. int x;
  41. cin >> x;
  42. update(i, i, { x });
  43. }
  44. while (q--) {
  45. int op, l, r;
  46. cin >> op >> l >> r;
  47. if (op == 1) {
  48. int x;
  49. cin >> x;
  50. update(l, r, { x });
  51. }
  52. else cout << query(l, r) << '\n';
  53. }
  54. return 0;
  55. }

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

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

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

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

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

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

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

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  10. A Simple Problem with Integers

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

随机推荐

  1. Python追踪内存占用

    技术背景 当我们需要对python代码所占用的内存进行管理时,首先就需要有一个工具可以对当前的内存占用情况进行一个追踪.虽然在Top界面或者一些异步的工具中也能够看到实时的内存变化,还有一些工具可以统 ...

  2. 基于AHB_BUS SRAM控制器的设计-02

    AHB-SRAMC Design 片选信号决定哪几个memory被选择和功耗 sram_addr和sram_wdata都是可以通过AHB总线的控制信号得到的 1. sram_csn信号理解 hsize ...

  3. [转帖]linux audit审计(7-1)--读懂audit日志

    https://www.cnblogs.com/xingmuxin/p/8807774.html  auid=0 auid记录Audit user ID,that is the loginuid.当我 ...

  4. [转帖]minio 的 warp

    3 benchmarking tool. Download Download Binary Releases for various platforms. Configuration Warp can ...

  5. [转帖]tidb 如何对 TiDB 进行 TPC-C 测试

    https://docs.pingcap.com/zh/tidb/stable/benchmark-tidb-using-tpcc TPC-C 是一个对 OLTP(联机交易处理)系统进行测试的规范,使 ...

  6. [转帖]Nginx应用调优案例

    https://bbs.huaweicloud.com/blogs/146367 [摘要] 1 问题背景nginx的应用程序移植到TaiShan服务器上,发现业务吞吐量没有达到硬件预期,需要做相应调优 ...

  7. [转帖]Kafka关键参数设置

    https://www.cnblogs.com/wwcom123/p/11181680.html 生产环境中使用Kafka,参数调优非常重要,而Kafka参数众多,我们的java的Configurat ...

  8. js正则手机号 验证

    注意一下 现在手机号第二位是不是 只有3 4 5 7 8这几个数, 如果还有请告诉我,否则这个正则表达式式错误的. <div id="app"> <el-inpu ...

  9. Go复合类型之数组类型

    Go复合类型之数组 @ 目录 Go复合类型之数组 一.数组(Array)介绍 1.1 基本介绍 1.2 数组的特点 二.数组的声明与初始化 2.1 数组声明 2.2 常见的数据类型声明方法 2.3 数 ...

  10. [3] 以逆向的角度来看循环语句——do、while、for的比较

    [3] 以逆向的角度来看循环语句--do.while.for的比较 1. do循环 ​ 先执行循环体,后比较判断 #include <stdio.h> int main(int argc, ...