voidget_sa(char *str, int *sa, int n) { staticint c[N], x[N], y[N]; int m = 126, num; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i] = str[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[i]] --] = i; for (int k = 1; k <= n; k <<= 1) { num = 0; for (int i = n - k + 1; i <= n; ++ i) y[++ num] = i; for (int i = 1; i <= n; ++ i) if (sa[i] > k) y[++ num] = sa[i] - k; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[y[i]]] --] = y[i]; for (int i = 1; i <= n; ++ i) y[i] = 0; swap(x, y); x[sa[1]] = 1, num = 1; for (int i = 2; i <= n; ++ i) if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) x[sa[i]] = num; else x[sa[i]] = ++ num; if (num == n) return; m = num; } }
typedeflonglong LL; typedef pair<LL, LL> PLL; constint N = 3e5 + 10; const LL INF = 2e18; char str[N]; int sa[N], n, height[N], fa[N], a[N]; int mx1[N], mx2[N], mn1[N], mn2[N], sz[N]; LL cnt, res = -INF; PLL ans[N]; vector<int> com[N];
voidget_sa(char *str, int *sa, int n) { staticint c[N], x[N], y[N]; int m = 126, num; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i] = str[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[i]] --] = i; for (int k = 1; k <= n; k <<= 1) { num = 0; for (int i = n - k + 1; i <= n; ++ i) y[++ num] = i; for (int i = 1; i <= n; ++ i) if (sa[i] > k) y[++ num] = sa[i] - k; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[y[i]]] --] = y[i]; swap(x, y); x[sa[1]] = 1, num = 1; for (int i = 2; i <= n; ++ i) if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) x[sa[i]] = num; else x[sa[i]] = ++ num; if (num == n) return; m = num; } }
voidget_height(char *str, int *sa, int *height, int n) { staticint rk[N]; for (int i = 1; i <= n; ++ i) rk[sa[i]] = i; for (int i = 1; i <= n; ++ i) { if (rk[i] == 1) continue; int j = sa[rk[i] - 1], k = max(0, height[rk[i - 1]] - 1); while (i + k <= n && j + k <= n && str[i + k] == str[j + k]) k ++; height[rk[i]] = k; } }
intmain() { scanf("%d%s", &n, str + 1); for (int i = 1; i <= n; ++ i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++ i) fa[i] = i, sz[i] = 1; get_sa(str, sa, n), get_height(str, sa, height, n); for (int i = 1; i <= n; ++ i) mx1[i] = mn1[i] = a[sa[i]], mx2[i] = -1e9, mn2[i] = 1e9; for (int i = 2; i <= n; ++ i) com[height[i]].push_back(i); for (int i = n - 1; ~i; -- i) ans[i] = solve(i); for (int i = 0; i < n; ++ i) printf("%lld %lld\n", ans[i].first, ans[i].second); return0; }
typedeflonglong LL; constint N = 1e5 + 10; int str[N]; int sa[N], n, m, height[N], rk[N]; unordered_map<int, int> dis; LL ans[N], res; int d[N], u[N];
voidget_sa(int *str, int *sa, int n) { staticint c[N], x[N], y[N]; int num; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i] = str[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[i]] --] = i; for (int k = 1; k <= n; k <<= 1) { num = 0; for (int i = n - k + 1; i <= n; ++ i) y[++ num] = i; for (int i = 1; i <= n; ++ i) if (sa[i] > k) y[++ num] = sa[i] - k; for (int i = 1; i <= m; ++ i) c[i] = 0; for (int i = 1; i <= n; ++ i) c[x[i]] ++; for (int i = 1; i <= m; ++ i) c[i] += c[i - 1]; for (int i = n; i; -- i) sa[c[x[y[i]]] --] = y[i]; swap(x, y); x[sa[1]] = 1, num = 1; for (int i = 2; i <= n; ++ i) if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) x[sa[i]] = num; else x[sa[i]] = ++ num; if (num == n) return; m = num; } }
voidget_height(int *str, int *sa, int *height, int n) { for (int i = 1; i <= n; ++ i) rk[sa[i]] = i; for (int i = 1; i <= n; ++ i) { if (rk[i] == 1) continue; int j = sa[rk[i] - 1], k = max(0, height[rk[i - 1]] - 1); while (i + k <= n && j + k <= n && str[i + k] == str[j + k]) k ++; height[rk[i]] = k; } }
intdiscrete(int x) { if (dis.count(x) == 0) dis[x] = ++ m; return dis[x]; }
intmain() { cin >> n; for (int i = 1; i <= n; ++ i) scanf("%d", &str[i]); for (int i = 1; i <= n; ++ i) str[i] = discrete(str[i]); reverse(str + 1, str + n + 1); get_sa(str, sa, n), get_height(str, sa, height, n); for (int i = 1; i <= n; ++ i) res += n - sa[i] + 1 - height[i]; for (int i = 1; i <= n; ++ i) u[i] = i - 1, d[i] = i + 1; u[n + 1] = n, d[0] = 1; for (int i = 1; i <= n; ++ i) { ans[i] = res; int x = rk[i], y = d[x]; res -= n - sa[x] + 1 - height[x] + n - sa[y] + 1 - height[y]; height[y] = min(height[y], height[x]); res += n - sa[y] + 1 - height[y]; u[d[x]] = u[x], d[u[x]] = d[x]; } for (int i = n; i; -- i) printf("%lld\n", ans[i]); return0; }