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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值