zoukankan      html  css  js  c++  java
  • luogu P5294 [HNOI2019]序列

    传送门

    这个什么鬼证明直接看uoj的题解吧根本不会证明

    首先方案一定是若干段等值的(B),然后对于一段,(B)的值应该是(A)的平均值.这个最优方案是可以线性构造的,也就是维护以区间平均值为权值的单调栈,每次在后面插入一个元素,不断弹栈并与最后一个合并,直到平均值单调递增

    然后这个单调栈是可以两个区间的单调栈直接合并的,因为合并完后新单调栈的断点集合是原来两段的断点集合的子集.合并直接暴力就好了合并的话一定是前面那个的一段后缀的后面的一段前缀合并,然后后面的前缀位置(就是合并区间的右端点)是可以二分的,然后二分左端点使得合并的区间平均值最大,然后如果这个平均值比右端点右边的那一块的平均值小,那么最优的右端点就在在这个右端点以及其左边,否则在右边

    然后用个可持久化线段树预处理一下每个前缀和后缀的单调栈就行了,注意从后往前和从前往后做都是一样的,能得到最优解

    以上都是蒯的题解qwq,感性理解一下?

    // luogu-judger-enable-o2
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<map>
    #include<set>
    #define LL long long
    #define db long double
    
    using namespace std;
    const int N=1e5+10,mod=998244353;
    const db eps=1e-10;
    int rd()
    {
        int x=0,w=1;char ch=0;
        while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
        return x*w;
    }
    int fpow(int a,int b){int an=1;while(b){if(b&1) an=1ll*an*a%mod;a=1ll*a*a%mod,b>>=1;} return an;}
    int inv(int a){return fpow(a,mod-2);}
    LL sm[N*85],rsm[N*85];
    int n,m,st[N],tp,a[N],ps1[N],ps2[N],an[N*85],nm[N*85],rnm[N*85],ch[N*85][2],len1[N],len2[N],rt1[N],rt2[N],tt;
    LL sta[N][2];
    void inst(int o1,int o2,int x,int ll,int rr,LL ns,int nn)
    {
        int l=1,r=n,tl=0;
        while(l<r)
        {
            st[++tl]=o1;
            int mid=(l+r)>>1;
            if(x<=mid)
            {
                ch[o1][0]=++tt,ch[o1][1]=ch[o2][1];
                o1=ch[o1][0],o2=ch[o2][0];
                r=mid;
            }
            else
            {
                ch[o1][0]=ch[o2][0],ch[o1][1]=++tt;
                o1=ch[o1][1],o2=ch[o2][1];
                l=mid+1;
            }
        }
        sm[o1]=rsm[o1]=ns,nm[o1]=rnm[o1]=nn;
        int ax=1ll*ns%mod*inv(nn%mod)%mod;
        an[o1]=nn?(1ll*(ps2[rr]-ps2[ll-1]+mod)%mod+1ll*ax*ax%mod*nn%mod-2ll*(ps1[rr]-ps1[ll-1]+mod)%mod*ax%mod)%mod:0;
        an[o1]+=an[o1]<0?mod:0;
        while(tl)
        {
            int o=st[tl--];
            sm[o]=sm[ch[o][0]]+sm[ch[o][1]],nm[o]=nm[ch[o][0]]+nm[ch[o][1]],an[o]=(an[ch[o][0]]+an[ch[o][1]])%mod;
            rsm[o]=rsm[ch[o][1]],rnm[o]=rnm[ch[o][1]];
        }
    }
    struct node
    {
        LL x;
        int y;
        node(){x=y=0;}
        node(LL ns,int nn){x=ns,y=nn;}
        node operator + (const node &bb) const {return node(x+bb.x,y+bb.y);}
    };
    node quer(int o,int l,int r,int ll,int rr)
    {
        if(ll>rr||!o) return node(0,0);
        if(ll<=l&&r<=rr) return node(sm[o],nm[o]);
        int mid=(l+r)>>1;
        node an;
        if(ll<=mid) an=an+quer(ch[o][0],l,mid,ll,rr);
        if(rr>mid) an=an+quer(ch[o][1],mid+1,r,ll,rr);
        return an;
    }
    node quer(int o,int l,int r,int lx)
    {
        if(!o) return node(0,0);
        if(l==r) return node(sm[o],nm[o]);
        int mid=(l+r)>>1;
        if(lx<=mid) return quer(ch[o][0],l,mid,lx);
        return quer(ch[o][1],mid+1,r,lx);
    }
    int getan(int o,int l,int r,int ll,int rr)
    {
        if(ll>rr||!o) return 0;
        if(ll<=l&&r<=rr) return an[o];
        int mid=(l+r)>>1,an=0;
        if(ll<=mid) an+=getan(ch[o][0],l,mid,ll,rr);
        if(rr>mid) an+=getan(ch[o][1],mid+1,r,ll,rr);
        return an%mod;
    }
    int cmp(LL s1,LL n1,LL s2,LL n2)
    {
        s1*=n2,s2*=n1;
        if(s1==s2) return 0;
        return s1>s2?1:-1;
    }
    int nl;
    node querl(int o,node aa)
    {
        node an;
        int l=1,r=n;
        while(l<r)
        {
            int mid=(l+r)>>1;
            node ar=aa+an+node(sm[ch[o][1]],nm[ch[o][1]]),al=node(rsm[ch[o][0]],rnm[ch[o][0]])+ar;
            if(cmp(al.x,al.y,ar.x,ar.y)<0) o=ch[o][1],l=mid+1;
            else an=an+node(sm[ch[o][1]],nm[ch[o][1]]),o=ch[o][0],r=mid;
        }
        node ar=aa+an,al=node(sm[o],nm[o])+ar;
        if(cmp(al.x,al.y,ar.x,ar.y)>0) an=an+node(sm[o],nm[o]);
        nl=l;
        return an;
    }
    
    int main()
    {
        n=rd(),m=rd();
        for(int i=1;i<=n;++i)
        {
            a[i]=rd();
            ps1[i]=(ps1[i-1]+a[i])%mod;
            ps2[i]=(ps2[i-1]+1ll*a[i]%mod*a[i]%mod)%mod;
        }
        for(int i=1;i<=n;++i)
        {
            int las=tp;
            sta[++tp][0]=a[i],sta[tp][1]=1;
            while(tp>1&&cmp(sta[tp-1][0],sta[tp-1][1],sta[tp][0],sta[tp][1])>=0)
                sta[tp-1][0]+=sta[tp][0],sta[tp-1][1]+=sta[tp][1],--tp;
            rt1[i]=rt1[i-1];
            for(int j=las;j>=tp;--j)
            {
                int la=rt1[i];
                inst(rt1[i]=++tt,la,j,j,j,0,0);
            }
            int la=rt1[i];
            inst(rt1[i]=++tt,la,tp,i-sta[tp][1]+1,i,sta[tp][0],sta[tp][1]);
            len1[i]=tp;
        }
        tp=0;
        for(int i=n;i;--i)
        {
            int las=tp;
            sta[++tp][0]=a[i],sta[tp][1]=1;
            while(tp>1&&cmp(sta[tp-1][0],sta[tp-1][1],sta[tp][0],sta[tp][1])<=0)
                sta[tp-1][0]+=sta[tp][0],sta[tp-1][1]+=sta[tp][1],--tp;
            rt2[i]=rt2[i+1];
            for(int j=las;j>=tp;--j)
            {
                int la=rt2[i];
                inst(rt2[i]=++tt,la,j,j,j,0,0);
            }
            int la=rt2[i];
            inst(rt2[i]=++tt,la,tp,i,i+sta[tp][1]-1,sta[tp][0],sta[tp][1]);
            len2[i]=tp;
        }
        printf("%d
    ",an[rt1[n]]);
        while(m--)
        {
            int x=rd(),y=rd();
            int l=1,r=len2[x+1],z=0;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                node a1=quer(rt2[x+1],1,n,mid,len2[x+1])+node(y,1),a2=mid>1?quer(rt2[x+1],1,n,mid-1):node(1e14,1);
                if(cmp(a1.x,a1.y,a2.x,a2.y)<0) z=mid,l=mid+1;
                else r=mid-1;
            }
            int z1=len1[x-1]+1,z2=0;
            l=1,r=z+1;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                node a1=quer(rt2[x+1],1,n,mid,len2[x+1])+node(y,1);
                node a2=querl(rt1[x-1],a1),a3=mid>1?quer(rt2[x+1],1,n,mid-1):node(1e14,1);
                if(cmp((a1+a2).x,(a1+a2).y,a3.x,a3.y)<0) z1=nl,z2=mid,l=mid+1;
                else r=mid-1;
            }
            node a1=quer(rt1[x-1],1,n,z1,len1[x-1]),a2=quer(rt2[x+1],1,n,z2,len2[x+1]),aa=a1+a2+node(y,1);
            int ll=x-a1.y,rr=x+a2.y,ax=1ll*aa.x%mod*inv(aa.y%mod)%mod;
            int ans=(((1ll*ps2[rr]-ps2[ll-1]-1ll*a[x]*a[x]+1ll*y*y)%mod+mod)%mod+1ll*ax*ax%mod*(rr-ll+1)%mod-2ll*(1ll*ps1[rr]-ps1[ll-1]-a[x]+y+mod+mod)%mod*ax%mod)%mod;
            ans+=ans<0?mod:0;
            printf("%lld
    ",(1ll*ans+getan(rt1[x-1],1,n,1,z1-1)+getan(rt2[x+1],1,n,1,z2-1))%mod);
        }
        return 0;
    }
    
  • 相关阅读:
    内核随记(三)同步(1)
    排列算法
    SQLite入门与分析(八)存储模型(3)
    内核随记(一)——理解中断(2)
    dup与dup2系统调用
    内核随记(四)文件系统(1)
    SQLite入门与分析(八)存储模型(2)
    SQLite入门与分析(九)VACUUM命令分析
    c中static变量局部变量
    (i++)+(i++)与(++i)+(++i)
  • 原文地址:https://www.cnblogs.com/smyjr/p/10713361.html
Copyright © 2011-2022 走看看