QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#655894#9406. Triangle11d10xyWA 5ms38700kbC++144.2kb2024-10-19 10:13:592024-10-19 10:13:59

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 10:13:59]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:38700kb
  • [2024-10-19 10:13:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
namespace sa{
char str[300010];
int n,sa[300010],rk[300010],cnt[300010],tmp[300010],h[300010],mi[300010][21];
void cntsort(int*s,int*key,int*t,int V){
   for(int i=0;i<=V;i++)cnt[i]=0;
   for(int i=1;i<=n;i++)cnt[key[i]]++;
   for(int i=1;i<=V;i++)cnt[i]+=cnt[i-1];
   for(int i=n;i>=1;i--)t[cnt[key[s[i]]]--]=s[i];
}
void build(){
   for(int i=1;i<=n;i++)tmp[i]=i,rk[i]=str[i];
   cntsort(tmp,rk,sa,'z');
   for(int len=1,p=0;p<n;len<<=1){
      p=0;
      for(int i=n;i>n-len;i--)tmp[++p]=i;
      for(int i=1;i<=n;i++)if(sa[i]>len)tmp[++p]=sa[i]-len;
      cntsort(tmp,rk,sa,n);
      for(int i=1;i<=n;i++)tmp[i]=rk[i];
      p=0;
      for(int i=1;i<=n;i++){
         if(tmp[sa[i]]==tmp[sa[i-1]]&&tmp[sa[i]+len]==tmp[sa[i-1]+len]){
            rk[sa[i]]=p;
         }else rk[sa[i]]=++p;
      }
   }
   for(int i=1;i<=n;i++){
      h[i]=max(h[i-1]-1,0);
      for(;str[i+h[i]]==str[sa[rk[i]-1]+h[i]];h[i]++);
   }
   for(int i=1;i<=n;i++)mi[i][0]=h[sa[i]];
   for(int i=1;i<=20;i++)for(int k=1;k+(1<<i)-1<=n;k++){
      mi[i][k]=min(mi[i-1][k],mi[i-1][k+(1<<i-1)]);
   }
}
int lcp(int i,int j){
   if(i==j)return n-i+1;
   i=rk[i],j=rk[j];
   if(i>j)swap(i,j);
   i++;int g=__lg(j-i+1);
   return min(mi[g][i],mi[g][j-(1<<g)+1]);
}
void clear(){n=0;}
}
int n,pos[300010];
struct N_{string s;int c;}a[300010];
char buf[300010];
namespace calcer{
int cnt[300010];
inline void add(int p,int v){
   for(;p<=n;p+=p&-p)cnt[p]+=v;
}
inline int ask(int p){
   int s=0;for(;p;p-=p&-p)s+=cnt[p];
   return s;
}
struct P_{int y,w;};
struct R_{int l,r;i64 w;};
vector<P_>pnt[300010];
vector<R_>qs[300010];
i64 work(){
   i64 res=0;
   for(int i=1;i<=n;i++){
      for(P_ y:pnt[i])add(y.y,y.w);
      for(R_ x:qs[i])res+=x.w*(ask(x.r)-ask(x.l-1));
   }
   return res;
}
void clear(){
   for(int i=1;i<=n;i++){
      cnt[i]=0,pnt[i].clear(),qs[i].clear();
   }
}
}
inline int lcp(int i,int pi,int j,int pj){
   return min({sa::lcp(pos[i]+pi,pos[j]+pj),(int)a[i].s.length()-pi,(int)a[j].s.length()-pj});
}
int perm[300010],invp[300010],L[300010];
void init(){
   scanf("%d",&n);
   vector<string>S(n);
   for(auto&s:S)scanf("%s",buf),s.assign(buf);
   stable_sort(begin(S),end(S));
   n=0;
   for(auto&s:S){
      if(!n||s!=a[n].s)a[++n]={s,1};
      else a[n].c++;
   }
   sa::clear();
   for(int i=1;i<=n;i++){
      pos[i]=sa::n+1;
      for(char c:a[i].s)sa::str[++sa::n]=c;
   }
   sa::build();
   for(int i=1;i<=n;i++){
      if(i>1&&a[i-1].s.length()<a[i].s.length()&&a[i-1].s==a[i].s.substr(0,a[i-1].s.length()))L[i]=L[i-1];
      else L[i]=i;
   }
}
void solve(){
   init();
   for(int i=1;i<=n;i++)perm[i]=i;
   auto cmper=[](int x,int y){
      int i=a[x].s.length(),p=lcp(y,0,y,i);
      if(i+p<a[y].s.length()){
         return a[y].s[p]-a[y].s[i+p];
      }
      i=a[y].s.length()-a[x].s.length(),p=lcp(x,0,y,i);
      if(p==a[x].s.length())return 0;
      return a[y].s[i+p]-a[x].s[p];
   };
   stable_sort(perm+1,perm+n+1,[&](int x,int y){
      if(!((L[x]<=y&&y<=x)||(L[y]<=x&&x<=y)))return x<y;
      if(x<y)return cmper(x,y)<0;
      else return cmper(y,x)>0;
   });
   for(int i=1;i<=n;i++)invp[perm[i]]=i;
   calcer::clear();
   for(int i=1;i<=n;i++){
      calcer::pnt[i].push_back({invp[i],a[i].c});
   }
   i64 ans=0;
   for(int i=1;i<=n;i++)for(int j=L[i];j<i;j++){
      int l=1,r=i;
      while(l<r){
         int mid=l+r>>1,p=lcp(mid,0,i,a[j].s.length());
         bool ge;
         if(p==a[mid].s.length())ge=false;
         else if(a[j].s.length()+p==a[i].s.length())ge=true;
         else ge=a[mid].s[p]>a[i].s[a[j].s.length()+p];
         if(ge)r=mid;
         else l=mid+1;
      }
      if(l<i){
         calcer::qs[l-1].push_back({1,invp[j]-1,-a[i].c*1ll*a[j].c});
         calcer::qs[i-1].push_back({1,invp[j]-1,a[i].c*1ll*a[j].c});
      }
      if(l<=j)ans+=a[i].c*1ll*a[j].c*(a[j].c-1)/2;
   }
   for(int i=1,s=0;i<=n;i++){
      ans+=a[i].c*1ll*(a[i].c-1)*(a[i].c-2)/6+a[i].c*1ll*(a[i].c-1)/2*s;
      s+=a[i].c;
   }
   ans+=calcer::work();
   printf("%lld\n",ans);
}
int main(){
   int T;for(scanf("%d",&T);T--;)solve();
   return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 38700kb

input:

3
6
cbaa
cb
cb
cbaa
ba
ba
3
sdcpc
sd
cpc
1
ccpc

output:

16
0
0

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 38664kb

input:

14
1
lfpbavjsm
2
pdtlkfwn
mbd
3
dvqksg
dvqksg
uhbhpyhj
4
ombwb
ombwb
ombwb
ombwb
5
oztclz
oztclz
oztclz
oztclz
kul
6
vco
vco
vco
dtktsqtinm
vco
vco
7
tb
tb
kowbsu
ygkfphcij
tb
uvei
tb
8
vxxtxssht
abnsxbf
bydaae
bydaae
udalyvmcef
bydaae
bydaae
bydaae
9
aaze
zvyonw
qjfv
mmhkef
qjfv
qjfv
qjfv
mmhkef
qj...

output:

0
0
0
4
10
20
10
20
41
14
63
74
18
11042

result:

wrong answer 14th lines differ - expected: '11081', found: '11042'