답안 #109866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109866 2019-05-08T08:15:02 Z sealnot123 회문 (APIO14_palindrome) C++14
65 / 100
1000 ms 87924 KB
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define ep emplace_back
#define sz(a) (a).size()
#define R real()
#define I imag()
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
typedef complex<LL> cd;
const int N=300007,lgn=20;
char str[2*N];
int suffix[2*N],lcp[2*N],Rank[2*N],tRank[2*N],stRank[2*N],tsuffix[2*N],fsuffix[2*N],nxt[2*N];
int LCA[lgn][2*N],now,mk[2*N],level[2*N],top;
PII line[2*N];
struct A{
    LL a,b,c;
};
A sta[2*N];
PII intersect(PII a, PII b){
    PII tmp = {a.y-b.y, b.x-a.x};
    if(tmp.y<0) tmp.x*=-1,tmp.y*=-1;
    return tmp;
}
int push(int idx, LL m, LL c){
    line[++now] = {m,c};
    LCA[0][now]=idx;
    level[now]=level[idx]+1;
    int i;
    for(i=1;i<=19;i++) LCA[i][now] = LCA[i-1][LCA[i-1][now]];
    int tmp=now;
    for(i=19;i>=0;i--){
        if(level[LCA[i][tmp]]>1){
            int p=LCA[i][tmp];
            int q=LCA[0][p];
            PII line1 = intersect(line[p],line[q]), line2 = intersect(line[p],line[now]);
            if(line2.x*line1.y<=line1.x*line2.y) tmp = LCA[i][tmp];
        }
    }
    LCA[0][now]=LCA[0][tmp];
    for(i=1;i<=19;i++) LCA[i][now]=LCA[i-1][LCA[i-1][now]];
    level[now]=level[LCA[0][now]]+1;
    return now;
}
bool cmp(int a, int b){
    return str[a]<str[b];
}
void suffix_array(int n){
    int i,j,k,h,cnt;
    memset(Rank, -1, sizeof Rank);
    for(i=1;i<=n;i++) suffix[i]=i;
    sort(suffix+1, suffix+1+n, cmp);
    for(i=1,cnt=0;i<=n;i++){
        if(str[suffix[i]]!=str[suffix[i-1]]) cnt++;
        Rank[suffix[i]]=cnt;
    }
    for(h=1;h<n;h<<=1){
        for(i=1;i<=n;i=j){
            for(j=i;j<=n&&Rank[suffix[i]]==Rank[suffix[j]];j++) stRank[suffix[j]]=i;
            nxt[i]=j; fsuffix[i]=i;
        }
        for(i=n;i>=n-h+1;i--){
            tsuffix[fsuffix[stRank[i]]]=i;
            fsuffix[stRank[i]]++;
        }
        for(i=1;i<=n;i=nxt[i]){
            for(j=i;j<nxt[i];j++){
                if(suffix[j]-h<=0) continue;
                int p = suffix[j]-h;
                tsuffix[fsuffix[stRank[p]]]=suffix[j]-h;
                fsuffix[stRank[p]]++;
            }
        }
        for(i=1;i<=n;i++) suffix[i]=tsuffix[i];
        for(i=1,cnt=1;i<=n;i=j){
            for(j=i;j<=n&&Rank[suffix[i]]==Rank[suffix[j]]&&Rank[suffix[i]+h]==Rank[suffix[j]+h];j++) tRank[suffix[j]]=cnt;
            cnt++;
        }
        for(i=1;i<=n;i++) Rank[i]=tRank[i];
    }
    int t=0;
    for(i=1;i<=n;i++){
        while(Rank[i]<n && i+t<=n && str[i+t]==str[suffix[Rank[i]+1]+t]) t++;
        lcp[Rank[i]] = t;
        if(t) t--;
    }
}
LL calc(int idx, LL V){
    int i;
    if(level[idx]==1) return line[idx].x*V+line[idx].y;
    PII tmp = intersect(line[idx], line[LCA[0][idx]]);
    if(V*tmp.y>=tmp.x) return line[idx].x*V+line[idx].y;
    for(i=18;i>=0;i--){
        if(level[idx]>1){
            int p = LCA[i][idx];
            int q = LCA[0][p];
            tmp = intersect(line[p],line[q]);
            if(V*tmp.y<tmp.x) idx = LCA[i][idx];
        }
    }
    idx = LCA[0][idx];
    return line[idx].x*V+line[idx].y;
}
int main(){
    int n,i,j,k,len;
    scanf("%s",str+1);
    len = strlen(str+1);
    str[len+1]='#';
    for(i=1;i<=len;i++){
        str[2*len+2-i]=str[i];
    }
    str[2*len+2]='&';
    len+=len+2;
    suffix_array(len);
    for(i=1;i<=len;i++){
        if(suffix[i]>len/2) mk[i]=2;
        else mk[i]=1;
    }
    LL ans=0;
    sta[top++]={-1<<30,0,0};
    int last1=0,last2=0;
    for(i=1;i<=len;i++){
        int fin;
        if(mk[i]==1) last1=i,fin=last2;
        else last2=i,fin=last1;
        int l=0,r=top-1,m;
        if(fin){
            while(l<r){
                m = (l+r)>>1;
                if(sta[m].b<fin) l=m+1;
                else r=m;
            }
            ans = max(ans, calc(sta[l].c,i));
        }
        while(top && sta[top-1].a>=lcp[i]) top--;
        sta[top]={lcp[i],i,push(sta[top-1].c, lcp[i], -(LL)lcp[i]*sta[top-1].b)},top++;
    }
    assert((ans&1)==0);
    printf("%lld",ans/2);
    return 0;
}

