[ZJOI2016]线段树 解题报告
来源:互联网 发布:计算机科学和编程导论 编辑:程序博客网 时间:2024/05/16 11:15
。。很久以前看过题面然后没有仔细想,再做的时候忘了序列是随机的了。。然后怎么搞都是
我们可以将状态设为f(x,i,l,r),表示在i次操作后,[l,r]<x,l-1和r+1大于等于x的方案数。(不妨认为
然而这样的话状态数似乎是
再考虑转移,
注意到后面两个相当于是对一段区间加同一个数,类似差分一下什么的就很好搞了。
代码:
#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
- [ZJOI2016]线段树 解题报告
- bzoj4574: [Zjoi2016]线段树
- BZOJ4574: [Zjoi2016]线段树
- [ZJOI2016]线段树
- [zjoi2016][小星星][容斥]解题报告
- 【BZOJ4574】【Zjoi2016】线段树 dp
- 4574 [Zjoi2016]线段树 dp
- bzoj4574 [Zjoi2016]线段树 DP
- 线段树_POJ2528_解题报告
- bzoj 4574: [Zjoi2016]线段树 动态规划
- [bzoj4574][UOJ#196][ZJOI2016]线段树
- [DP] BZOJ 4574 [Zjoi2016]线段树
- bzoj 4574: [Zjoi2016]线段树 动态规划
- Binary Simulation 解题报告--线段树
- HDU1166 敌兵布阵 解题报告--线段树
- HDU3074 Multiply game 解题报告--线段树
- 解题报告:线段树查询 II
- 解题报告:线段树的查询
- 【LeetCode】马三来刷题之 Single Number
- Java笔记(6)-Math、BIgInteger、DecimalFormat、Pattern和Macth、Scanner、System.exit()
- 编程试事物
- 十五章上机练习1
- Swift 中 Selector 方法的访问权限控制问题 @objc
- [ZJOI2016]线段树 解题报告
- java.lang.IllegalArgument,Parse error in application web.xml file at jndi:/localhost/WEB-INF/web.xm
- js+JQuery实现返回顶部功能
- PHP的switch方法需要注意break
- 数学之美-读书笔记
- GCD使用经验与技巧浅谈
- Accp 7.0 第15章上机练习1
- 微信打开网址添加在浏览器中打开提示
- 栅格数据数据入库管理