NC17877 整数序列
题目
题目描述
给出一个长度为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
输入
4
1 2 3 4
5
2 2 4
1 1 3 1
2 2 4
1 2 4 2
2 1 3
输出
0.3
-1.4
-0.3
题解
知识点:线段树,数学。
分析一下要维护哪些信息,首先要维护区间正弦和,需要维护区间加,先推一个区间加的公式:
\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)\) ,因此考虑再维护一个区间余弦和,同样推一下区间加的公式:
\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)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct T {
double ssum, csum;
static T e() { return { 0,0 }; }
friend T operator+(const T &a, const T &b) { return{ a.ssum + b.ssum,a.csum + b.csum }; }
};
struct F {
ll add;
static F e() { return { 0 }; }
T operator()(const T &x) {
return {
x.ssum * cos(add) + x.csum * sin(add),
x.csum * cos(add) - x.ssum * sin(add),
};
}
F operator()(const F &f) { return { f.add + add }; }
};
template<class T, class F>
class SegmentTreeLazy {
int n;
vector<T> node;
vector<F> lazy;
void push_down(int rt) {
node[rt << 1] = lazy[rt](node[rt << 1]);
lazy[rt << 1] = lazy[rt](lazy[rt << 1]);
node[rt << 1 | 1] = lazy[rt](node[rt << 1 | 1]);
lazy[rt << 1 | 1] = lazy[rt](lazy[rt << 1 | 1]);
lazy[rt] = F::e();
}
void update(int rt, int l, int r, int x, int y, F f) {
if (r < x || y < l) return;
if (x <= l && r <= y) return node[rt] = f(node[rt]), lazy[rt] = f(lazy[rt]), void();
push_down(rt);
int mid = l + r >> 1;
update(rt << 1, l, mid, x, y, f);
update(rt << 1 | 1, mid + 1, r, x, y, f);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
}
T query(int rt, int l, int r, int x, int y) {
if (r < x || y < l) return T::e();
if (x <= l && r <= y) return node[rt];
push_down(rt);
int mid = l + r >> 1;
return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
}
public:
SegmentTreeLazy(int _n = 0) { init(_n); }
SegmentTreeLazy(const vector<T> &src) { init(src); }
void init(int _n) {
n = _n;
node.assign(n << 2, T::e());
lazy.assign(n << 2, F::e());
}
void init(const vector<T> &src) {
assert(src.size() >= 2);
init(src.size() - 1);
function<void(int, int, int)> build = [&](int rt, int l, int r) {
if (l == r) return node[rt] = src[l], void();
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
};
build(1, 1, n);
}
void update(int x, int y, F f) { update(1, 1, n, x, y, f); }
T query(int x, int y) { return query(1, 1, n, x, y); }
};
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<T> a(n + 1);
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
a[i] = { sin(x),cos(x) };
}
SegmentTreeLazy<T, F> sgt(a);
int q;
cin >> q;
cout << fixed << setprecision(1);
while (q--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 1) {
int x;
cin >> x;
sgt.update(l, r, { x });
}
else cout << sgt.query(l, r).ssum << '\n';
}
return 0;
}
NC17877 整数序列的更多相关文章
- //给定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; ]={,- ...
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- Interview----判断整数序列是否是二叉搜索树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: ...
- Python实现在给定整数序列中找到和为100的所有数字组合
摘要: 使用Python在给定整数序列中找到和为100的所有数字组合.可以学习贪婪算法及递归技巧. 难度: 初级 问题 给定一个整数序列,要求将这些整数的和尽可能拼成 100. 比如 [17, 1 ...
- Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询
题目链接:https://www.nowcoder.com/acm/contest/160/D 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524288K ...
- python 和 R 中的整数序列
python 中的 range() 函数是很常用的,R 中相应的函数是 seq(), 其实,R 中的“ :”也能代替 python 中的 range() 函数. 1.生成升序整数序列 python: ...
- Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)
链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...
- PTA 递增的整数序列链表的插入
6-4 递增的整数序列链表的插入 (15 分) 本题要求实现一个函数,在递增的整数序列链表(带头结点)中插入一个新整数,并保持该序列的有序性. 函数接口定义: List Insert( List ...
- akoj-1048-求某一整数序列的全排列问题
求某一整数序列的全排列问题 Time Limit:1000MS Memory Limit:65536K Total Submit:35 Accepted:16 Description 现有一整数序列 ...
- OpenJudge1.5.6:整数序列的元素最大跨度值
描述 给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值). 输入一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不 ...
随机推荐
- Could not get a resource from the pool 异常定位和解决
最近在服务中经常看到以下错误,进行下定位和问题解决分析: 2023-12-08 00:10:58.248 WARN [terra-sr-server,a9006fd27ccb81d0,a9006fd2 ...
- 【C/C++】函数入参检查
// 统计变参数量 #define CALC_VA_COUNT(arg...) \ ({ \ int count = 0; \ int insideQuotes = 0; \ const char * ...
- 【FreeRTOS】内核查找最高优先级就绪任务
查找最高优先级就绪任务 FreeRTOS\Source\tasks.c #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) /* If confi ...
- 【C】C语言头文件互锁和包含问题
[来源] https://mp.weixin.qq.com/s/4tPHkwEd5n-xHNHJHtCoBw
- SpringBoot利用自定义注解实现多数据源
自定义多数据源 SpringBoot利用自定义注解实现多数据源,前置知识:注解.Aop.SpringBoot整合Mybaits 1.搭建工程 创建一个SpringBoot工程,并引入依赖 <de ...
- [转帖]总成本降低80%,支付宝使用OceanBase的历史库实践
https://open.oceanbase.com/blog/5377309696 为解决因业务增长引发的数据库存储空间问题,支付宝基于 OceanBase 数据库启动历史库项目,通过历史数据归档. ...
- [转帖]/dev/random 和 /dev/urandom的一点备忘
https://www.cnblogs.com/ohmygirl/p/random.html 1. 基本介绍 /dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两 ...
- [转帖]防火墙、DCD与TCP Keep alive
https://www.laoxiong.net/tag/network 在以前我写的一篇文章<Oracle与防火墙>中提到,网络防火墙会切断长时间空闲的TCP连接,这个空闲时间具体多长可 ...
- [转帖]50年来Intel CPU变化有多大?频率从0.75MHz提升到5.2GHz
https://m.baidu.com/bh/m/detail/ar_9297450181050583423?data_from=lemon 今天(11月15日)是Intel推出4004处理器50周年 ...
- Ubuntu2204设置固定IP地址
前言 Ubuntu每次升级都会修改一部分组件. 从1804开始Ubuntu开始使用netplan的方式进行网络设置. 但是不同版本的配置一直在升级与变化. 今天掉进坑里折腾了好久. 所以这边总结一下, ...