[ZJOI2016]线段树 解题报告

来源:互联网 发布:计算机科学和编程导论 编辑:程序博客网 时间:2024/05/16 11:15

。。很久以前看过题面然后没有仔细想,再做的时候忘了序列是随机的了。。然后怎么搞都是O(n4)的。
我们可以将状态设为f(x,i,l,r),表示在i次操作后,[l,r]<x,l-1和r+1大于等于x的方案数。(不妨认为a[0]=a[n+1]=)这样的话假如说有跨越区间端点的操作,那么就一定会使操作中的数≥x。然后令g(x,j)表示让位置j最终<x的方案数,那么g(x,j)=jl=1nr=jf(x,q,l,r)xxg(x+1,j)g(x,j)就是答案了。
然而这样的话状态数似乎是O(n4)的。。但是对于a[i]=x而言,可能成为x的位置只在它左右第一个比它大的位置之间,而且数据随机。数据随机就有笛卡尔树高是O(lgn)的,那么一个简单但不严谨的想法就是可以把笛卡尔树看成一个满二叉树,那么状态数就是O(log2ni=12i1(n2i)2)=O(log2ni=1n22i)=O(n2)。一个稍微严谨一点的想法是基于随机n个数,那么第n个数最大的概率是1n,这样的话就有O(ni=1ij=2(j1k=2k1k)1jj2)=O(n2).所以总的状态数其实只有O(n2q)
再考虑转移,

f(x,i,l,r)>((l1)l2+(rl+1)(rl+2)2+(nr)(nr+1)2)f(x,i+1,l,r)>(l1)f(x,i+1,j,r) (l<xr)>(nr)f(x,i+1,l,j) (lx<r)

注意到后面两个相当于是对一段区间加同一个数,类似差分一下什么的就很好搞了。

代码:

#include<cstdio>#include<iostream>using namespace std;#include<cstring>#include<cmath>#include<algorithm>typedef long long LL;const int N=400+5,Q=400+5;int n,q;const int Mod=1e9+7;inline LL power(LL prod,int x){    LL ans=1;    for(;x;x>>=1)    {        if(x&1)ans=ans*prod%Mod;        prod=prod*prod%Mod;    }    return ans;}LL f[2][N][N];int a[N],hash[N];LL g[N][N];int stack[N];int chain[N][N];inline void cal(int L,int R,int x){    if(++L>--R)return;    //printf("---cal([%d,%d],%d)---\n",L,R,hash[x]);    int now=0,post=1;    for(int l=L;l<=R;++l)memset(f[0][l]+l,0,sizeof(LL)*(R-l+1));    f[0][L][R]=1;    for(int i=1;i<=q;++i)    {        swap(now,post);        for(int l=L;l<=R;++l)memset(f[now][l]+l,0,sizeof(LL)*(R-l+1));        for(int r=L;r<=R;++r)        {            LL s=0;            for(int l=L;l<=r;++l)            {                f[now][l][r]+=s;                s+=(l-1)*f[post][l][r];            }        }        for(int l=L;l<=R;++l)        {            LL s=0;            for(int r=R;r>=l;--r)            {                f[now][l][r]+=s;                s+=(n-r)*f[post][l][r];            }        }        for(int l=L;l<=R;++l)            for(int r=l;r<=R;++r)                (f[now][l][r]+=(((l-1)*l>>1)+((r-l+1)*(r-l+2)>>1)+((n-r)*(n-r+1)>>1))*f[post][l][r])%=Mod;        /*for(int l=L;l<=R;++l)            for(int r=l;r<=R;++r)                if(f[now][l][r])                    printf("f(%d,[%d,%d])=%I64d\n",i,l,r,f[now][l][r]);*/    }    for(int l=L;l<=R;++l)        for(int r=l;r<=R;++r)            for(int j=l;j<=r;++j)                g[x][j]+=f[now][l][r];    for(int j=L;j<=R;++j)g[x][j]%=Mod;    for(int j=L;j<=R;++j)chain[j][++chain[j][0]]=x;}int main(){    freopen("uoj196.in","r",stdin);    freopen("uoj196.out","w",stdout);    scanf("%d%d",&n,&q);    for(int i=1;i<=n;++i)    {        scanf("%d",a+i);        hash[i]=a[i];    }    sort(hash+1,hash+n+1);    int htot=unique(hash+1,hash+n+1)-hash;    for(int i=n;i;--i)a[i]=lower_bound(hash+1,hash+htot,a[i])-hash;    a[0]=htot,a[n+1]=htot,hash[htot]=-1;    int top=1;    for(int i=1;i<=n;++i)    {        for(;top&&a[stack[top-1]]<a[i];--top)cal(stack[top-1],i,a[stack[top-1]]);        if(top)        {            cal(stack[top-1],i,a[i]);            if(a[stack[top-1]]==a[i])--top;        }        stack[top++]=i;    }    while(--top)cal(stack[top],n+1,a[stack[top]]);    g[htot][1]=power(n*(n+1)>>1,q);    for(int i=n;i>1;--i)g[htot][i]=g[htot][1];    for(int i=1;i<=n;++i)    {        chain[i][++chain[i][0]]=a[i];        sort(chain[i]+1,chain[i]+chain[i][0]+1);        chain[i][0]=unique(chain[i]+1,chain[i]+chain[i][0]+1)-chain[i];        chain[i][chain[i][0]]=htot;        //printf("----%d----\n",i);        //for(int j=1;j<=chain[i][0];++j)printf("g[%d]=%I64d\n",hash[chain[i][j]],g[chain[i][j]][i]);        LL ans=0;        for(int j=1;j<chain[i][0];++j)(ans+=hash[chain[i][j]]*(g[chain[i][j+1]][i]-g[chain[i][j]][i]))%=Mod;        (ans+=Mod)%=Mod;        if(i!=1)putchar(' ');        printf("%lld",ans);        //puts("");    }    puts("");}

总结:
①一定要仔细读题,注意变量的上下界,是否有特殊约定。
②如果确定不会爆long long的话,可以最后再模,这样可以减小常数。
③#define rep这种东西感觉挺科学的,以后要学着写的。

1 0
原创粉丝点击