$SP2713$ $GSS4$ $-$ $Can$ $you$ $answer$ $these$ $queries$ $IV$

link

background

\(Fudan\) \(University\) \(Problem\) \(Setters\)\(2008.5.21\)\(SP\) \(1716\)

The meaning of problems

A given interval, the interval required prescribing (rounded down), the summation interval.

solution

Tree line interval violence modify, interval query template . Each maintenance intervals and \ (SUM \) , the maximum interval \ (MAXN \) .
This problem is characteristic in that the section prescribing (rounded down) operation does not have this interval may increase, and therefore can not maintain marking. And because \ (\ sqrt {1} = 1 \) , \ (\ 0 = sqrt {0} \) , interval maximum value so less \ (1 \) This section need not update. And each of the interval were \ (10 ^ {18} \) or less, and \ (10 ^ {18} \) Continuous \ (6 \) sub-interval prescribing (rounded down) after as \ ( 1 \) , each number of modifications that do not exceed \ (6 \) times, the operation of all modifications total time complexity is \ (O (n-) \) . The time complexity of the query is \ (O (log \) \ (the n-) \) , so the total time complexity is still on.

\(trick\)

\ (1 \) interval operation \ ([l, r] \ ) to ensure \ (L> R & lt \) .

detail

\ (1 \) achievements Fu remember about endpoint. . . . . .

\ (2 \) Note Kichiku output format. . . . . .

\ (3 \) Note Funny data range. . . . . .

\ (4 \) sets of data have to remember to empty. . . . . .

\ (5 \) attention to ask what value. . . . . .

Code

\(View\) \(Code\)

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
inline long long readl()
{
    long long ret=0;
    int f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int cas,n,q,op,l,r;
long long a[100005];
struct SegmentTree
{
    int l,r;
    long long sum,maxn;
}t[400005];
void build(int pos,int l,int r)
{
    t[pos].l=l;
    t[pos].r=r;
    if(l==r)
    {
        t[pos].sum=a[l];
        t[pos].maxn=a[l];
        return;
    }
    int mid=(l+r)/2;
    build(pos<<1,l,mid);
    build(pos<<1|1,mid+1,r);
    t[pos].sum=t[pos<<1].sum+t[pos<<1|1].sum;
    t[pos].maxn=max(t[pos<<1].maxn,t[pos<<1|1].maxn);
}
void modify(int pos,int l,int r)
{
    if(t[pos].l==t[pos].r)
    {
        t[pos].sum=(long long)sqrt(t[pos].sum);
        t[pos].maxn=(long long)sqrt(t[pos].maxn);
        return;
    }
    int mid=(t[pos].l+t[pos].r)/2;
    if(l<=mid&&1<t[pos<<1].maxn)
        modify(pos<<1,l,r);
    if(mid<r&&1<t[pos<<1|1].maxn)
        modify(pos<<1|1,l,r);
    t[pos].sum=t[pos<<1].sum+t[pos<<1|1].sum;
    t[pos].maxn=max(t[pos<<1].maxn,t[pos<<1|1].maxn);
}
long long query(int pos,int l,int r)
{
    if(l<=t[pos].l&&t[pos].r<=r)
        return t[pos].sum;
    long long val=0;
    int mid=(t[pos].l+t[pos].r)>>1;
    if(l<=mid)
        val+=query(pos<<1,l,r);
    if(mid<r)
        val+=query(pos<<1|1,l,r);
    return val;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        cas++;
        printf("Case #%d:\n",cas);
        memset(t,0,sizeof(t));
        for(register int i=1;i<=n;i++)
            a[i]=readl();
        build(1,1,n);
        q=read();
        while(q--)
        {
            op=read();
            l=read();
            r=read();
            if(r<l)
                swap(l,r);
            if(!op)
                modify(1,l,r);
            else
                printf("%lld\n",query(1,l,r));
        }
        printf("\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11372557.html