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 ...
随机推荐
- 01_14_Struts2_结果类型_result_type
01_14_Struts2_结果类型_result_type 1. result类型 result类型 说明 dispatcher 默认服务端转发jsp chain 服务端action转发 redir ...
- Binary Agents-freecodecamp算法题目
Binary Agents 1.要求 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 2.思路 用.split(' ')将输入二进制字符串转化为各个二进制数字符串组成的数组 用 ...
- 第二篇:ssh.invoke_shell() 切换root出现的新问题
接上一篇:按照上一篇的方式,在没有对ssh.invoke_shell()执行后的登录提示符进行判断的话,那边有部分机器就回因为返回为空导致程序卡死. 正常机器 ssh.recv(9999) 命令返 ...
- Zimber 8.8.12卸载后重新安装报错解决办法
1.1 zimber故障处理步骤 1.1.1 现象描述 Running Post Installation Configuration: /opt/zimbra/bin/zmlocalconfig ...
- h5中的video与audio
·首先带大家熟悉一下video标签的属性方法,根据属性方法做一个小demo, HTML5支持的视频格式: Ogg 带有Theora视频编码+Ogg文件 支持的浏览器:F.O MEPG4 带有H.26 ...
- ubuntu crontab设置定时任务
ubuntu 设置定时任务 crontab -l #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用* ...
- 微信小程序第3课 目录结构及小知识点
目录 目录结构 安装包下载地址 一.pages目录介绍 二.index目录介绍 index.js(相当JavaScript文件,必不可少的) index.json(可以不需要) index.wxml( ...
- 数据结构-单链表(Linked List)
#include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...
- Sonya and Robots CodeForces - 1004C (思维题)
Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...
- P2920 [USACO08NOV]时间管理Time Management
P2920 [USACO08NOV]时间管理Time Management 题目描述 Ever the maturing businessman, Farmer John realizes that ...