题目大意

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. 【转】C++ 值传递、指针传递、引用传递详解

    而关于值传递,指针传递,引用传递这几个方面还会存在误区, 所有我觉的有必要在这里也说明一下~ 下文会通过例子详细说明哦 值传递: 形参是实参的拷贝,改变形参的值并不会影响外部实参的值.从被调用函数的角 ...

  2. mysql 查询 7天内的数据

    SELECT ID,SERVICE FROM new_schedules_spider_full WHERE SERVICE = 'WSA2' and date_sub(curdate(), inte ...

  3. Python 正则表达式 search vs match

    search()和match()函数都是正则表达式中的匹配函数,二者有何区别呢? 1.match()从string的开始位置进行正则匹配,即从0位置开始匹配,若匹配上则返回一个match对象,否则返回 ...

  4. beautifulsoup解析

    beautifulsoup解析 python独有 优势:简单.便捷.高效 - 环境安装 需要将pip源设置为国内源 -需要安装:pip install bs4 bs4在使用时需要一个第三方库 pip ...

  5. Linux下Oracle JDK替换Open JDK

    Oracle的产品需要Oracle JDK,但是Linux发行版附带的都是开源的Open JDK,这里给出的方法是在不删除原有Open JDK的情况下,安装Oracle JDK 环境 系统:CentO ...

  6. 一道题目关于Java类加载

    public class B { public static B t1 = new B(); public static B t2 = new B(); { System.out.println(&q ...

  7. IOS开发---菜鸟学习之路--(五)-MacBook购买前后感想

    前几天刚入手了一台MACBOOK AIR 13寸 13版的 这几天使用过来个人感觉还是非常不错的. 这几天每天晚上都抱着她玩到十一.二点. 今天晚上突然想起来好久没续写博客了.就连忙开始码字了. 此章 ...

  8. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  9. 转:vc与界面开发之间的文章

    [很好的一篇文章,很喜欢看同行的心路历程:http://www.vckbase.com/index.php/nv/444.html] 本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的 ...

  10. Python+Selenium练习篇之9-清除文本方法

    在前面的基础篇的最后一篇,我们用到了输入字符和点击按钮这样的操作.用send_keys()来输入字符串到文本输入框这样的页面元素,用click()来点击页面上支持点击的元素.有时候,我们需要清除一个文 ...