QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#287512 | #7206. Triple | zjy0001 | WA | 3ms | 11860kb | C++14 | 5.5kb | 2023-12-20 18:14:06 | 2023-12-20 18:14:06 |
Judging History
answer
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define LL long long
#define uint unsigned
#define LLL __int128_t
#define ldb long double
#define uLL unsigned long long
using namespace std;
char wS[262144],rdc[262144],*rS,*rT,*wT=wS;
#define gc() (rS==rT?rT=rdc+fread(rS=rdc,1,262144,stdin),rS==rT?EOF:*rS++:*rS++)
#define flush() fwrite(wS,1,wT-wS,stdout),wT=wS
inline void pc(const char&x){*wT++=x;if(__builtin_expect(wS+262144==wT,0))flush();}
template<class T=int>inline T read(){
T x(0);char c;bool f(1);while(!isdigit(c=gc())&&c!=EOF)if(c==45)f=!f;
if(c==EOF)return -1;
if(f)do x=x*10+(c&15);while(isdigit(c=gc()));
else do x=x*10-(c&15);while(isdigit(c=gc()));
return x;
}
template<>inline char read(){char c;while((c=gc())<33);return c;}
template<>inline string read(){char c;string s;while((c=gc())<33);do s+=c;while((c=gc())>32);return s;}
inline int read(char*const s){char c,*t=s;while((c=gc())<33);do *t++=c;while((c=gc())>32);return *t=0,t-s;}
template<class T>inline void write(T x){
int wtop=0;char wbuf[numeric_limits<T>::digits10+1];
if(x>=0)do wbuf[wtop++]=(x%10)|48;while(x/=10);
else{pc(45);do wbuf[wtop++]=-(x%10)|48;while(x/=10);}
for(;wtop;pc(wbuf[--wtop]));
}
template<>inline void write(char x){pc(x);}
template<>inline void write(char*s){for(;*s;pc(*s++));}
template<>inline void write(const char*s){for(;*s;pc(*s++));}
template<>inline void write(string s){for(auto c:s)pc(c);}
template<class T,class...YP>inline void write(const T&x,const YP&...y){write(x),write(y...);}
const int N=1e5+5;
int n,lim,rt,Cn,Bn;
bool vs[N];
LL ans,R[N];
vector<int>vec[N];
int Gm,Gh[N],Gv[N*2],Gnxt[N*2];
int sz[N],mx[N],len[N],son[N],cnt[N],L[N],U[N],B[N];
struct DS{
int L,R,all,a[N*2];
inline void clear(){L=N,R=N-1,all=0;}
inline void shift(){a[--L]=0;}
inline int qry(const int&p){return a[p+L];}
inline void add(const int&p,const int&q){
while(R<L+p)a[++R]=0;
a[L+p]+=q,all+=q;
}
inline int qrysuf(const int&p){
if(L+p>R)return 0;
int res=all;
for(int i=0;i<p;++i)res-=a[L+i];
return res;
}
}ds;
struct fenwicktree{
int c[N];
inline void add(int x,const int&y){
for(++x;x<=n;x+=x&-x)c[x]+=y;
}
inline int qry(int x){
if(x<0)return 0;
int res=0;
for(++x;x;x-=x&-x)res+=c[x];
return res;
}
}T;
inline void add(const int&u,const int&v){
Gv[++Gm]=v,Gnxt[Gm]=Gh[u],Gh[u]=Gm;
Gv[++Gm]=u,Gnxt[Gm]=Gh[v],Gh[v]=Gm;
}
inline void dfs0(const int&u,const int&fa){
sz[u]=1,mx[u]=0;
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(v!=fa&&!vs[v])dfs0(v,u),sz[u]+=sz[v],mx[u]=max(mx[u],sz[v]);
if((mx[u]=max(mx[u],lim-sz[u]))<mx[rt])rt=u;
}
inline void dfs1(const int&u,const int&fa,const int&bl,const int&d){
if(d>len[bl])len[bl]=d,vec[bl].emplace_back(0);
++vec[bl][d],sz[u]=1,son[u]=0;
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])if(v!=fa&&!vs[v])
dfs1(v,u,bl,d+1),sz[u]+=sz[v],sz[v]>sz[son[u]]?son[u]=v:0;
}
inline void ins(const int&u,const int&fa,const int&d){
++cnt[d],Cn=max(d,Cn);
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(v!=fa&&!vs[v])ins(v,u,d+1);
}
inline void dfs2(const int&u,const int&fa,const int&d){
ds.clear();
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(v!=fa&&!vs[v]&&v!=son[u])dfs2(v,u,d+1);
if(son[u])dfs2(son[u],u,d+1),ds.shift();
ans+=2ll*ds.all*L[0],ds.add(0,1);
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(v!=fa&&!vs[v]&&v!=son[u]){
Cn=0,ins(v,u,1);
int lst=0,suf=ds.qrysuf(Cn+1);
for(int j=Cn;~j;--j){
int nw=ds.qry(j);
ans+=L[max(0,j-d+1)]*(2ll*lst*nw+2ll*cnt[j]*suf);
suf+=nw,ds.add(j,cnt[j]);
ans+=T.qry(j-d-1)*(2ll*lst*nw+2ll*cnt[j]*suf);
lst+=cnt[j],cnt[j]=0;
}
}
}
inline void solve(const int&u){
Bn=0,L[0]=1;
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])if(!vs[v]){
vec[v]={len[v]=0},dfs1(v,u,v,1),B[++Bn]=v;
for(int d=len[v];d;--d)
ans+=vec[v][d]*(L[d+1]*(L[d+1]-1ll)-R[d+1]);
for(int d=len[v],nL=0;~d;--d)
L[d]+=(nL+=vec[v][d]),R[d]+=(nL*(nL-1ll)),T.add(d,vec[v][d]);
}
ans+=(L[1]*(L[1]-1ll)-R[1]);
for(int i=1,v;v=B[i],i<=Bn;++i){
for(int d=len[v],nL=0;~d;--d)L[d]-=(nL+=vec[v][d]),T.add(d,-vec[v][d]);
dfs2(v,u,1);
for(int d=len[v],nL=0;~d;--d)L[d]+=(nL+=vec[v][d]),T.add(d,vec[v][d]);
}
for(int i=1,v;v=B[i],i<=Bn;++i){
for(int d=len[v],nL=0;~d;--d)L[d]-=(nL+=vec[v][d]),T.add(d,-vec[v][d]);
for(int d=len[v];d;--d)ans+=(LL)vec[v][d]*L[d+1]*U[d+1];
for(int d=len[v],nL=0;d;--d)U[d]+=(nL+=vec[v][d]);
}
for(int i=1,v;v=B[i],i<=Bn;++i){
for(int d=len[v],nL=0;d;--d)
U[d]-=(nL+=vec[v][d]),R[d]-=(nL*(nL-1ll));
for(int d=len[v];d;--d)
ans+=vec[v][d]*(U[d+1]*(U[d+1]-1ll)-R[d+1]);
}
vs[u]=1;
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(!vs[v]&&sz[v]>2)lim=sz[v],dfs0(v,u),solve(rt);
}
inline void MAIN(){
ans=0,Gm=0,fill(Gh+1,Gh+n+1,0),fill(vs+1,vs+n+1,0);
for(int i=1;i<n;++i)add(read(),read());
mx[rt=0]=1e9,lim=n,dfs0(1,0),solve(rt);
write(n*(n-1ll)*(n-2)-ans,'\n');
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
#define LOCAL
#ifndef LOCAL
freopen("triple.in","r",stdin);
freopen("triple.out","w",stdout);
#endif
while(~(n=read()))MAIN();
return flush(),0;
}
/*
min(AX,BX) > CR+XR
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11824kb
input:
3 1 2 2 3 4 1 2 2 3 2 4
output:
4 18
result:
ok 2 tokens
Test #2:
score: 0
Accepted
time: 2ms
memory: 11860kb
input:
5 4 1 3 4 2 5 3 5 5 5 4 3 4 4 1 1 2 5 4 5 4 1 3 2 4 3 5 1 3 4 3 2 4 5 1 5 4 1 4 2 5 4 5 3 5 1 4 5 2 3 5 3 1 5 5 3 3 4 2 1 5 2 5 5 1 5 4 1 2 2 3 5 5 2 2 4 1 3 1 2 5 1 3 2 4 5 4 3 5 5 4 1 2 4 5 4 3 1 5 1 5 3 1 1 2 4 2 5 2 5 4 2 3 4 4 1 5 5 1 4 2 2 1 1 3 5 4 2 3 4 1 4 5 2 5 3 1 3 2 3 5 4 1 5 3 4 2 5 4 ...
output:
40 44 44 40 44 40 40 40 44 40 44 44 44 44 44 44 40 40 44 40 40 44 44 44 40 44 40 40 44 44 40 44 44 40 44 48 44 40 40 40 40 40 48 40 44 44 40 44 48 40 40 44 40 44 44 44 40 40 40 40 44 44 44 44 40 40 44 48 40 44 40 40 44 44 44 40 44 44 44 44 44 48 44 40 40 40 40 48 40 44 44 40 44 44 44 40 40 44 44 40 ...
result:
ok 200 tokens
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 11860kb
input:
6 3 5 2 5 6 1 6 3 4 6 6 1 2 4 1 6 2 2 3 5 6 6 3 4 6 3 1 2 1 5 4 1 6 1 2 6 3 1 3 1 4 4 5 6 2 1 6 2 5 2 3 5 4 2 6 4 6 2 3 6 1 3 5 1 5 6 5 4 6 2 6 1 1 5 3 4 6 2 1 6 4 1 4 2 5 3 6 6 1 6 1 3 5 3 6 2 2 4 6 5 3 5 4 1 3 6 4 2 3 6 4 5 1 3 2 4 3 2 6 4 6 1 4 3 6 6 1 1 2 1 5 6 6 4 1 4 4 5 3 4 3 2 6 6 3 2 3 4 6 ...
output:
86 86 86 86 94 80 80 80 80 86 86 94 94 86 86 94 92 86 87 80 86 92 86 80 94 80 94 94 80 86 92 92 86 86 86 87 94 86 86 86 86 87 80 94 86 80 94 80 80 86 86 80 86 87 94 80 94 86 94 80 94 80 94 86 86 86 80 86 86 94 86 86 86 80 86 86 86 86 86 86 86 92 86 86 87 80 80 80 86 86 80 86 86 87 80 86 86 87 86 80 ...
result:
wrong answer 19th words differ - expected: '86', found: '87'