Solution to a problem [[Ynoi2012] NOIP2015 filled with hope]

\[ \texttt{Preface} \]

Road Ynoi second title, in memory of.

This is probably the only thing I can do myself Ynoi the subject.
\ [\ Texttt {Description} \
] to maintain a length \ (n-\) is the number of columns \ (A \) , supports two modes of operation:

  • 1 l r vThe \ (a_l, a_ {l + 1}, ..., a_r \) respectively plus \ (V \)
  • 2 l r 询问 \(\sum\limits_{i=l}\limits^{r}\sin(a_i)\)

\[ \texttt{Solution} \]

  • Interval Interval modify and query makes us think of tree line.

  • Try to maintain a tree line segment interval \ (\ sin \) and.

  • We found that the time interval plus the original \ (\ sin (a_i) \ ) have become \ (\ SiN (a_i + v) \) , is not easy to direct maintenance.

  • Consider these two formulas:
    \ [\ SiN (A + V) = \ A SiN \ COS + V \ A COS \ SiN V \]

    \ [\ Cos (a + v) = \ cos \ cos v- \ without a \ without v \]

  • Obviously interval plus the interval \ ([l, r] \ ) of \ (\ SiN \) and the \ (\ sum \ limits_ {i = l} \ limits ^ {r} \ sin (a_i) \) becomes \ (\ SUM \ limits_ {I = L} \ Limits ^ {R & lt} \ SiN (a_i + V) \) , further, there are:
    \ [\ SUM \ limits_ {I = L} \ Limits ^ {R & lt} \ SiN a_i \ cos v + \ cos a_i \ sin v \]

\[ \cos v \sum\limits_{i=l}\limits^{r}\sin a_i + \sin v\sum\limits_{i=l}\limits^{r}\cos a_i \]

  • So we can maintain a longer interval \ (\ COS \) and, on the interval plus \ ([l, r] \ ) of \ (\ COS \) impact and is:

\[ \cos v \sum\limits_{i=l}\limits^{r} \cos a_i -\sin v\sum\limits_{i=l}\limits^{r} \sin a_i \]

  • Apply the above equation updating interval \ (\ SiN \) and the interval and \ (\ COS \) and to remember lay mark.

\[ \texttt{Code} \]

#include<cstdio>
#include<cmath>

#define RI register int

using namespace std;

namespace IO
{
    static char buf[1<<20],*fs,*ft;
    inline char gc()
    {
        if(fs==ft)
        {
            ft=(fs=buf)+fread(buf,1,1<<20,stdin);
            if(fs==ft)return EOF;
        }
        return *fs++;
    }
    #define gc() getchar()
    inline int read()
    {
        int x=0,f=1;char s=gc();
        while(s<'0'||s>'9'){if(s=='-')f=-f;s=gc();}
        while(s>='0'&&s<='9'){x=x*10+s-'0';s=gc();}
        return x*f;
    }
}using IO::read;

const int N=200100;

int n,m;

int a[N];

struct SegmentTree{
    int l,r;
    double sinv,cosv;
    long long tag;
}t[N*4];

void upd(int p)
{
    t[p].sinv=t[p*2].sinv+t[p*2+1].sinv;
    t[p].cosv=t[p*2].cosv+t[p*2+1].cosv;
}

void spread(int p)
{
    if(t[p].tag)
    {
        double sina,cosa,sinx=sin(t[p].tag),cosx=cos(t[p].tag);
        sina=t[p*2].sinv,cosa=t[p*2].cosv;
        t[p*2].sinv=sina*cosx+cosa*sinx;
        t[p*2].cosv=cosa*cosx-sina*sinx;
        sina=t[p*2+1].sinv,cosa=t[p*2+1].cosv;
        t[p*2+1].sinv=sina*cosx+cosa*sinx;
        t[p*2+1].cosv=cosa*cosx-sina*sinx;
        t[p*2].tag+=t[p].tag;
        t[p*2+1].tag+=t[p].tag;
        t[p].tag=0;
    }
}

void build(int p,int l,int r)
{
    t[p].l=l,t[p].r=r;
    if(l==r)
    {
        t[p].sinv=sin(a[l]),t[p].cosv=cos(a[l]);
        return;
    }
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    upd(p);
}

void change(int p,int l,int r,int val)
{
    if(l<=t[p].l&&t[p].r<=r)
    {
        double sina=t[p].sinv,cosa=t[p].cosv,sinx=sin(val),cosx=cos(val);
        t[p].sinv=sina*cosx+cosa*sinx;
        t[p].cosv=cosa*cosx-sina*sinx;
        t[p].tag+=val;
        return;
    }
    spread(p);
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid)
        change(p*2,l,r,val);
    if(mid<r)
        change(p*2+1,l,r,val);
    upd(p);
}

double ask(int p,int l,int r)
{
    if(l<=t[p].l&&t[p].r<=r)return t[p].sinv;
    spread(p);
    int mid=(t[p].l+t[p].r)/2;
    double val=0;
    if(l<=mid)
        val+=ask(p*2,l,r);
    if(mid<r)
        val+=ask(p*2+1,l,r);
    return val;
}

int main()
{
    n=read();

    for(RI i=1;i<=n;i++)
        a[i]=read();

    build(1,1,n);

    m=read();

    while(m--)
    {
        int opt=read(),l=read(),r=read();

        switch(opt)
        {
            case 1:{

                double val; scanf("%lf",&val);
                change(1,l,r,val);

                break;
            }

            case 2:{

                printf("%.1lf\n",ask(1,l,r));

                break;
            }
        }
    }

    return 0;
}

\[ \texttt{Thanks} \ \texttt{for} \ \texttt{watching} \]

Guess you like

Origin www.cnblogs.com/cjtcalc/p/12324869.html