题目链接

题目

题目描述

给出一个长度为n的整数序列 \(a_1,a_2,...,a_n\) ,进行 \(m\) 次操作,操作分为两类。

操作1:给出 \(l,r,v\) ,将 \(a_l,a_{l+1},...,a_r\) 分别加上 \(v\) ;

操作2:给出 \(l,r\) ,询问 \(\sum\limits_{i=l}^{r}sin(a_i)=l\)

输入描述

第一行一个整数 \(n\)

接下来一行 \(n\) 个整数表示 \(a_1,a_2,...,a_n\)

接下来一行一个整数 \(m\)

接下来 \(m\) 行,每行表示一个操作,操作1表示为1 l r v,操作2表示为2 l r

保证 \(1 \leq n,m,a_i,v \leq 200000\) ; \(1 \leq l \leq r \leq n\) , \(v\) 是整数

输出描述

对每个操作2,输出一行,表示答案,四舍五入保留一位小数

保证答案的绝对值大于0.1,且答案的准确值的小数点后第二位不是4或5

数据随机生成(n,m人工指定,其余整数在数据范围内均匀选取),并去除不满足条件的操作2

示例1

输入

  1. 4
  2. 1 2 3 4
  3. 5
  4. 2 2 4
  5. 1 1 3 1
  6. 2 2 4
  7. 1 2 4 2
  8. 2 1 3

输出

  1. 0.3
  2. -1.4
  3. -0.3

题解

知识点:线段树,数学。

分析一下要维护哪些信息,首先要维护区间正弦和,需要维护区间加,先推一个区间加的公式:

\[\begin{aligned}
\sum_{i = l}^r sin(a_i + x) &= \sum_{i = l}^r sin(a_i)cos(x) + cos(a_i)sin(x) \\
&= cos(x) \sum_{i = l}^r sin(a_i) + sin(x)\sum_{i = l}^r cos(a_i)
\end{aligned}
\]

出现了 \(\displaystyle \sum_{i=l}^r cos(a_i)\) ,因此考虑再维护一个区间余弦和,同样推一下区间加的公式:

\[\begin{aligned}
\sum_{i = l}^r cos(a_i + x) &= \sum_{i = l}^r cos(a_i)cos(x) - sin(a_i)sin(x) \\
&= cos(x) \sum_{i = l}^r cos(a_i) - sin(x)\sum_{i = l}^r sin(a_i)
\end{aligned}
\]

可以看到只需要区间正弦和、余弦和就能维护了。

因此,区间信息只需要维护区间正弦和 \(ssum\)、区间余弦和 \(csum\) ,区间和直接加即可。

区间修改需要维护区间加 \(add\) ,之前的分析已经得到区间修改公式。

区间修改需要设置懒标记,标记修改做简单加法即可。

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

NC17877 整数序列的更多相关文章

  1. //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和

    //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...

  2. IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果

    问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...

  3. Interview----判断整数序列是否是二叉搜索树的后序遍历结果

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果:   ...

  4. Python实现在给定整数序列中找到和为100的所有数字组合

    摘要:  使用Python在给定整数序列中找到和为100的所有数字组合.可以学习贪婪算法及递归技巧. 难度:  初级 问题 给定一个整数序列,要求将这些整数的和尽可能拼成 100. 比如 [17, 1 ...

  5. Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询

    题目链接:https://www.nowcoder.com/acm/contest/160/D 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524288K ...

  6. python 和 R 中的整数序列

    python 中的 range() 函数是很常用的,R  中相应的函数是 seq(), 其实,R 中的“ :”也能代替 python 中的 range() 函数. 1.生成升序整数序列 python: ...

  7. Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)

    链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...

  8. PTA 递增的整数序列链表的插入

    6-4 递增的整数序列链表的插入 (15 分)   本题要求实现一个函数,在递增的整数序列链表(带头结点)中插入一个新整数,并保持该序列的有序性. 函数接口定义: List Insert( List ...

  9. akoj-1048-求某一整数序列的全排列问题

    求某一整数序列的全排列问题 Time Limit:1000MS  Memory Limit:65536K Total Submit:35 Accepted:16 Description 现有一整数序列 ...

  10. OpenJudge1.5.6:整数序列的元素最大跨度值

    描述 给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值). 输入一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不 ...

随机推荐

  1. Could not get a resource from the pool 异常定位和解决

    最近在服务中经常看到以下错误,进行下定位和问题解决分析: 2023-12-08 00:10:58.248 WARN [terra-sr-server,a9006fd27ccb81d0,a9006fd2 ...

  2. 【C/C++】函数入参检查

    // 统计变参数量 #define CALC_VA_COUNT(arg...) \ ({ \ int count = 0; \ int insideQuotes = 0; \ const char * ...

  3. 【FreeRTOS】内核查找最高优先级就绪任务

    查找最高优先级就绪任务 FreeRTOS\Source\tasks.c #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) /* If confi ...

  4. 【C】C语言头文件互锁和包含问题

    [来源] https://mp.weixin.qq.com/s/4tPHkwEd5n-xHNHJHtCoBw

  5. SpringBoot利用自定义注解实现多数据源

    自定义多数据源 SpringBoot利用自定义注解实现多数据源,前置知识:注解.Aop.SpringBoot整合Mybaits 1.搭建工程 创建一个SpringBoot工程,并引入依赖 <de ...

  6. [转帖]总成本降低80%,支付宝使用OceanBase的历史库实践

    https://open.oceanbase.com/blog/5377309696 为解决因业务增长引发的数据库存储空间问题,支付宝基于 OceanBase 数据库启动历史库项目,通过历史数据归档. ...

  7. [转帖]/dev/random 和 /dev/urandom的一点备忘

    https://www.cnblogs.com/ohmygirl/p/random.html 1.  基本介绍 /dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两 ...

  8. [转帖]防火墙、DCD与TCP Keep alive

    https://www.laoxiong.net/tag/network 在以前我写的一篇文章<Oracle与防火墙>中提到,网络防火墙会切断长时间空闲的TCP连接,这个空闲时间具体多长可 ...

  9. [转帖]50年来Intel CPU变化有多大?频率从0.75MHz提升到5.2GHz

    https://m.baidu.com/bh/m/detail/ar_9297450181050583423?data_from=lemon 今天(11月15日)是Intel推出4004处理器50周年 ...

  10. Ubuntu2204设置固定IP地址

    前言 Ubuntu每次升级都会修改一部分组件. 从1804开始Ubuntu开始使用netplan的方式进行网络设置. 但是不同版本的配置一直在升级与变化. 今天掉进坑里折腾了好久. 所以这边总结一下, ...