版本比较
「题单题解」后缀数组
正文
增加 1 行删除 1 行
7 行未改动891011121314151623 行未改动7 行未改动=&\sum_{1\le i \lt j \le n} |suf_i|+|suf_j|-2\sum_{1 \le i \lt j \le n}\text{lcp}(suf_i,suf_j)\\=&\dfrac{n(n-1)(n+1)} 2-2\sum_{1 \le i \lt j \le n}\text{lcp}(suf_{sa_i},suf_{sa_j})\\=&\dfrac{n(n-1)(n+1)} 2-2\sum_{1 \le i \lt j \le n} \min_{k=i+1}^j ht_k\\\end{aligned}$$求出 $ht$ 数组后直接枚举 $j$ 并利用单调栈计算即可。时间复杂度 $\mathcal O(n \log n)$。## [P2463 [SDOI2008] Sandy 的卡片](https://www.luogu.com.cn/problem/P2463)23 行未改动7 行未改动891011121314151623 行未改动7 行未改动=&\sum_{1\le i \lt j \le n} |suf_i|+|suf_j|-2\sum_{1 \le i \lt j \le n}\text{lcp}(suf_i,suf_j)\\=&\dfrac{n(n-1)(n+1)} 2-2\sum_{1 \le i \lt j \le n}\text{lcp}(suf_{sa_i},suf_{sa_j})\\=&\dfrac{n(n-1)(n+1)} 2-2\sum_{1 \le i \lt j \le n} \min_{k=i+1}^j ht_k\\\end{aligned}$$求出 $ht$ 数组后直接枚举 $j$ 并利用单调栈计算即可。时间复杂度 $\mathcal O(n \log n)$。## [P2463 [SDOI2008] Sandy 的卡片](https://www.luogu.com.cn/problem/P2463)23 行未改动