题目大意

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. vue2.0中ckeckbox(复选框)的使用心得,及对click事件和change的理解

    最近在公司项目中使用vue2.0做开发,在使用checkbox时遇到了一些问题,首先我们先了解一下需求. 如上如所示,在上方复选框被点击时,更改下方p标签的文本内容为:复选框已被选中.并将p标签文字颜 ...

  2. 微信小程序js学习心得体会

    微信小程序js学习心得体会 页面控制的bindtap和catchtap 用法,区别 <button id='123' data-userDate='100' bindtap='tabMessag ...

  3. windows Server 2008 r2-搭建FTP服务器

    FTP协议介绍 FTP协议工作在OSI参考模型的第七层,TCP模型的第四层上(即应用层上).使用FTP传输而不是UDP,与服务端建立连接经过三次握手. FTP端口介绍 FTP默认端口是21,.(21端 ...

  4. 扩展程序 - Google Chrome

    Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...

  5. hdu1950Bridging signals(求最长上升自序列nlogn算法)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. jquery 如何实现回顶部 带滑动效果

    $("#returnTop").click(function () { var speed=200;//滑动的速度 $('body,html').animate({ scrollT ...

  7. Java消息中间件--初级篇

    一. 为什么使用消息中间件? 假设用户登录系统   传统方式 用户登录  调用短息服务   积分服务  日志服务等各种服务  如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...

  8. day38--MySQL基础二

    1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...

  9. 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 ...

  10. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...