P6327 区间加区间sin和 线段树 + 数学

传送门

文章目录

题意:

给你一个长度为 n n n的序列 a a a,有 m m m次操作,每次操作分两种类型:

( 1 ) l , r , v (1)l,r,v (1)l,r,v,将 a l , a l + 1 , . . . , a r a_l,a_{l+1},...,a_r al,al+1,...,ar分别加上 v v v

( 2 ) l , r (2)l,r (2)l,r,询问 ∑ i = l r sin ⁡ ( a i ) \sum_{i=l}^r\sin(a_i) i=lrsin(ai)

思路:

闲的没事来水俩题。

考虑高中的和差公式, s i n ( a + b ) = s i n ( a ) c o s ( b ) + s i n ( b ) c o s ( a ) sin(a+b)=sin(a)cos(b)+sin(b)cos(a) sin(a+b)=sin(a)cos(b)+sin(b)cos(a),所以我们可以多维护一个 c o s cos cos,让后 c o s ( a + b ) = c o s ( a ) c o s ( b ) − s i n ( x ) s i n ( b ) cos(a+b)=cos(a)cos(b)-sin(x)sin(b) cos(a+b)=cos(a)cos(b)sin(x)sin(b),打个 l a z y lazy lazy,维护的时候注意一下细节即可。

瞎搞维护一下就好。

// Problem: P6327 区间加区间sin和
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6327
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
int a[N];
struct Node {
	int l,r;
	double sums,sumc;
	LL lazy;
}tr[N<<2];

void pushup(int u) {
	tr[u].sumc=tr[L].sumc+tr[R].sumc;
	tr[u].sums=tr[L].sums+tr[R].sums;
}

void pushdown(int u) {
	LL lazy=tr[u].lazy; tr[u].lazy=0;
	tr[L].lazy+=lazy; tr[R].lazy+=lazy;

	double ss=tr[L].sums,sc=tr[L].sumc;
	tr[L].sums=ss*cos(lazy)+sin(lazy)*sc;
	tr[L].sumc=sc*cos(lazy)-ss*sin(lazy);

	ss=tr[R].sums,sc=tr[R].sumc;
	tr[R].sums=ss*cos(lazy)+sin(lazy)*sc;
	tr[R].sumc=sc*cos(lazy)-ss*sin(lazy);
}

void build(int u,int l,int r) {
	tr[u]={l,r};
	if(l==r) {
		tr[u].sums=sin(a[l]);
		tr[u].sumc=cos(a[l]);
		return;
	}
	build(L,l,Mid); build(R,Mid+1,r);
	pushup(u);
}

void change(int u,int l,int r,int x) {
	if(tr[u].l>=l&&tr[u].r<=r) {
		tr[u].lazy+=x;
		double ss=tr[u].sums,sc=tr[u].sumc;
		tr[u].sums=ss*cos(x)+sin(x)*sc;
		tr[u].sumc=sc*cos(x)-sin(x)*ss;
		return;
	}
	pushdown(u);        
	if(l<=Mid) change(L,l,r,x);
	if(r>Mid) change(R,l,r,x);
	pushup(u);
}

double query(int u,int l,int r) {
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sums;
	pushdown(u);
	double ans=0.0;
	if(l<=Mid) ans+=query(L,l,r);
	if(r>Mid) ans+=query(R,l,r);
	return ans;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	build(1,1,n);
	scanf("%d",&m);
	while(m--) {
		int op,l,r; scanf("%d%d%d",&op,&l,&r);
		if(op==1) {
			int x; scanf("%d",&x);
			change(1,l,r,x);
		} else printf("%.1lf\n",query(1,l,r));
	}


	return 0;
}
/*

*/









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值