题目大意

uoj131

分析

题目的提示还是很明显的

\(r\)相似就就代表了\(0...r-1\)相似

建出后缀树我们能dfs算出答案

再后缀和更新一下即可

注意

细节挺多的,但数据很良心

不然我就狂wa不止了

LL,权值有负等等

solution

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cmath>
using namespace std;
const int M=600007;
typedef long long LL;
const LL INF=1e9+7;
const LL oo=9223372036854775807; inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
} int n;
char s[M];
int val[M];
int ch[M][26];
int fa[M],stp[M];
int right[M];
int last,tot;
LL sz[M],mx[M],mn[M];
LL ans1[M],ans2[M]; struct edge{int y,nxt;};
struct vec{
int g[M],te;
edge e[M];
vec(){memset(g,0,sizeof(g));te=0;}
void clear(){memset(g,0,sizeof(g));te=0;}
inline void push(int x,int y){e[++te].y=y;e[te].nxt=g[x];g[x]=te;}
inline int& operator () (int &x){return g[x];}
inline edge& operator [] (int &x){return e[x];}
}go; int newnode(int ss){
stp[++tot]=ss;
return tot;
} int ext(int p,int q,int d){
int nq=newnode(stp[p]+1);
fa[nq]=fa[q]; fa[q]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
return nq;
} int sam(int p,int d){
int np=ch[p][d];
if(np) return (stp[p]+1==stp[p]) ? np : ext(p,np,d); np=newnode(stp[p]+1);
for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
if(!p) fa[np]=1;
else{
int q=ch[p][d];
fa[np]= (stp[p]+1==stp[q]) ? q :ext(p,q,d);
}
return np;
} void dfs(int x){
sz[x]=(right[x]>0);
mx[x]=(right[x]>0)?val[right[x]]:-INF;
mn[x]=(right[x]>0)?val[right[x]]:INF;
int p,y;
LL tp=0,secmx=-INF,secmn=INF;
for(p=go(x);p;p=go[p].nxt){
y=go[p].y;
dfs(y);
tp+=sz[x]*sz[y];
if(mx[y]>=mx[x]) secmx=mx[x],mx[x]=mx[y];
else if(mx[y]>secmx) secmx=mx[y];
if(mn[y]<=mn[x]) secmn=mn[x],mn[x]=mn[y];
else if(mn[y]<secmn) secmn=mn[y];
sz[x]+=sz[y];
} if(tp){
int d=stp[x];
ans1[d]+=tp;
ans2[d]=max(ans2[d],mx[x]*secmx);
ans2[d]=max(ans2[d],mn[x]*secmn);
}
} int main(){ int i; n=rd();
scanf("%s",s+1);
for(i=1;i<=n;i++) val[i]=rd(); last=tot=1;
for(i=n;i>0;i--){
last=sam(last,s[i]-'a');
right[last]=i;
} for(i=2;i<=tot;i++) go.push(fa[i],i);
for(i=0;i<=n;i++) ans2[i]=-oo; dfs(1); for(i=n-1;i>=0;i--){
ans1[i]+=ans1[i+1];
ans2[i]=max(ans2[i],ans2[i+1]);
}
for(i=n-1;i>=0;i--) if(ans1[i]==0) ans2[i]=0; for(i=0;i<n;i++) printf("%lld %lld\n",ans1[i],ans2[i]); return 0;
}

uoj 131/bzoj 4199 [NOI2015]品酒大会 后缀树+树d的更多相关文章

  1. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  2. BZOJ.4199.[NOI2015]品酒大会(后缀自动机 树形DP)

    BZOJ 洛谷 后缀数组做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 只考虑求极长相同子串,即所有后缀之间的LCP. 而后缀的LCP在后缀树的LCA处.同差异这道题,在每个点处 ...

  3. BZOJ.4199.[NOI2015]品酒大会(后缀数组 单调栈)

    BZOJ 洛谷 后缀自动机做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 显然只需要考虑极长的相同子串的贡献,然后求后缀和/后缀\(\max\)就可以了. 对于相同子串,我们能想 ...

  4. BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )

    求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...

  5. bzoj 4199: [Noi2015]品酒大会 后缀树

    题目大意: 给定一个长为n的字符串,每个下标有一个权\(w_i\),定义下标\(i,j\)是r相似的仅当\(r \leq LCP(suf(i),suf(j))\)且这个相似的权为\(w_i,w_j\) ...

  6. BZOJ 4199: [Noi2015]品酒大会 后缀自动机_逆序更新

    一道裸题,可以考虑自底向上去更新方案数与最大值. 没啥难的 细节........ Code: #include <cstdio> #include <algorithm> #i ...

  7. bzoj 4199: [Noi2015]品酒大会

    Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...

  8. 【刷题】BZOJ 4199 [Noi2015]品酒大会

    Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...

  9. BZOJ 4199 [Noi2015]品酒大会:后缀数组 + 并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4199 题意: 给你一个长度为n的字符串s,和一个长为n的数组v. 对于每个整数r∈[0,n ...

随机推荐

  1. runtime消息转发机制

    Objective-C 扩展了 C 语言,并加入了面向对象特性和 Smalltalk 式的消息传递机制.而这个扩展的核心是一个用 C 和 编译语言 写的 Runtime 库.它是 Objective- ...

  2. Perl_实用报表提取语言

    Perl 语法 - 基础   perl语言的核心是正则表达式,在文本处理上非常有优势,与python类似,但语法不同,perl的语法很灵活,用多了才会觉得好用. 常用知识点总结: perl语法类似于C ...

  3. mysql 复制一列到另一列

    https://www.cnblogs.com/clphp/p/6251469.html

  4. MySQL解决中文编码问题

    转载组员博客 地址:MySQL解决中文编码问题

  5. arduino 语音音箱 :语音控制、MP3播放、报时、回复温湿度情况

    arduino 语音音箱 :语音控制.MP3播放.报时.回复温湿度情况 效果图 线路图 包装后的效果 功能 需要材料 arduino板 MP3播放模块及喇叭 时钟模块 温湿度模块 语音识别模块 面包板 ...

  6. [译]The Python Tutorial#1. Whetting Your Appetite

    [译]The Python Tutorial#Whetting Your Appetite 1. Whetting Your Appetite 如果你需要使用计算机做很多工作,最终会发现很多任务需要自 ...

  7. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  8. 笔记-Python-cProfile

    笔记-Python-cProfile 1. 简介python官方提供了cProfile和profile对程序进行性能分析,建议使用cProfile; cProfile:基于lsprof的用C语言实现的 ...

  9. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  10. proget Android代码混淆

    混淆的时候,还要添加Android.jar,不然,你的程序一篇空白.我就吃了亏. 还有,activity是不能混淆的,因为AndroidMeaxinfast.xml里面会找他.