题目链接

题目

题目描述

给定数列 \(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. 供应链投毒预警 | 恶意Py包仿冒tensorflow AI框架实施后门投毒攻击

    概述 本周(2024年01月15号),悬镜供应链安全实验室在Pypi官方仓库(https://pypi.org/)中捕获1起Py包投毒事件,投毒者利用包名错误拼写(typo-squatting)的攻击 ...

  2. 一个容易弄错的.textContent和text()问题

    最近在写一些前端页面,遇到需要通过点击事件获取被点击对象的文本内容,正确代码如下: <!DOCTYPE html> <html lang="en"> < ...

  3. Laravel - 解决 $.ajax success 返回的数据为空的问题 (后台为laravel)

    原因之一 :  后台 dump()打印 , 去掉或者注释就好了

  4. RabbitMQ .net core 客户端 EasyNetQ 的使用

    依赖注入 var connectionConfiguration = new ConnectionConfiguration { Hosts = new List<HostConfigurati ...

  5. http-长连接

    1. 短链接 http1.0 -- 1个请求-响应过程会创建且1个新的连接 2. 长连接 http1.1 -- 同域下可以创建1个tcp连接,多个请求在同一个tcp上串行处理请求 http2.0 -- ...

  6. 2.4G+MCU低功耗二合一芯片SI24R03

    2.4G+MCU低功耗二合一芯片SI24R03 1 简介 Si24R03 是一款高度集成的低功耗 SOC 芯片,其集成了基于 RISC-V 核的低功耗 MCU 和 工作在 2.4GHz ISM 频段的 ...

  7. [转帖]数据库的快照隔离级别(Snapshot Isolation)

    https://www.cnblogs.com/gered/p/10251744.html 总结:已提交读快照只影响语句级别的锁定行为,而快照隔离影响整个事务.  转自:https://www.cnb ...

  8. [转帖]docker使用阿里镜像源

    ps:docker使用阿里镜像源特别快 首先安装docker:参考https://www.jianshu.com/p/2dae7b13ce2f 一.使用阿里镜像地址: dockerd --regist ...

  9. [转帖]linux 下 {}大括号的用法

    我们平时使用Linux的时候经常遇到这样一个问题,举例有这样一种情况:执行命令 $ cp /etc/apt/sources.list /etc/apt/sources.list.bak 这里面有个问题 ...

  10. [转帖]看6大国产CPU加速替代,谁才是“王者”选手?

    https://baijiahao.baidu.com/s?id=1761150458273739276&wfr=spider&for=pc 2023-03-23 17:33湖北匠心计 ...