可爱萌新求助简单 SA 20pts 喵,码风清新
查看原帖
可爱萌新求助简单 SA 20pts 喵,码风清新
852144
Loser_Syx楼主2024/7/19 09:00
#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <random>
#include <ctime>
#include <map>
#include <set>
using namespace std;
// #define int long long
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
	template <typename T = int>
	inline T read() {
		T s = 0, w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		return s * w;
	}
	template <typename T>
	inline void read(T &s) {
		s = 0;
		int w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		s = s * w;
	}
	template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
		read(x), read(arp...);
	}
	template <typename T>
	inline void write(T x, char ch = '\n') {
		if (x < 0) x = -x, putchar('-');
		static char stk[25];
		int top = 0;
		do {
			stk[top++] = x % 10 + '0', x /= 10;
		} while (x);
		while (top) putchar(stk[--top]);
		putchar(ch);
		return;
	}
	template <typename T>
	inline void smax(T &x, T y) {
		if (x < y) x = y;
	}
	template <typename T>
	inline void smin(T &x, T y) {
		if (x > y) x = y;
	}
	void quit() {
		exit(0);
	}
} using namespace FastIO;
const int N = 1e6 + 19;
int sa[N], rnk[N], h[N], n, k, ans = 0, f[N], siz[N], mx[N], mn[N], cnt[N], tmp[N], a[N], hoks[N], syx[N];
char s[N]; vector<int> hikari[N]; long long maxtimes = -1e18;
void get_sa() {
	for (int i = 0; i < n; ++i) sa[i] = i;
	sort(sa, sa + n, [&](int x, int y) {
		return s[x] < s[y];
	} );
	for (int i = 0; i < n; ++i) {
		if (i && s[sa[i]] == s[sa[i - 1]]) rnk[sa[i]] = rnk[sa[i - 1]];
		else rnk[sa[i]] = i;
	}
	for (int w = 1; w < n; w <<= 1) {
		for (int i = 0; i < w; ++i) tmp[i] = i + n - w;
		for (int i = 0, j = w; j < n; ++i) if (sa[i] >= w) tmp[j++] = sa[i] - w;
		for (int i = 0; i < n; ++i) cnt[i] = 0;
		for (int i = 0; i < n; ++i) ++cnt[rnk[i]];
		for (int i = 1; i < n; ++i) cnt[i] += cnt[i - 1];
		for (int i = n - 1; ~i; --i) sa[--cnt[rnk[tmp[i]]]] = tmp[i];
		bool ok = 1;
		for (int i = 0; i < n; ++i) {
			if (i && rnk[sa[i - 1]] == rnk[sa[i]] && (sa[i - 1] + w < n ? rnk[sa[i - 1] + w] : -1) == (sa[i] + w < n ? rnk[sa[i] + w] : -1)) {
				ok = 0;
				tmp[sa[i]] = tmp[sa[i-1]];
			} else tmp[sa[i]] = i;
		}
		for (int i = 0; i < n; ++i) rnk[i] = tmp[i];
		if (ok) break;
	}
	for (int i = 0, k = 0; i < n; ++i) {
		if (!rnk[i]) continue;
		if (k) --k;
		while (s[i + k] == s[sa[rnk[i] - 1] + k]) ++k;
		h[rnk[i]] = k;
	}
} int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void go(int x, int y) {
	int fx = find(x), fy = find(y);
	if (siz[fx] < siz[fy]) swap(fx, fy);
//	cout << mn[fx] << ' ' << mn[fy] << ' ' << mx[fx] << ' ' << mx[fy] << '\n';
	ans += siz[fx] * siz[fy]; smax(maxtimes, max(1LL * mx[fx] * mx[fy], 1LL * mn[fx] * mn[fy]));
	f[fx] = fy; siz[fy] += siz[fx];
	smin(mn[fy], mn[fx]); smax(mx[fy], mx[fx]);
}
signed main() {
	n = read();
	scanf("%s", s);
	get_sa();
	for (int i = 0; i < n; ++i) read(a[i]);
	for (int i = 0; i < n; ++i) f[i] = i, siz[i] = 1, mn[i] = mx[i] = a[sa[i]];
	for (int i = 1; i < n; ++i) hikari[h[i]].eb(i);
	for (int i = n-1; ~i; --i) {
		for (int j : hikari[i]) go(j, j-1);
		if (ans) hoks[i] = ans, syx[i] = maxtimes;
	} for (int i = 0; i < n; ++i) {
		write(hoks[i], ' '); write(syx[i]);
	}
	return 0;
}
2024/7/19 09:00
加载中...