BZOJ3238 [Ahoi2013]差异

首先把后缀数组和height数组都搞出来。。。

然后用两个单调栈维护$[l, r]$表示对于一个点$x$,满足$height[x] \le height[l..x] \ \&\&\  height[x] < height[x..r]$的最小的$l$和最大的$r$

这样子就可以保证不会重复计算了

 /**************************************************************
Problem: 3238
User: rausen
Language: C++
Result: Accepted
Time:4496 ms
Memory:20336 kb
****************************************************************/ #include <cstdio>
#include <cstring> using namespace std;
typedef long long ll;
const int N = 5e5 + ; int a[N], len;
int sa[N], rank[N], height[N]; inline void Sort(int *a, int *b, int *c, int n, int m) {
static int i, sum[N];
for (i = ; i <= m; ++i) sum[i] = ;
for (i = ; i < n; ++i) ++sum[c[a[i]]];
for (i = ; i <= m; ++i) sum[i] += sum[i - ];
for (i = n - ; ~i; --i)
b[--sum[c[a[i]]]] = a[i];
} void make_sa(int *s) {
int i, j;
static int x[N], y[N];
for (i = ; i < len; ++i) x[i] = s[i], rank[i] = i;
Sort(rank, sa, x, len, );
rank[sa[]] = ;
for (i = ; i < len; ++i)
rank[sa[i]] = rank[sa[i - ]] + (x[sa[i]] != x[sa[i - ]]);
for (i = ; i <= len; i <<= ) {
for (j = ; j < len; ++j)
x[j] = rank[j], y[j] = j + i < len ? rank[j + i] : , sa[j] = j;
Sort(sa, rank, y, len, len), Sort(rank, sa, x, len, len);
rank[sa[]] = ;
for (j = ; j < len; ++j)
rank[sa[j]] = rank[sa[j - ]] + (x[sa[j]] != x[sa[j - ]] || y[sa[j]] != y[sa[j - ]]);
if (rank[sa[len - ]] == len) return;
}
} void make_height() {
int i, j;
for (i = j = ; i < len; ++i) {
if (j) --j;
if (rank[i] != )
while (a[i + j] == a[sa[rank[i] - ] + j]) ++j;
height[rank[i]] = j;
}
} ll work() {
int i;
ll res;
static int s[N], top, l[N], r[N];
for (res = , i = ; i <= len; ++i) res += 1ll * i * (len - );
for (s[top = ] = , i = ; i <= len; ++i) {
while (height[i] <= height[s[top]] && top) --top;
l[i] = s[top] + ;
s[++top] = i;
}
for (s[top = ] = len + , i = len; i; --i) {
while (height[i] < height[s[top]] && top) --top;
r[i] = s[top] - ;
s[++top] = i;
}
for (i = ; i <= len; ++i)
res -= 2ll * (i - l[i] + ) * (r[i] - i + ) * height[i];
return res;
} int main() {
int i;
char ch;
for (len = ; ;) {
ch = getchar();
if ('a' <= ch && ch <= 'z') a[len++] = ch - 'a' + ;
else break;
}
make_sa(a);
make_height();
printf("%lld\n", work());
return ;
}
上一篇:android入门问题--R文件丢失


下一篇:Android studio R文件丢失或错误解决方法