QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#180400 | #7086. Inner Product | brz# | RE | 531ms | 65512kb | C++20 | 6.5kb | 2023-09-15 19:35:53 | 2023-09-15 19:35:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++)
#define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--)
#define debug(x) cerr<<#x<<"="<<x<<endl
#define all(x) (x).begin(),(x).end()
#define cle(x) memset(x,0,sizeof(x))
#define ll long long
#define ull unsigned ll
#define db double
#define lb long db
#define pb push_back
#define mp make_pair
#define fi first
#define se second
inline ll read()
{
ll x=0; char ch=getchar(); bool f=0;
for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
return f?-x:x;
}
const ll mod = 1e9 + 7;
inline ll Pow(ll x,ll y) {
ll ans = 1;
for(; y; y >>= 1, x = x * x % mod)
if(y & 1)
ans = ans * x % mod;
return ans;
}
inline ll Add(ll x,ll y) {
x += y;
return (x >= mod) ? x - mod : x;
}
inline ll Dec(ll x,ll y) {
x -= y;
return (x < 0) ? x + mod : x;
}
inline ll Mul(ll x,ll y) {
return 1ll * x * y % mod;
}
const int N=200010;
const int M=18;
const int inf=2e9;
const ll Inf=4e18;
int lg[N];
int top[2],s[2][N];
ll w[2][N];
ll ans;
namespace T2{
ll ans;
int ne[N],head[N],ver[N],tot=1,dep[N];
int dfn[N],tim,low[N];
ll val[N],dis[N];
int cnt,fir[N],f[M][N];
inline void add(ll z,int y,int x)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
ver[++tot]=x; val[tot]=z; ne[tot]=head[y]; head[y]=tot;
}
inline void _add(int x,int y,ll z)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
}
void dfs(int u,int pre)
{
dep[u]=dep[pre]+1; f[0][++cnt]=u; fir[u]=cnt;
dfn[u]=++tim;
for(int i=head[u],v;i;i=ne[i])
if((v=ver[i])!=pre)
{
dis[v]=dis[u]+val[i];
dfs(v,u);
f[0][++cnt]=u;
}
low[u]=tim;
}
inline int cmp(int x,int y){return dep[x]<dep[y]?x:y;}
void pre(int n)
{
dfs(1,0);
fo(j,1,M-1)
fo(i,1,cnt+1-(1<<j))
f[j][i]=cmp(f[j-1][i],f[j-1][i+(1<<(j-1))]);
fo(i,1,n) head[i] = 0;
tot=1;
}
inline int lca(int x,int y)
{
x=fir[x]; y=fir[y];
if(x>y) swap(x,y);
int k=lg[y-x+1];
return cmp(f[k][x],f[k][y-(1<<k)+1]);
}
int b[N],m,opt[N],st[N];
ll va[N];
bool vis[N];
int siz[N][2];
ll d1[N][2], d2[N][2], d3[N][2];
void dfs(int u)
{
fo(j,0,1)
d1[u][j] = d2[u][j] = d3[u][j] = siz[u][j] = 0;
if (vis[u]) {
d1[u][opt[u]] = va[u] % mod;
d2[u][opt[u]] = dis[u] % mod;
d3[u][opt[u]] = 1ll * (va[u] % mod) * (dis[u] % mod) % mod;
siz[u][opt[u]] = 1;
}
for(int i=head[u],v;i;i=ne[i])
{
v=ver[i];
dfs(v);
fo(j,0,1)
ans = (ans +
d3[u][j] * siz[v][1-j] + d2[u][j] * d1[v][1-j] +
d3[v][j] * siz[u][1-j] + d2[v][j] * d1[u][1-j] +
-2ll * (dis[u] % mod) * ((d1[u][j] * siz[v][1-j] + d1[v][j] * siz[u][1-j]) % mod) % mod
) % mod;
fo(j,0,1)
siz[u][j] += siz[v][j],
d1[u][j] = Add(d1[u][j], d1[v][j]),
d2[u][j] = Add(d2[u][j], d2[v][j]),
d3[u][j] = Add(d3[u][j], d3[v][j]);
}
}
inline void build_tree()
{
m=0;
fo(j,0,1) {
fo(i,1,top[j]) b[++m]=s[j][i];
}
sort(b+1,b+m+1,[&](const int &x,const int &y){return dfn[x]<dfn[y];});
fo(i,1,m) vis[b[i]]=1;
fo(j,0,1) fo(i,1,top[j]) opt[s[j][i]]=j;
fo(j,0,1) fo(i,1,top[j]) va[s[j][i]]=w[j][i];
fo(i,1,m-1) b[++m]=lca(b[i],b[i+1]);
sort(b+1,b+m+1,[&](const int &x,const int &y){return dfn[x]<dfn[y];});
m=unique(b+1,b+m+1)-b-1;
fo(i,1,m) head[b[i]]=0; tot=1;
int top=0;
fo(i,1,m)
{
for(;top&&low[st[top]]<dfn[b[i]];--top);
if(top) _add(st[top],b[i],dis[b[i]]-dis[st[top]]);
st[++top]=b[i];
}
}
void init()
{
fo(i,1,m) va[b[i]]=opt[b[i]]=vis[b[i]]=head[b[i]]=0;
m=0; tot=1;
}
inline ll solve() {
ans = 0;
build_tree();
dfs(b[1]);
init();
return ans;
}
inline void clear(int n) {
cnt = tim = 0;
fo(i,1,n) head[i] = dfn[i] = low[i] = 0;
tot = 1;
}
}
int n;
namespace T1{
int pre_n;
int ne[N],head[N],ver[N],tot=1;
ll val[N];
bool vis[N];
inline void add(int x,int y,ll z)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
ver[++tot]=x; val[tot]=z; ne[tot]=head[y]; head[y]=tot;
}
vector<int> son[N]; ll value[N];
void dfs(int u,int pre)
{
for(int i=head[u],v;i;i=ne[i])
if((v=ver[i])!=pre)
{
dfs(v,u);
son[u].pb(v); value[v]=val[i];
}
}
void rebuild()
{
dfs(1,0);
fo(i,1,n) head[i]=0;
tot=1;
int N=n,w[2],d=0;
for(int i=1;i<=N;i++)
if(son[i].size()<=2) for(auto v:son[i]) add(i,v,value[v]);
else
{
w[0]=++N; w[1]=++N;
add(i,w[0],0); add(i,w[1],0);
for(auto v:son[i])
{
d=1-d; son[w[d]].pb(v);
}
}
n=N;
}
int siz[N],minn,edge;
ll dis[N];
void getroot(int u,int pre,int s)
{
siz[u]=1;
for(int i=head[u],v,tmp;i;i=ne[i])
if((v=ver[i])!=pre&&!vis[i>>1])
{
getroot(v,u,s);
siz[u]+=siz[v];
tmp=max(siz[v],s-siz[v]);
if(tmp<minn) minn=tmp,edge=i;
}
}
int now;
void getdis(int u,int pre)
{
if(u<=pre_n)
{
++top[now];
s[now][top[now]]=u;
w[now][top[now]]=dis[u];
}
for(int i=head[u],v;i;i=ne[i])
if(!vis[i>>1]&&(v=ver[i])!=pre)
{
dis[v]=dis[u]+val[i];
getdis(v,u);
}
}
void divide(int u,int s)
{
minn=inf; getroot(u,0,s);
if(minn>=inf) return;
int j=edge,s1=siz[ver[j]],s2=s-s1;
vis[j>>1]=1; top[0]=top[1]=0;
dis[ver[j]]=dis[ver[j^1]]=0;
dis[ver[j]]=val[j];
now=0; getdis(ver[j],0);
now=1; getdis(ver[j^1],0);
ans = (ans + T2::solve() + mod) % mod;
fo(j,0,1) fo(i,1,top[j]) w[j][i]=0;
divide(ver[j],s1); divide(ver[j^1],s2);
}
inline void work()
{
pre_n=n;
rebuild();
divide(1,n);
}
void clear() {
fo(i,1,n)
son[i].clear(), head[i] = 0;
fo(i,1,tot) vis[i>>1] = 0;
tot = 1;
}
}
void Solve() {
n=read();
fo(i,2,n<<1) lg[i]=lg[i>>1]+1;
int x,y; ll z;
fo(i,2,n) x=read(),y=read(),z=read(),T1::add(x,y,z);
fo(i,2,n) T2::add(read(),read(),read());
T2::pre(n); T1::work();
ans = (ans * 2 % mod + mod) % mod;
printf("%lld\n", ans);
T1::clear(); T2::clear(n);
}
int main() {
int T = read();
for (;T--;) {
ans = 0;
Solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 34448kb
input:
1 2 1 2 3 1 2 4
output:
24
result:
ok "24"
Test #2:
score: 0
Accepted
time: 33ms
memory: 36436kb
input:
18265 4 1 4 770451592 2 4 277067438 3 2 307076074 3 4 164467604 1 4 588797903 2 1 537346425 1 7 7 4 382835486 2 7 473042763 5 2 39929242 6 2 585229407 3 5 807830350 1 2 898554961 3 7 541921359 2 7 796516696 6 3 239857604 4 3 145052803 5 2 418549973 1 2 781381628 9 1 7 158088424 3 7 61764661 5 1 6866...
output:
503735836 0 737031969 848125471 711293684 0 895782141 103217087 487510904 949916343 843006376 0 425493202 152334353 877947602 763745879 450239245 217817539 109645158 446460359 238672116 375432426 741349146 635549172 50862625 12408855 114291847 857285339 439093366 315692084 396448753 49849870 1573942...
result:
ok 18265 tokens
Test #3:
score: 0
Accepted
time: 24ms
memory: 36440kb
input:
20000 5 3 5 794569311 4 3 307232641 1 3 289365434 2 1 700354988 1 4 383486460 5 4 722980657 3 5 165974518 2 5 702624506 5 2 4 574855297 3 2 581682486 5 2 697270602 1 3 669374551 4 5 709221897 2 5 358274522 1 4 30427751 3 2 294939001 5 5 4 105548111 1 5 505805412 3 5 675481326 2 3 272458928 3 4 20999...
output:
379923377 157771981 78792794 902650786 796068382 39934522 97828891 258460903 67741975 518986572 617358464 279757449 767863317 926170805 173376086 984164529 165980252 224459326 660216372 789550257 99090272 531801969 414525072 219030980 746933970 526469799 940055873 91691862 244785418 161502150 503545...
result:
ok 20000 tokens
Test #4:
score: 0
Accepted
time: 45ms
memory: 38456kb
input:
10000 10 4 7 43083983 3 4 159041968 6 4 331188098 2 4 627336744 10 2 632761483 9 6 270546623 5 10 980814948 1 5 300664654 8 9 791255656 6 4 835024257 9 4 577069471 10 9 906928171 1 10 859746830 7 9 954123960 3 10 794062640 2 1 325348972 8 3 339912418 5 2 392441182 10 5 8 29444203 10 8 763643316 2 8 ...
output:
569354001 55005953 248735967 540828069 58009541 517535699 925224307 209441158 682745260 469328732 531875116 881399131 647460471 81289235 84426983 966213948 13447101 207182591 514685114 929211254 704974619 456323114 768648113 252858697 813002820 847535266 669856401 546888766 133874278 464098904 25606...
result:
ok 10000 tokens
Test #5:
score: 0
Accepted
time: 112ms
memory: 38460kb
input:
1000 100 8 74 961577785 26 8 237741238 40 8 511962199 42 26 999748991 23 26 627262064 60 23 280805048 67 23 819557610 78 23 692051345 28 78 534857058 90 26 891847856 87 28 998173732 52 28 831771820 65 23 534099123 9 87 505371795 36 23 175227975 91 90 510554725 96 52 50945098 66 91 221944214 27 96 92...
output:
584066633 677217116 955670098 529000416 277904616 644392522 369058376 475191551 320321511 118825785 93233102 323983099 676317075 701750899 148576291 792936435 333889326 645018411 832409570 737895533 913114503 936652508 148198323 764275727 8729610 33178157 227497812 594598346 811998361 665831946 3152...
result:
ok 1000 tokens
Test #6:
score: 0
Accepted
time: 133ms
memory: 42564kb
input:
1000 100 27 10 879037025 38 27 548087321 98 27 114632787 91 98 112509705 2 91 134492317 51 38 523568160 14 51 904624080 87 51 50059234 25 10 370184066 54 25 153338391 59 91 151782091 4 25 751095882 100 14 542358729 15 59 668146761 97 25 155284764 49 59 906331304 75 59 671149674 46 54 282068286 16 59...
output:
6690871 316472479 667214716 513423179 944707808 17289072 391459604 357224282 238454559 86914895 273055166 239467319 804354838 48885904 935046233 267441197 741187056 714037743 521342601 171472431 182907980 595156564 346510689 902754549 53563418 119458847 293776352 601535423 148585973 187598634 129473...
result:
ok 1000 tokens
Test #7:
score: 0
Accepted
time: 113ms
memory: 38540kb
input:
1000 100 59 62 651272073 71 59 153400700 34 62 157494863 20 62 225270418 63 59 791465674 89 34 206522760 61 63 134914742 67 62 703034420 39 63 910543779 41 89 4763519 37 20 600357747 97 67 230228456 9 67 405394142 36 20 830921727 8 63 545406961 95 89 156883691 78 95 586321546 2 37 342192357 47 41 26...
output:
883048260 102342458 49308680 848411195 792733193 905084392 882396170 804636051 39369700 952847470 313676393 36638150 11729796 868006488 631271233 110512488 384522049 76610450 499049566 18507198 988418583 966338954 906878807 50799269 73591583 155845715 943847726 871019780 188292887 153722219 42927652...
result:
ok 1000 tokens
Test #8:
score: 0
Accepted
time: 130ms
memory: 44612kb
input:
1000 100 50 64 568731313 14 50 23555295 75 50 760165451 57 14 483255323 81 64 593663224 98 64 744253168 27 14 219981212 43 50 501233797 58 98 891094979 79 64 121029863 99 79 899190298 8 27 149552517 18 79 708621044 32 27 553505205 42 79 230496454 54 18 407436078 3 42 206526123 6 43 697283724 2 6 231...
output:
841643396 608528581 846859108 781309030 813525165 549972467 660605601 326451168 975606230 16838323 960295422 283697074 561503654 761019101 707294850 503454840 712670957 904050531 174590097 796323935 763666817 179202998 140844465 460703168 813427899 935188971 113331927 939334194 20241289 22708550 576...
result:
ok 1000 tokens
Test #9:
score: 0
Accepted
time: 114ms
memory: 40532kb
input:
1000 100 11 19 486190553 14 11 333901379 68 11 657803336 81 68 890983333 42 68 100893477 80 19 281983576 56 80 305047682 12 68 564274390 38 80 726421987 77 68 677487694 79 80 52798657 33 56 773909283 40 33 571656457 25 40 716280171 8 80 770361754 92 25 508245361 62 77 826730699 22 92 757407796 4 8 4...
output:
63958840 840245701 792108221 36685385 999333493 309976575 949317316 515366846 866324907 631512551 342982958 164655907 588923605 970417539 398733305 766771424 825955104 975270706 722588084 7903488 446554216 338251244 317305112 362327987 483144819 593895033 109782751 355074927 340601246 546523899 2030...
result:
ok 1000 tokens
Test #10:
score: 0
Accepted
time: 188ms
memory: 42888kb
input:
100 1000 412 273 334195969 986 273 819701401 710 412 453711147 283 710 185643354 693 710 82511187 501 412 115391593 101 501 764309472 83 501 555206920 755 83 102795917 493 101 132890893 431 83 499461355 58 501 311712098 26 710 978854620 353 501 224027268 900 493 261335322 14 900 272550149 869 755 53...
output:
604615368 978082128 191941581 778932994 163817665 516021405 682651616 812546780 220630451 8858085 809392379 819580084 657140422 143698820 192590316 860909553 645897146 635583598 979582965 235991633 907386130 43933065 720443814 842206851 205530420 41851817 426879808 431706528 834774063 575642548 6539...
result:
ok 100 tokens
Test #11:
score: 0
Accepted
time: 202ms
memory: 44596kb
input:
100 1000 885 890 482201886 79 890 884024981 23 890 773044358 346 79 972200715 351 890 48432777 455 885 568840212 462 23 749413906 937 79 403020062 400 23 901306670 899 23 758548727 22 937 23463823 873 346 626250836 919 346 694467340 363 400 433131597 694 22 327142943 606 937 203427039 111 400 110433...
output:
679885887 942710759 708959559 809150891 978098124 816055997 929650116 17841804 554767747 980567334 746174751 394204321 649909542 820964293 998509907 908077311 17881227 692783298 220149079 368446483 486350137 16456124 304910353 544553774 340442429 217130388 950941294 824730981 981981225 475194596 539...
result:
ok 100 tokens
Test #12:
score: 0
Accepted
time: 202ms
memory: 42624kb
input:
100 1000 408 255 64740368 238 255 557257983 794 255 269482955 439 238 146037332 666 439 738683515 927 238 816958237 150 794 528503868 152 150 538660479 985 238 429734364 784 794 660040595 213 408 554697565 787 408 191242725 975 784 652185207 980 794 49007094 737 213 914900755 946 794 865865659 266 2...
output:
205022462 705720994 704979841 16070695 485906603 155609152 255004152 835650727 263569656 894760697 359967590 648954582 881602102 652042510 608362701 222001564 997842132 928810120 831657749 901780053 448866456 512147288 862285104 603485820 913798593 322382502 129221634 996392024 583634321 69213902 66...
result:
ok 100 tokens
Test #13:
score: 0
Accepted
time: 202ms
memory: 42700kb
input:
100 1000 476 659 67522093 579 659 916548859 596 579 443591974 254 476 932594693 913 579 144796594 855 913 975439560 429 596 953799790 228 596 91506324 725 659 933277820 517 659 285698428 823 596 928956929 270 579 505781463 786 228 662765222 635 823 553078719 669 254 275675672 60 635 796742549 739 57...
output:
548794585 773670400 556094471 101331604 673098988 334108796 833088355 993342957 587003404 674433917 640306486 606967103 484656058 572696808 591251243 443432991 994500028 335528737 568596932 413410253 805249805 33423124 263289623 200378545 709721884 91441586 704084591 300074351 179055921 156324304 17...
result:
ok 100 tokens
Test #14:
score: 0
Accepted
time: 308ms
memory: 47824kb
input:
10 10000 1044 2894 216102857 8040 1044 82592044 1377 2894 664093125 2844 2894 565616229 5338 1044 620675473 924 2894 906851770 8408 1044 80415921 9497 2894 627134041 8805 1044 181778077 8017 8408 372257444 7829 5338 633452355 7249 8408 767785651 7905 924 491696003 5843 8805 342303951 4986 8017 69188...
output:
384581015 610949077 224676126 361012936 780034839 301226958 834065372 809274597 218142002 662687036
result:
ok 10 tokens
Test #15:
score: 0
Accepted
time: 297ms
memory: 45788kb
input:
10 10000 4522 9928 275420975 3083 4522 31105408 1816 9928 860695294 6139 4522 432709561 196 9928 199970245 7437 3083 43610432 2747 196 502348946 8322 196 337152246 4374 2747 471414074 3478 196 108734146 2839 196 975143634 7378 6139 20875275 2772 8322 258852918 5572 6139 120345152 234 196 597484165 4...
output:
280455135 310140209 421372881 514401969 467934037 839131701 965760771 173873934 966287143 45237586
result:
ok 10 tokens
Test #16:
score: 0
Accepted
time: 298ms
memory: 47692kb
input:
10 10000 2476 3364 462283699 6998 3364 881898623 6762 6998 380561444 4853 6762 696904532 636 6762 723380285 2889 636 609007391 9193 6998 134573599 9980 636 153219675 1434 9193 194871357 7639 4853 334198036 8877 4853 151772640 2271 8877 364341100 6196 6762 402327970 3436 2889 779518490 4119 8877 9076...
output:
179473617 433465741 516920151 388146697 423691864 344009254 869456101 715139257 566600446 491200998
result:
ok 10 tokens
Test #17:
score: 0
Accepted
time: 314ms
memory: 45680kb
input:
10 10000 2619 2191 473064350 4249 2619 534471644 9393 4249 16077839 2595 4249 187998586 1380 2595 611817469 3296 2595 999902379 8247 2619 822997913 9272 4249 922711288 4737 1380 528441702 40 9393 208852864 3301 9272 836634811 280 9393 87981940 4115 8247 706521156 6334 2595 591360665 4189 2191 825226...
output:
571844100 47805019 761282754 265190312 467593533 52887292 733989742 776052354 421401084 603021227
result:
ok 10 tokens
Test #18:
score: 0
Accepted
time: 529ms
memory: 65512kb
input:
1 100000 9776 74965 843558653 34388 74965 130985194 40973 9776 611071956 61652 40973 62401333 64001 40973 685839342 38934 61652 163899259 24367 64001 776561767 1591 38934 966941186 19448 1591 57936720 8622 19448 618453827 98366 8622 915963949 21918 98366 42937455 88695 21918 729266806 18729 21918 83...
output:
723259448
result:
ok "723259448"
Test #19:
score: 0
Accepted
time: 531ms
memory: 65456kb
input:
1 100000 39951 37442 501826650 18621 39951 583699795 56855 18621 988553925 49915 56855 379041010 13280 56855 712313298 30378 13280 293504600 36849 13280 555533817 13519 36849 846254702 29614 13519 456596407 14258 29614 121575011 36257 29614 372815886 52262 36257 872696363 46602 52262 913319171 25333...
output:
19770813
result:
ok "19770813"
Test #20:
score: -100
Runtime Error
input:
1 100000 51447 86095 105492133 13086 86095 196763883 83257 86095 368354749 61255 51447 553490521 46464 83257 14547215 9680 83257 411470100 87807 9680 675191190 51284 9680 818381840 90275 61255 564973023 46214 46464 182810900 21701 87807 881136778 13946 87807 801791803 80837 21701 738099919 47913 902...