Compilation message

palindrome.cpp: In function 'void suffix_array(int)':
palindrome.cpp:51:13: warning: unused variable 'k' [-Wunused-variable]
     int i,j,k,h,cnt;
             ^
palindrome.cpp: In function 'int main()':
palindrome.cpp:107:9: warning: unused variable 'n' [-Wunused-variable]
     int n,i,j,k,len;
         ^
palindrome.cpp:107:13: warning: unused variable 'j' [-Wunused-variable]
     int n,i,j,k,len;
             ^
palindrome.cpp:107:15: warning: unused variable 'k' [-Wunused-variable]
     int n,i,j,k,len;
               ^
palindrome.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s",str+1);
     ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2944 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 3072 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 4 ms 2944 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 5 ms 2944 KB Output is correct
11 Correct 5 ms 2944 KB Output is correct
12 Correct 5 ms 2944 KB Output is correct
13 Correct 5 ms 2944 KB Output is correct
14 Correct 5 ms 2944 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 4 ms 2944 KB Output is correct
17 Correct 4 ms 2944 KB Output is correct
18 Correct 4 ms 2944 KB Output is correct
19 Correct 5 ms 2944 KB Output is correct
20 Correct 4 ms 2944 KB Output is correct
21 Correct 5 ms 2944 KB Output is correct
22 Correct 4 ms 2944 KB Output is correct
23 Correct 5 ms 3072 KB Output is correct
24 Correct 4 ms 2944 KB Output is correct
25 Correct 4 ms 2944 KB Output is correct
26 Correct 4 ms 2944 KB Output is correct
27 Correct 4 ms 2944 KB Output is correct
28 Correct 5 ms 2944 KB Output is correct
29 Correct 5 ms 2944 KB Output is correct
30 Correct 2 ms 2944 KB Output is correct
31 Correct 4 ms 2944 KB Output is correct
32 Incorrect 5 ms 2944 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3200 KB Output is correct
2 Correct 5 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 5 ms 3200 KB Output is correct
5 Correct 5 ms 3200 KB Output is correct
6 Correct 6 ms 3200 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 5 ms 3200 KB Output is correct
9 Correct 5 ms 3328 KB Output is correct
10 Correct 5 ms 3200 KB Output is correct
11 Correct 5 ms 3200 KB Output is correct
12 Correct 7 ms 3200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 5760 KB Output is correct
2 Correct 20 ms 5504 KB Output is correct
3 Correct 21 ms 5752 KB Output is correct
4 Correct 20 ms 5632 KB Output is correct
5 Correct 18 ms 5504 KB Output is correct
6 Correct 16 ms 5504 KB Output is correct
7 Correct 14 ms 5504 KB Output is correct
8 Correct 21 ms 5504 KB Output is correct
9 Correct 20 ms 5468 KB Output is correct
10 Correct 17 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 225 ms 31176 KB Output is correct
2 Correct 171 ms 30172 KB Output is correct
3 Correct 273 ms 31308 KB Output is correct
4 Correct 195 ms 30456 KB Output is correct
5 Correct 242 ms 28892 KB Output is correct
6 Correct 230 ms 29048 KB Output is correct
7 Correct 181 ms 29276 KB Output is correct
8 Correct 331 ms 29148 KB Output is correct
9 Correct 306 ms 29560 KB Output is correct
10 Correct 221 ms 28920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 995 ms 84412 KB Output is correct
2 Correct 451 ms 81500 KB Output is correct
3 Correct 990 ms 87924 KB Output is correct
4 Correct 729 ms 83488 KB Output is correct
5 Execution timed out 1079 ms 73180 KB Time limit exceeded
6 Halted 0 ms 0 KB -