This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
using namespace std;
int n, C[101000], ord1[101000], ord2[101000], SA[101000], Rank[101000], LCP[101000], cc;
char p[101000];
struct Tree{
int l, r, s;
}IT[3001000];
int Root[101000];
struct AA{
int a, b;
}w[101000];
void Suffix_Array(){
int i, M = max(n, 200), LL = 1;
for(i=0;i<n;i++){
w[i].a = p[i],w[i].b = p[i+1];
}
while(1){
for(i=0;i<=M;i++)C[i]=0;
for(i=0;i<n;i++)C[w[i].b]++;
for(i=1;i<=M;i++)C[i]+=C[i-1];
for(i=n-1;i>=0;i--)ord1[--C[w[i].b]] = i;
for(i=0;i<=M;i++)C[i]=0;
for(i=0;i<n;i++)C[w[i].a]++;
for(i=1;i<=M;i++)C[i]+=C[i-1];
for(i=n-1;i>=0;i--)ord2[--C[w[ord1[i]].a]] = ord1[i];
int cnt = 0;
for(i=0;i<n;i++){
if(i==0||w[ord2[i]].a!=w[ord2[i-1]].a||w[ord2[i]].b!=w[ord2[i-1]].b)cnt++;
Rank[ord2[i]] = cnt;
}
if(cnt == n)break;
LL<<=1;
for(i=0;i<n;i++){
w[i].a = Rank[i];
if(i+LL >= n)w[i].b = 0;
else w[i].b = Rank[i+LL];
}
}
for(i=0;i<n;i++)SA[Rank[i]] = i;
int t = 0, x, y;
for(i=0;i<n;i++){
x = Rank[i];
if(t)t--;
if(x==n)continue;
y = SA[x+1];
while(p[i+t]==p[y+t])t++;
LCP[x] = t;
}
}
int top, R[101000];
struct TP{
int l, b;
}st[101000];
void Ins(int nd, int rt, int b, int e, int x){
IT[nd] = IT[rt];
if(b==e){
IT[nd].s++;
return;
}
int m = (b+e)>>1;
if(x <= m){
IT[nd].l = ++cc;
Ins(IT[nd].l, IT[rt].l, b, m, x);
}
else{
IT[nd].r = ++cc;
Ins(IT[nd].r, IT[rt].r, m+1, e, x);
}
IT[nd].s = IT[IT[nd].l].s + IT[IT[nd].r].s;
}
int Get(int nd, int b, int e, int s, int l){
if(s>l)return 0;
if(b==s&&e==l)return IT[nd].s;
int m = (b+e)>>1, r = 0;
if(s<=m)r += Get(IT[nd].l, b, m, s, min(m,l));
if(l>m)r += Get(IT[nd].r, m+1, e, max(s,m+1), l);
return r;
}
int main(){
int i, j;
scanf("%s",p);
for(i=0;p[i];i++);
n = i;
for(i=0;i<n/2;i++){
swap(p[i],p[n-i-1]);
}
Suffix_Array();
for(i=1;i<=n;i++){
Root[i] = ++cc;
Ins(Root[i],Root[i-1], 0, n-1, SA[i]);
}
for(i=1;i<=n;i++){
int b = -1;
while(top && st[top].l >= LCP[i-1]){
b = st[top].b;
top--;
}
if(b!=-1){
st[++top] = {LCP[i-1],b};
}
int r = 0;
for(j=top;st[j].l;j--){
r += (st[j].l-st[j-1].l) * (Get(Root[i-1], 0, n-1, SA[i]+1, n-1) - Get(Root[st[j].b-1], 0, n-1, SA[i]+1, n-1));
}
R[n-SA[i]] += r*2;
st[++top] = {n - SA[i], i};
}
top = 0;
for(i=n;i>=1;i--){
int b = -1;
while(top && st[top].l >= LCP[i]){
b = st[top].b;
top--;
}
if(b!=-1){
st[++top] = {LCP[i],b};
}
int r = 0;
for(j=top;st[j].l;j--){
r += (st[j].l-st[j-1].l) * (Get(Root[st[j].b], 0, n-1, SA[i]+1, n-1) - Get(Root[i], 0, n-1, SA[i]+1, n-1));
}
R[n-SA[i]] += r*2;
st[++top] = {n - SA[i], i};
}
long long sum = 0;
for(i=1;i<=n;i++){
sum = sum + i + R[i];
printf("%lld\n",sum);
}
}
Compilation message (stderr)
NO.cpp: In function 'int main()':
NO.cpp:100:36: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {LCP[i-1],b};
^
NO.cpp:100:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {LCP[i-1],b};
^
NO.cpp:107:34: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {n - SA[i], i};
^
NO.cpp:107:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {n - SA[i], i};
^
NO.cpp:117:34: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {LCP[i],b};
^
NO.cpp:117:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {LCP[i],b};
^
NO.cpp:124:34: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {n - SA[i], i};
^
NO.cpp:124:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
st[++top] = {n - SA[i], i};
^
NO.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",p);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |