[NOI2015] 品酒大会 题解
题目大意
给定一个长度为
对于每一个
以及所有的
题解
1
可以转化为倒序枚举
求后缀和、 后缀最大值
2
由于
实际上就是要求区间最小值为
3
将
每次在
用并查集维护大小、最值即可。
code
- #include
- using namespace std;
- typedef long long LL;
- const int N = 3e5 + 5;
- int n, sa[N], rk[N], old[N], t[N], id[N], m, h[N];
- int fa[N], sz[N];
- LL a[N], mx[N], mn[N], s1[N], s2[N], cnt, res = -1e18;
- char str[N];
- inline void rs() {
- for (int i = 1; i <= m; i++) t[i] = 0;
- for (int i = 1; i <= n; i++) ++t[rk[i]];
- for (int i = 1; i <= m; i++) t[i] += t[i - 1];
- for (int i = n; i >= 1; i--) sa[t[rk[id[i]]]--] = id[i], id[i] = 0;
- }
- inline int EQ(int x, int y, int k)
- { return old[x] == old[y] && old[x + k] == old[y + k]; }
- inline void bui() {
- m = 200;
- for (int i = 1; i <= n; i++) rk[i] = str[i], id[i] = i;
- rs();
- for (int k = 1, p; k <= n; k <<= 1) {
- p = 0;
- for (int i = n - k + 1; i <= n; i++) id[++p] = i;
- for (int i = 1; i <= n; i++) if (sa[i] > k) id[++p] = sa[i] - k;
- rs(), memcpy(old, rk, sizeof(rk)), p = 0;
- for (int i = 1; i <= n; i++) rk[sa[i]] = EQ(sa[i], sa[i - 1], k) ? p : ++p;
- if (p == n) break;
- m = p;
- }
- for (int i = 1, j, k = 0; i <= n; h[rk[i++]] = k)
- for (k ? --k : 0, j = sa[rk[i] - 1]; str[i + k] == str[j + k]; k++);
- }
- int fd(int x) {
- while (fa[x] ^ x) x = fa[x] = fa[fa[x]];
- return x;
- }
- vector<int> b[N];
- inline void mer(int x, int y) {
- x = fd(x), y = fd(y);
- cnt += 1ll * sz[x] * sz[y], res = max(res, max(mx[x] * mx[y], mn[x] * mn[y]));
- fa[y] = x, sz[x] += sz[y], mx[x] = max(mx[x], mx[y]), mn[x] = min(mn[x], mn[y]);
- }
- int main() {
- scanf("%d%s", &n, str + 1);
- for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
- bui();
- for (int i = 1; i <= n; i++) {
- fa[i] = i, sz[i] = 1, mx[i] = mn[i] = a[sa[i]];
- b[h[i]].push_back(i);
- }
- for (int i = n - 1; ~i; i--) {
- // for (int x : b[i]) mer(x, x - 1);
- int le = b[i].size();
- for (int j = 0; j < le; j++) mer(b[i][j], b[i][j] - 1);
- if (cnt) s1[i] = cnt, s2[i] = res;
- }
- for (int i = 0; i < n; i++) printf("%lld %lld\n", s1[i], s2[i]);
- }