P6327 interval plus interval sin sum

topic

topic

Ideas

According to mathematical knowledge, there are:
sin (x + y) = cos (x) sin (y) + cos (y) sin (x) sin(x+y)=cos(x)sin(y)+cos(y) sin(x)s i n ( x+and )=c o s ( x ) s i n ( y )+c or s ( y ) s i n ( x ) ,
cos (x + y) = cos (x) cos (y) - sin (y) sin (x) cos (x + y) = cos (x) cos ( y) -sin (y) sin (x)cos(x+and )=c o s ( x ) c o s ( y )s i n ( y ) s i n ( x ) The
next step is to set the line segment tree board, seemy P2357 problem solution.
The name of this problem is not a board but a board
code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
long long lazy[800008],n,f,x,L,R;
double s[800008],c[800008],a[400004]; 
void jianshu(long long l,long long r,long long id)
{
    
    
	if (l==r)
	{
    
    
		s[id]=sin(a[l]);
		c[id]=cos(a[l]);
		return;
	}
	jianshu(l,(l+r)>>1,id*2);
	jianshu(((l+r)>>1)+1,r,id*2+1);
	s[id]=s[id*2]+s[id*2+1];
	c[id]=c[id*2]+c[id*2+1];
	return; 
}
void down(long long l,long long r,long long id)
{
    
    
	if (lazy[id])
	{
    
    
		lazy[id*2]+=lazy[id],lazy[id*2+1]+=lazy[id];
		double ss=s[id*2],cc=c[id*2];
		s[id*2]=cos(lazy[id])*ss+sin(lazy[id])*cc;
		c[id*2]=cos(lazy[id])*cc-sin(lazy[id])*ss;
		ss=s[id*2+1],cc=c[id*2+1];
		s[id*2+1]=cos(lazy[id])*ss+sin(lazy[id])*cc;
		c[id*2+1]=cos(lazy[id])*cc-sin(lazy[id])*ss;
		lazy[id]=0;
	}
	return;
}
void qjgx(long long l,long long r,long long id)
{
    
    
	if (l>=L&&R>=r)
	{
    
    
		double ss=s[id],cc=c[id];
		s[id]=cos(x)*ss+sin(x)*cc;
		c[id]=cos(x)*cc-sin(x)*ss;
		lazy[id]+=x;
		return;
	}
	down(l,r,id);
	long long mid=(l+r)>>1;
	if (L<=mid) qjgx(l,mid,id*2);
	if (R>mid) qjgx(mid+1,r,id*2+1);
	s[id]=s[id*2]+s[id*2+1];
	c[id]=c[id*2]+c[id*2+1];
	return;
}
double qjcx(long long l,long long r,long long id)
{
    
    
	if (l>=L&&R>=r)
	{
    
    
		return s[id];
	}
	down(l,r,id);
	long long mid=(l+r)>>1;
	double ans=0;
	if (L<=mid) ans+=qjcx(l,mid,id*2);
	if (R>mid) ans+=qjcx(mid+1,r,id*2+1);
	return ans;
}
long long u;
int main()
{
    
    
	cin>>n;
	for (long long i=1;i<=n;i++) cin>>a[i];
	jianshu(1,n,1);
	cin>>f;
	for (long long i=1;i<=f;i++)
	{
    
    
		scanf("%lld",&u);
		if (u==1)
		{
    
    
			scanf("%lld%lld%lld",&L,&R,&x);
			qjgx(1,n,1);
		}
		else
		{
    
    
			scanf("%lld%lld",&L,&R);
			printf("%.1lf\n",qjcx(1,n,1));
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_49843717/article/details/113823360