uoj 131/bzoj 4199 [NOI2015]品酒大会 后缀树+树d
题目大意
分析
题目的提示还是很明显的
\(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的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ.4199.[NOI2015]品酒大会(后缀自动机 树形DP)
BZOJ 洛谷 后缀数组做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 只考虑求极长相同子串,即所有后缀之间的LCP. 而后缀的LCP在后缀树的LCA处.同差异这道题,在每个点处 ...
- BZOJ.4199.[NOI2015]品酒大会(后缀数组 单调栈)
BZOJ 洛谷 后缀自动机做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 显然只需要考虑极长的相同子串的贡献,然后求后缀和/后缀\(\max\)就可以了. 对于相同子串,我们能想 ...
- BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )
求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...
- bzoj 4199: [Noi2015]品酒大会 后缀树
题目大意: 给定一个长为n的字符串,每个下标有一个权\(w_i\),定义下标\(i,j\)是r相似的仅当\(r \leq LCP(suf(i),suf(j))\)且这个相似的权为\(w_i,w_j\) ...
- BZOJ 4199: [Noi2015]品酒大会 后缀自动机_逆序更新
一道裸题,可以考虑自底向上去更新方案数与最大值. 没啥难的 细节........ Code: #include <cstdio> #include <algorithm> #i ...
- bzoj 4199: [Noi2015]品酒大会
Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...
- 【刷题】BZOJ 4199 [Noi2015]品酒大会
Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...
- BZOJ 4199 [Noi2015]品酒大会:后缀数组 + 并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4199 题意: 给你一个长度为n的字符串s,和一个长为n的数组v. 对于每个整数r∈[0,n ...
随机推荐
- vue2.0中ckeckbox(复选框)的使用心得,及对click事件和change的理解
最近在公司项目中使用vue2.0做开发,在使用checkbox时遇到了一些问题,首先我们先了解一下需求. 如上如所示,在上方复选框被点击时,更改下方p标签的文本内容为:复选框已被选中.并将p标签文字颜 ...
- 微信小程序js学习心得体会
微信小程序js学习心得体会 页面控制的bindtap和catchtap 用法,区别 <button id='123' data-userDate='100' bindtap='tabMessag ...
- windows Server 2008 r2-搭建FTP服务器
FTP协议介绍 FTP协议工作在OSI参考模型的第七层,TCP模型的第四层上(即应用层上).使用FTP传输而不是UDP,与服务端建立连接经过三次握手. FTP端口介绍 FTP默认端口是21,.(21端 ...
- 扩展程序 - Google Chrome
Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...
- hdu1950Bridging signals(求最长上升自序列nlogn算法)
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- jquery 如何实现回顶部 带滑动效果
$("#returnTop").click(function () { var speed=200;//滑动的速度 $('body,html').animate({ scrollT ...
- Java消息中间件--初级篇
一. 为什么使用消息中间件? 假设用户登录系统 传统方式 用户登录 调用短息服务 积分服务 日志服务等各种服务 如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...
- day38--MySQL基础二
1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...
- leetcode 【Search a 2D Matrix 】python 实现
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- leetcode 【 Remove Element 】python 实现
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...