萌新刚学 SA 后缀数组,求调 0 分,hack 全过
查看原帖
萌新刚学 SA 后缀数组,求调 0 分,hack 全过
852144
Loser_Syx楼主2024/7/19 19:12
#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, cnt[N], tmp[N], lsh[N], a[N], st[25][N], lg[N];
int s[N];
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;
	}
} void initST() {
	for (int i = 0; i < n; ++i) st[0][i] = h[i];
	for (int i = 1; i <= lg[n]; ++i) {
		for (int j = 0; j + (1 << i) + 1 < n; ++i) st[i][j] = min(st[i-1][j], st[i-1][j + (1 << (i-1))]);
	}
} int query(int l, int r) {
	int t = lg[r - l + 1];
	return min(st[t][l], st[t][r - (1 << t) + 1]);
}
signed main() {
	read(n);
	for (int i = 0; i < n; ++i) a[n - i - 1] = lsh[i + 1] = read();
	sort(lsh + 1, lsh + 1 + n); int len = unique(lsh + 1, lsh + 1 + n) - lsh - 1;
	for (int i = 0; i < n; ++i) s[i] = lower_bound(lsh + 1, lsh + 1 + len, a[i]) - lsh;
	get_sa();
	for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
	initST(); set<int> st; long long ans = 0;
	for (int i = n-1; ~i; --i) {
		st.insert(rnk[i]);
		auto qwq = st.find(rnk[i]);
		int oh = 0;
		if (qwq != st.begin()) {
			int s = *(--qwq);
			++qwq;
			smax(oh, query(s + 1, rnk[i]));
		} ++qwq;
		if (qwq != st.end()) {
			int s = *qwq;
			smax(oh, query(rnk[i] + 1, s));
		} ans += n - i - oh;
		write(ans);
	}
	return 0;
}
2024/7/19 19:12
加载中...