codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)

 

 

 

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
const ll INF=1e18;
ll sum1[maxn],sum2[maxn],treea[4*maxn],treeb[4*maxn],lazya[4*maxn],lazyb[4*maxn];
int a[maxn];
void bulida(int l,int r,int rt)
{
    int md=(r+l)/2;
    if(r==l)
    {
        treea[rt]=a[l];
        return;
    }
    bulida(l,md,rt*2);
    bulida(md+1,r,rt*2+1);
    treea[rt]=treea[rt*2]+treea[rt*2+1];
}
void pushdowna(int l,int r,int rt)
{
    int md=(l+r)/2;
    if(lazya[rt]!=-INF)
    {
        treea[rt*2]=(md-l+1)*lazya[rt];
        treea[rt*2+1]=(r-md-1+1)*lazya[rt];
        lazya[rt*2]=lazya[rt*2+1]=lazya[rt];
        lazya[rt]=-INF;
    }
}
ll quera(int l,int r,int nowl,int nowr,int rt)
{
    if(r<nowl||l>nowr)return 0;
    int md=(nowr+nowl)/2;
    if(l<=nowl&&r>=nowr)return treea[rt];
    pushdowna(nowl,nowr,rt);
    return quera(l,r,nowl,md,rt*2)+quera(l,r,md+1,nowr,rt*2+1);
}
void updataa(ll x,int l,int r,int nowl,int nowr,int rt)
{
    if(r<nowl||l>nowr)return ;
    int md=(nowr+nowl)/2;
    if(l<=nowl&&r>=nowr)
    {
        treea[rt]=(nowr-nowl+1)*x;
        lazya[rt]=x;
        return ;
    }
    pushdowna(nowl,nowr,rt);
    updataa(x,l,r,nowl,md,rt*2);
    updataa(x,l,r,md+1,nowr,rt*2+1);
    treea[rt]=treea[rt*2]+treea[rt*2+1];
}

void pushdownb(int l,int r,int rt)
{
    int md=(l+r)/2;
    if(lazyb[rt]!=-INF)
    {
        treeb[rt*2]=sum2[md-1]-sum2[l-2]+(l-md-1)*sum1[lazyb[rt]-1];
        treeb[rt*2+1]=sum2[r-1]-sum2[md+1-2]+(md+1-r-1)*sum1[lazyb[rt]-1];
        lazyb[rt*2]=lazyb[rt*2+1]=lazyb[rt];
        lazyb[rt]=-INF;
    }
}
void updatabb(ll x,int pos,int nowl,int nowr,int rt)
{
    int md=(nowr+nowl)/2;
    if(nowl==nowr)
    {
        treeb[rt]=x;
        return ;
    }
    pushdownb(nowl,nowr,rt);
    if(pos>=md+1)updatabb(x,pos,md+1,nowr,rt*2+1);
    else updatabb(x,pos,nowl,md,rt*2);
    treeb[rt]=treeb[rt*2]+treeb[rt*2+1];
}
ll querb(int l,int r,int nowl,int nowr,int rt)
{
    if(r<nowl||l>nowr)return 0;
    int md=(nowr+nowl)/2;
    if(l<=nowl&&r>=nowr)return treeb[rt];
    pushdownb(nowl,nowr,rt);
    return querb(l,r,nowl,md,rt*2)+querb(l,r,md+1,nowr,rt*2+1);
}
void updatab(ll x,int l,int r,int nowl,int nowr,int rt)
{
    if(r<nowl||l>nowr)return ;
    int md=(nowr+nowl)/2;
    if(l<=nowl&&r>=nowr)
    {
        treeb[rt]=sum2[nowr-1]-sum2[nowl-2]+(nowl-nowr-1)*sum1[x-1];
        lazyb[rt]=x;
        return ;
    }
    pushdownb(nowl,nowr,rt);
    updatab(x,l,r,nowl,md,rt*2);
    updatab(x,l,r,md+1,nowr,rt*2+1);
    treeb[rt]=treeb[rt*2]+treeb[rt*2+1];
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i=1; i<=n-1; i++)
    {
        int x;
        scanf("%d",&x);
        sum1[i]=sum1[i-1]+x;
        sum2[i]=sum2[i-1]+sum1[i];
    }
    for(int i=0; i<4*maxn; i++)lazya[i]=lazyb[i]=-INF;
    bulida(1,n,1);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        getchar();
        char key;
        scanf("%c",&key);
        if(key=='s')
        {
            int l,r;
            scanf("%d %d",&l,&r);
            printf("%lld\n",quera(l,r,1,n,1)+querb(l,r,1,n,1));
        }
        else if(key=='+')
        {
            ll x,add;
            scanf("%lld %lld",&x,&add);
            add=quera(x,x,1,n,1)+querb(x,x,1,n,1)+add;
            int st=x,en=n;
            while(st!=en)
            {
                int md=(st+en)/2;
                if(sum1[md+1-1]-sum1[x-1]+add>=querb(md+1,md+1,1,n,1)+quera(md+1,md+1,1,n,1))st=md+1;
                else en=md;
            }
            updataa(add,x,st,1,n,1);
            updatab(x,x+1,st,1,n,1);
            updatabb(0,x,1,n,1);
        }
    }
    return 0;
}

  

上一篇:洛谷 P3658 [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)


下一篇:[HDU6333]Problem B. Harvest of Apples