QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#287543 | #7206. Triple | zjy0001 | WA | 12ms | 6288kb | C++14 | 5.3kb | 2023-12-20 18:39:15 | 2023-12-20 18:39:16 |
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){
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]);
if(j>d)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,T.add(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],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]),R[d]-=(nL*(nL-1ll)),T.add(d,-vec[v][d]),
ans+=vec[v][d]*(L[d+1]*(L[d+1]-1ll)-R[d+1]);
dfs2(v,u,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]);
}
T.add(0,-1);
for(int i=1,v;v=B[i],i<=Bn;++i)
for(int d=len[v],nL=0;~d;--d)
L[d]=0,R[d]=0,T.add(d,-vec[v][d]);
vs[u]=1;
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(!vs[v]&&sz[v]>2)lim=sz[v],rt=0,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;
}
/*
AR,BR > CR
AX>CX and BX+XR > CX
AX,BX > RX+CR
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5876kb
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: 0ms
memory: 5816kb
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: 0
Accepted
time: 0ms
memory: 5868kb
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 86 80 86 92 86 80 94 80 94 94 80 86 92 92 86 86 86 86 94 86 86 86 86 86 80 94 86 80 94 80 80 86 86 80 86 86 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 86 80 80 80 86 86 80 86 86 86 80 86 86 86 86 80 ...
result:
ok 2000 tokens
Test #4:
score: 0
Accepted
time: 2ms
memory: 6024kb
input:
7 5 2 4 7 3 2 7 1 2 6 4 3 7 3 2 5 6 1 5 3 5 6 4 3 7 7 3 2 5 6 7 2 6 2 1 6 6 4 7 2 5 2 7 1 7 6 5 3 2 4 1 7 4 6 6 3 6 7 5 3 1 4 1 2 7 6 1 5 2 6 5 4 2 6 3 6 7 7 7 4 6 4 3 4 3 5 2 5 3 1 7 7 4 3 6 1 6 3 2 7 6 6 5 7 1 7 6 1 5 6 2 7 7 4 3 5 7 6 2 4 5 3 2 4 2 5 7 1 4 7 4 3 7 2 3 5 3 1 2 1 6 5 7 6 7 1 6 7 2 ...
output:
148 156 168 148 148 160 156 160 148 156 148 160 148 148 140 168 148 148 160 160 156 150 160 148 148 140 148 148 148 148 148 148 150 148 148 148 150 148 156 140 148 148 148 156 160 148 148 148 148 140 150 148 148 148 156 148 156 156 140 156 148 140 148 148 160 148 160 148 148 156 156 148 148 168 160 ...
result:
ok 5000 tokens
Test #5:
score: 0
Accepted
time: 7ms
memory: 6220kb
input:
8 2 8 7 1 3 2 7 8 8 4 5 7 6 4 8 1 4 8 7 5 6 1 5 4 8 7 2 4 3 8 5 2 1 5 6 1 7 6 3 8 4 7 3 6 8 1 4 6 3 8 3 8 4 8 7 3 5 2 6 8 1 7 4 3 8 3 3 5 2 1 2 8 6 3 8 4 2 5 4 7 2 8 6 6 4 6 3 1 7 8 4 2 1 7 3 7 1 2 1 5 8 5 7 6 8 1 2 5 7 3 8 8 7 5 1 8 6 5 4 8 8 7 4 2 4 3 2 7 3 5 4 6 1 2 8 8 4 1 3 3 6 7 8 1 7 5 8 3 2 ...
output:
248 234 238 244 250 244 248 244 244 244 252 244 238 276 224 244 234 244 238 260 238 234 234 234 250 252 278 224 234 244 244 248 250 254 268 234 234 234 234 244 244 234 244 224 260 234 250 244 244 244 238 260 244 238 234 260 244 260 268 244 248 234 248 234 260 234 234 244 250 244 250 248 234 234 244 ...
result:
ok 10000 tokens
Test #6:
score: 0
Accepted
time: 12ms
memory: 6112kb
input:
9 8 3 6 7 6 5 2 5 5 4 8 9 3 1 8 6 9 9 6 3 9 1 5 4 3 8 1 1 6 2 6 7 6 9 1 7 9 3 7 2 6 4 4 8 3 5 4 1 6 3 9 1 9 7 9 3 1 5 2 7 2 3 8 6 9 8 4 9 4 5 5 1 4 7 6 2 3 4 2 4 7 8 5 9 9 7 9 6 5 3 2 8 9 9 5 9 4 5 1 7 3 9 4 3 3 2 7 2 7 1 4 6 7 9 6 5 8 7 9 2 5 2 1 4 2 4 8 2 3 2 7 9 5 6 3 9 8 3 9 6 7 2 9 1 7 1 4 5 7 ...
output:
372 380 360 348 384 380 368 394 366 372 336 354 372 348 368 372 380 380 348 380 348 380 366 360 372 360 348 360 348 404 360 392 372 348 348 348 368 348 380 372 360 380 368 360 360 348 360 368 348 368 372 360 348 354 348 380 360 386 372 366 366 354 354 360 372 360 368 394 348 386 380 348 368 348 372 ...
result:
ok 10000 tokens
Test #7:
score: 0
Accepted
time: 10ms
memory: 6172kb
input:
10 8 2 8 4 2 5 7 6 10 3 6 4 9 3 3 6 1 10 10 9 2 5 10 3 1 1 6 10 4 2 10 1 4 1 7 9 8 10 6 1 3 8 7 8 9 8 6 2 5 7 2 10 1 3 4 6 10 10 1 8 5 7 4 3 4 10 4 2 4 10 6 7 8 5 9 10 1 7 8 4 7 8 9 5 7 2 7 5 8 6 5 10 7 3 10 10 1 8 3 3 5 10 9 8 6 7 2 6 1 4 5 2 6 10 6 10 10 5 1 2 2 8 5 9 9 1 7 1 3 5 7 4 10 10 5 1 7 1...
output:
508 532 508 532 576 502 516 522 520 516 522 508 524 508 522 536 532 524 532 522 522 554 502 516 508 522 516 522 516 508 540 522 530 494 508 494 548 532 546 502 546 524 508 534 522 518 508 566 532 494 562 520 508 532 522 522 534 518 524 532 552 520 532 552 538 516 502 518 502 534 534 534 508 508 494 ...
result:
ok 10000 tokens
Test #8:
score: 0
Accepted
time: 10ms
memory: 6288kb
input:
10 2 3 1 3 7 3 8 10 1 8 10 5 4 8 4 6 9 7 10 10 8 8 2 2 7 3 2 10 1 2 9 8 4 5 6 5 7 10 8 4 4 1 10 9 6 9 8 2 5 2 5 9 4 7 5 3 10 7 4 4 5 1 8 3 4 4 2 1 7 6 3 3 9 10 1 10 3 4 10 9 1 9 2 9 6 8 5 1 6 5 7 1 4 8 10 10 7 8 9 5 3 1 2 1 9 1 6 2 5 7 6 4 10 10 2 8 3 8 8 4 6 10 2 5 6 9 7 8 8 10 1 6 10 7 1 10 1 8 5 ...
output:
516 532 522 546 508 502 562 532 522 546 508 570 540 502 556 532 520 532 508 494 502 516 524 522 508 532 520 546 556 532 508 576 494 532 538 518 534 508 534 552 516 508 494 540 554 532 494 508 548 522 518 508 516 516 518 532 508 522 534 524 520 522 508 508 518 582 532 546 508 502 508 516 494 530 532 ...
result:
ok 10000 tokens
Test #9:
score: 0
Accepted
time: 6ms
memory: 6256kb
input:
11 8 10 2 3 5 7 10 3 4 11 6 10 7 10 9 6 1 11 10 11 11 8 5 7 9 10 11 11 6 3 9 4 3 8 7 4 2 1 4 6 8 11 6 2 3 2 2 9 5 8 1 3 7 9 4 7 4 5 10 3 3 11 11 6 2 11 4 8 7 3 1 10 7 1 10 9 4 2 5 6 4 3 11 11 2 11 3 1 1 7 10 4 1 10 9 3 11 4 5 7 4 6 8 4 11 2 3 3 9 4 3 7 1 1 5 8 11 4 10 4 5 6 11 4 6 11 1 7 5 2 5 9 6 2...
output:
770 692 720 676 730 732 734 720 692 728 722 692 728 746 720 720 704 686 740 702 708 732 686 736 724 764 748 730 702 708 676 692 740 732 812 720 702 702 720 720 728 744 772 714 702 686 692 764 704 720 728 692 708 740 718 784 702 772 728 740 756 752 738 686 728 708 776 708 736 762 728 740 686 708 748 ...
result:
ok 5000 tokens
Test #10:
score: 0
Accepted
time: 10ms
memory: 6156kb
input:
12 8 12 12 6 5 3 12 10 12 1 10 2 12 4 11 6 5 8 3 7 5 9 12 11 7 6 1 12 10 4 11 8 6 6 3 5 2 8 2 6 11 2 9 12 11 12 8 2 4 12 6 2 7 3 12 10 5 9 7 9 11 9 2 4 12 1 12 3 12 7 9 6 4 3 8 9 10 3 7 11 12 9 6 5 12 1 5 8 5 10 2 12 2 5 7 3 1 5 9 4 5 6 1 7 10 6 12 2 12 9 8 10 11 2 12 12 10 12 7 12 3 7 11 11 8 2 10 ...
output:
998 998 966 928 936 966 960 966 978 998 970 934 956 966 916 948 928 948 948 972 954 952 966 948 948 968 1010 932 930 954 958 954 958 948 916 982 958 968 934 966 934 1006 930 966 980 946 962 948 1008 974 928 972 1026 964 954 984 980 1000 934 916 966 972 934 972 936 934 966 984 966 958 934 1000 966 97...
result:
ok 5000 tokens
Test #11:
score: 0
Accepted
time: 9ms
memory: 6152kb
input:
13 6 12 10 7 12 8 7 4 7 2 5 2 5 3 8 7 12 11 1 11 4 9 13 4 13 9 6 13 5 8 13 11 3 7 1 1 12 11 5 13 7 11 4 10 12 9 7 2 10 13 12 6 5 3 7 3 12 10 11 5 1 7 9 2 1 9 10 1 5 8 13 3 4 11 13 9 5 2 5 2 1 5 11 7 1 9 10 3 4 4 10 10 12 6 3 8 4 1 13 13 8 6 8 10 2 11 7 9 5 2 6 5 4 12 3 5 12 7 3 7 1 13 6 13 13 5 10 1...
output:
1260 1222 1218 1224 1238 1240 1248 1218 1368 1224 1268 1254 1244 1318 1240 1204 1340 1232 1254 1302 1238 1258 1184 1228 1238 1240 1238 1210 1276 1198 1280 1240 1312 1258 1260 1164 1204 1298 1208 1282 1224 1252 1246 1204 1262 1304 1314 1260 1288 1178 1242 1258 1288 1212 1240 1204 1288 1324 1204 1204 ...
result:
ok 5000 tokens
Test #12:
score: -100
Wrong Answer
time: 10ms
memory: 6204kb
input:
14 14 5 8 9 12 10 2 8 11 13 7 2 4 12 10 13 11 7 1 14 3 2 11 5 6 2 14 5 7 9 13 14 9 6 14 1 13 11 2 3 11 14 5 14 8 11 6 12 2 9 4 10 1 14 2 7 8 5 9 11 9 8 2 8 1 12 7 12 12 4 12 3 2 13 3 6 4 14 13 10 14 5 6 9 7 14 10 14 6 10 7 4 6 8 2 2 11 13 12 1 7 3 5 2 3 6 12 14 13 7 3 11 1 2 10 6 12 4 8 5 9 10 4 9 8...
output:
1570 1582 1602 1576 1494 1590 1600 1540 1584 1522 1632 1544 1586 1568 1616 1576 1554 1578 1632 1628 1554 1600 1652 1562 1640 1478 1556 1580 1582 1516 1550 1696 1556 1550 1532 1522 1668 1522 1550 1578 1616 1636 1598 1516 1610 1566 1554 1574 1602 1532 1636 1684 1594 1544 1522 1602 1562 1554 1522 1532 ...
result:
wrong answer 486th words differ - expected: '1572', found: '1544'