1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n; int a[300005]; int sa[300005],rk[300005],ht[300005],st[300005][20],id[300005],cnt[300005],old[300005]; int rk2[300005],st2[300005][20]; bool same(int x,int y,int w){return x+w<n&&y+w<n&&old[x]==old[y]&&old[x+w]==old[y+w];} int lcp(int l,int r){ if(rk[l]>rk[r])swap(l,r); l=rk[l]+1;r=rk[r]; int s=__lg(r-l+1); return min(st[l][s],st[r-(1<<s)+1][s]); } int lcs(int l,int r){ if(rk2[n-l-1]>rk2[n-r-1])swap(l,r); l=rk2[n-l-1]+1;r=rk2[n-r-1]; int s=__lg(r-l+1); return min(st2[l][s],st2[r-(1<<s)+1][s]); } void buildsa(){ int m=n; REP(i,0,m)cnt[i]=0; REP(i,0,n)++cnt[rk[i]=a[i]]; REP(i,1,n)cnt[i]+=cnt[i-1]; REP(i,0,n)sa[--cnt[rk[i]]]=i; for(int w=1;w<n;w<<=1){ int cur=0; for(int i=n-1;i>=n-w;--i)id[cur++]=i; REP(i,0,n)if(sa[i]>=w)id[cur++]=sa[i]-w; REP(i,0,m)cnt[i]=0; REP(i,0,n)old[i]=rk[i],++cnt[rk[id[i]]]; REP(i,1,m)cnt[i]+=cnt[i-1]; for(int i=n-1;i>=0;--i)sa[--cnt[rk[id[i]]]]=id[i]; cur=1;rk[sa[0]]=0; REP(i,1,n)if(same(sa[i],sa[i-1],w))rk[sa[i]]=cur-1;else rk[sa[i]]=cur++; if(n==cur)break;else m=cur; } int k=0; REP(i,0,n)if(rk[i]){ int t=sa[rk[i]-1];k=max(0ll,k-1); while(t+k<n&&i+k<n&&a[i+k]==a[t+k])++k; ht[rk[i]]=k; } REP(i,0,n)st[i][0]=ht[i]; REP(j,0,__lg(n)){ REP(i,0,n-(1<<(j+1))+1)st[i][j+1]=min(st[i][j],st[i+(1<<j)][j]); } } struct ds{ int fa[300005][20]; int findfa(int x,int y){ return fa[x][y]==x? x:fa[x][y]=findfa(fa[x][y],y); } void init(){ REP(i,0,__lg(n)+1){ REP(j,0,n+1)fa[j][i]=j; } } int merge(int x,int y,int l){ int p1=findfa(x,l),p2=findfa(y,l); if(p1==p2)return 0; fa[p1][l]=p2; if(l==0)return 1; return merge(x,y,l-1)+merge(x+(1<<(l-1)),y+(1<<(l-1)),l-1); } int query(int x,int y,int l){ int t=__lg(l); return merge(x,y,t)+merge(x+l-(1<<t),y+l-(1<<t),t); } }s; int w[300005]; bool cmp(int x,int y){return w[x]<w[y];} void Main() { cin>>n; REP(i,0,n)cin>>a[i],--a[i]; REP(i,0,n/2)cin>>w[i+1]; s.init(); reverse(a,a+n);buildsa(); REP(i,0,n)rk2[i]=rk[i]; REP(j,0,__lg(n)+1){ REP(i,0,n)st2[i][j]=st[i][j]; } reverse(a,a+n);buildsa(); vector<int>v; REP(i,0,n/2)v.pb(i+1); sort(all(v),cmp); int ans=0; for(auto i:v){ for(int j=i;j<n;j+=i){ if(lcp(j,j-i)+lcs(j,j-i)-1>=i){ int x=j-lcs(j,j-i)+1,y=lcp(x,x-i); ans+=s.query(x-i,x,y)*w[i]; } } } cout<<ans<<endl; } void TC() { int tc=1; cin>>tc; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("endless.in","r",stdin); freopen("endless.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2935
- Time
- 4000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By