题目大意

uoj131

分析

题目的提示还是很明显的

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

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

再后缀和更新一下即可

注意

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

不然我就狂wa不止了

LL,权值有负等等

solution

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <cmath>
  7. using namespace std;
  8. const int M=600007;
  9. typedef long long LL;
  10. const LL INF=1e9+7;
  11. const LL oo=9223372036854775807;
  12. inline int rd(){
  13. int x=0;bool f=1;char c=getchar();
  14. for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
  15. for(;isdigit(c);c=getchar()) x=x*10+c-48;
  16. return f?x:-x;
  17. }
  18. int n;
  19. char s[M];
  20. int val[M];
  21. int ch[M][26];
  22. int fa[M],stp[M];
  23. int right[M];
  24. int last,tot;
  25. LL sz[M],mx[M],mn[M];
  26. LL ans1[M],ans2[M];
  27. struct edge{int y,nxt;};
  28. struct vec{
  29. int g[M],te;
  30. edge e[M];
  31. vec(){memset(g,0,sizeof(g));te=0;}
  32. void clear(){memset(g,0,sizeof(g));te=0;}
  33. inline void push(int x,int y){e[++te].y=y;e[te].nxt=g[x];g[x]=te;}
  34. inline int& operator () (int &x){return g[x];}
  35. inline edge& operator [] (int &x){return e[x];}
  36. }go;
  37. int newnode(int ss){
  38. stp[++tot]=ss;
  39. return tot;
  40. }
  41. int ext(int p,int q,int d){
  42. int nq=newnode(stp[p]+1);
  43. fa[nq]=fa[q]; fa[q]=nq;
  44. memcpy(ch[nq],ch[q],sizeof(ch[q]));
  45. for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
  46. return nq;
  47. }
  48. int sam(int p,int d){
  49. int np=ch[p][d];
  50. if(np) return (stp[p]+1==stp[p]) ? np : ext(p,np,d);
  51. np=newnode(stp[p]+1);
  52. for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
  53. if(!p) fa[np]=1;
  54. else{
  55. int q=ch[p][d];
  56. fa[np]= (stp[p]+1==stp[q]) ? q :ext(p,q,d);
  57. }
  58. return np;
  59. }
  60. void dfs(int x){
  61. sz[x]=(right[x]>0);
  62. mx[x]=(right[x]>0)?val[right[x]]:-INF;
  63. mn[x]=(right[x]>0)?val[right[x]]:INF;
  64. int p,y;
  65. LL tp=0,secmx=-INF,secmn=INF;
  66. for(p=go(x);p;p=go[p].nxt){
  67. y=go[p].y;
  68. dfs(y);
  69. tp+=sz[x]*sz[y];
  70. if(mx[y]>=mx[x]) secmx=mx[x],mx[x]=mx[y];
  71. else if(mx[y]>secmx) secmx=mx[y];
  72. if(mn[y]<=mn[x]) secmn=mn[x],mn[x]=mn[y];
  73. else if(mn[y]<secmn) secmn=mn[y];
  74. sz[x]+=sz[y];
  75. }
  76. if(tp){
  77. int d=stp[x];
  78. ans1[d]+=tp;
  79. ans2[d]=max(ans2[d],mx[x]*secmx);
  80. ans2[d]=max(ans2[d],mn[x]*secmn);
  81. }
  82. }
  83. int main(){
  84. int i;
  85. n=rd();
  86. scanf("%s",s+1);
  87. for(i=1;i<=n;i++) val[i]=rd();
  88. last=tot=1;
  89. for(i=n;i>0;i--){
  90. last=sam(last,s[i]-'a');
  91. right[last]=i;
  92. }
  93. for(i=2;i<=tot;i++) go.push(fa[i],i);
  94. for(i=0;i<=n;i++) ans2[i]=-oo;
  95. dfs(1);
  96. for(i=n-1;i>=0;i--){
  97. ans1[i]+=ans1[i+1];
  98. ans2[i]=max(ans2[i],ans2[i+1]);
  99. }
  100. for(i=n-1;i>=0;i--) if(ans1[i]==0) ans2[i]=0;
  101. for(i=0;i<n;i++) printf("%lld %lld\n",ans1[i],ans2[i]);
  102. return 0;
  103. }

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. 01_14_Struts2_结果类型_result_type

    01_14_Struts2_结果类型_result_type 1. result类型 result类型 说明 dispatcher 默认服务端转发jsp chain 服务端action转发 redir ...

  2. Binary Agents-freecodecamp算法题目

    Binary Agents 1.要求 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 2.思路 用.split(' ')将输入二进制字符串转化为各个二进制数字符串组成的数组 用 ...

  3. 第二篇:ssh.invoke_shell() 切换root出现的新问题

    接上一篇:按照上一篇的方式,在没有对ssh.invoke_shell()执行后的登录提示符进行判断的话,那边有部分机器就回因为返回为空导致程序卡死. 正常机器  ssh.recv(9999)  命令返 ...

  4. Zimber 8.8.12卸载后重新安装报错解决办法

    1.1  zimber故障处理步骤 1.1.1  现象描述 Running Post Installation Configuration: /opt/zimbra/bin/zmlocalconfig ...

  5. h5中的video与audio

    ·首先带大家熟悉一下video标签的属性方法,根据属性方法做一个小demo, HTML5支持的视频格式: Ogg 带有Theora视频编码+Ogg文件 支持的浏览器:F.O MEPG4  带有H.26 ...

  6. ubuntu crontab设置定时任务

    ubuntu 设置定时任务   crontab -l  #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用* ...

  7. 微信小程序第3课 目录结构及小知识点

    目录 目录结构 安装包下载地址 一.pages目录介绍 二.index目录介绍 index.js(相当JavaScript文件,必不可少的) index.json(可以不需要) index.wxml( ...

  8. 数据结构-单链表(Linked List)

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...

  9. Sonya and Robots CodeForces - 1004C (思维题)

    Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...

  10. P2920 [USACO08NOV]时间管理Time Management

    P2920 [USACO08NOV]时间管理Time Management 题目描述 Ever the maturing businessman, Farmer John realizes that ...