当前位置: 首页 > news >正文

P6327 区间加区间 sin 和 Solution

Description

给定序列 a = ( a 1 , a 2 , ⋯   , a n ) a=(a_1,a_2,\cdots,a_n) a=(a1,a2,,an),有 m m m 个操作分两种:

  • add ⁡ ( l , r , k ) \operatorname{add}(l,r,k) add(l,r,k):对每个 i ∈ [ l , r ] i\in[l,r] i[l,r] 执行 a i ← a i + k a_i\gets a_i+k aiai+k.
  • query ⁡ ( l , r ) \operatorname{query}(l,r) query(l,r):求 ∑ i = l r sin ⁡ ( a i ) \sum\limits_{i=l}^r \sin(a_i) i=lrsin(ai).

Limitations

1 ≤ n , m , a i , k ≤ 1 0 5 1\le n,m,a_i,k \le 10^5 1n,m,ai,k105
1 ≤ l ≤ r ≤ n 1\le l \le r\le n 1lrn
1 s , 125 MB 1\text{s},125\text{MB} 1s,125MB

Solution

首先考虑转化,使用泰勒展开,然后维护 k k k 次方和,但精度显然会炸.
事实上,有公式:

  • sin ⁡ ( x + y ) = sin ⁡ x cos ⁡ y + cos ⁡ x sin ⁡ y \sin(x+y)=\sin x \cos y + \cos x \sin y sin(x+y)=sinxcosy+cosxsiny.
  • cos ⁡ ( x + y ) = cos ⁡ x cos ⁡ y − sin ⁡ x sin ⁡ y \cos(x+y)=\cos x \cos y - \sin x \sin y cos(x+y)=cosxcosysinxsiny.

于是可用线段树维护区间 sin ⁡ \sin sin 和与 cos ⁡ \cos cos 和.
区间加考虑打 tag \textit{tag} tagpushdown 时用上面公式即可做到 O ( 1 ) O(1) O(1).

Code

2.64 KB , 4.59 s , 13.45 MB    (in   total,   C++20   with   O2) 2.64\text{KB},4.59\text{s},13.45\text{MB}\;\texttt{(in total, C++20 with O2)} 2.64KB,4.59s,13.45MB(in total, C++20 with O2)

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

namespace seg_tree {
	struct Node {
		int l, r;
		i64 tag = 0;
		f8 sin = 0., cos = 0.;
	};
	
	inline int ls(int u) { return 2 * u + 1; }
	inline int rs(int u) { return 2 * u + 2; }
	
	struct SegTree {
		vector<Node> tr;
		
		SegTree() {}
		SegTree(const vector<int> &a) {
			const int n = a.size();
			tr.resize(n << 1);
			build(0, 0, n - 1, a);
		}
		
		void apply(int u, i64 c, f8 sc, f8 cc) {
			f8 si = tr[u].sin, ci = tr[u].cos;
			tr[u].sin = si * cc + ci * sc;
			tr[u].cos = cc * ci - si * sc;
			tr[u].tag += c;
		}
		
		void pushup(int u, int mid) {
			tr[u].sin = tr[ls(mid)].sin + tr[rs(mid)].sin;
			tr[u].cos = tr[ls(mid)].cos + tr[rs(mid)].cos;
		}
		
		void pushdown(int u, int mid) {
			if (!tr[u].tag) return;
			i64 x = tr[u].tag;
			f8 sx = sin(x), cx = cos(x);
			apply(ls(mid), x, sx, cx);
			apply(rs(mid), x, sx, cx);
			tr[u].tag = 0;
		}
		
		void build(int u, int l, int r, const vector<int> &a) {
			tr[u].l = l; tr[u].r = r;
			if (l == r) {
				tr[u].sin = sin(a[l]);
				tr[u].cos = cos(a[l]);
				return;
			}
			
			int mid = (l + r) >> 1;
			build(ls(mid), l, mid, a);
			build(rs(mid), mid + 1, r, a);
			pushup(u, mid);
		}
		
		void modify(int u, int l, int r, int c) {
			if (l <= tr[u].l && tr[u].r <= r) {
				apply(u, c, sin(c), cos(c));
				return;
			}
			int mid = (tr[u].l + tr[u].r) >> 1;
			pushdown(u, mid);
			if (l <= mid) modify(ls(mid), l, r, c);
			if (mid < r) modify(rs(mid), l, r, c);
			pushup(u, mid);
		}
		
		f8 query(int u, int l, int r) {
			if (l <= tr[u].l && tr[u].r <= r) return tr[u].sin;
			f8 ans = 0;
			int mid = (tr[u].l + tr[u].r) >> 1;
			pushdown(u, mid);
			if (l <= mid) ans += query(ls(mid), l, r);
			if (mid < r) ans += query(rs(mid), l, r);
			return ans; 
		}
		
		void range_add(int l, int r, int v) { modify(0, l, r, v); }
		f8 range_sine(int l, int r) { return query(0, l, r); }
	};
}
using seg_tree::SegTree;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	SegTree sgt(a);
	int m; cin >> m;
	for (int i = 0, op, l, r, c; i < m; i++) {
		cin >> op >> l >> r, l--, r--;
		if (op == 1) {
			cin >> c;
			sgt.range_add(l, r, c);
		}
		else printf("%.1lf\n", sgt.range_sine(l, r));
	}
	
	return 0;
}

相关文章:

  • Neuralink API开发指南:用Python读取脑电信号控制智能家居
  • C++ unique_ptr、shared_ptr、weak_ptr全面解析
  • LLaMA Factory微调后的大模型在vLLM框架中对齐对话模版
  • 【LVLMs】LVLMs和OVD结合的一些想法
  • Spring AI Alibaba 对话记忆使用
  • Java基础-26-多态-认识多态
  • 第十九章:Python-pyttsx3 库实现文本转语音功能
  • OpenCV 图形API(5)API参考:数学运算用于执行图像或矩阵加法操作的函数add()
  • mapreduce的工作原理
  • Codeforces Round 1014 (Div. 2)
  • Jetson 设备卸载 OpenCV 4.5.4 并编译安装 OpenCV 4.2.0
  • 电商---part01 项目整体
  • Keil5工程中.uvoptx和.uvprojx后缀名什么意思?
  • 【设计模式】深入解析设计模式:门面模式(外观模式)的定义、优点和代码实现
  • 383. 赎金信
  • 【Git】-- 处理 Git 提交到错误分支的问题
  • 深入理解哈希优化策略与TypeScript实现
  • 【LeetCode Solutions】LeetCode 111 ~ 115 题解
  • 快速构建个人本地知识库管理系统与实现RAG问答
  • JVM面试专题
  • 视频网站的链接怎么做/长春网络优化最好的公司
  • 征婚网站上拉业务做恒指期货/登封seo公司
  • 鄂城区人民政府门户网站/快手作品免费推广软件
  • 合肥专业手机网站哪家好/黄页推广
  • 青海公司网页设计/百度的seo排名怎么刷
  • 去设计公司还是去企业/深圳搜索引擎优化seo