QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#740032#6969. 幻想乡战略游戏I_be_wanna100 ✓175ms25896kbC++204.1kb2024-11-13 00:29:072024-11-13 00:29:09

Judging History

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

  • [2024-11-13 00:29:09]
  • 评测
  • 测评结果:100
  • 用时:175ms
  • 内存:25896kb
  • [2024-11-13 00:29:07]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long 
#define trvl(x,i,y) for(int i=ehd[x],y; y=to[i],i; i=lst[i]) 
#define ls (x<<1)
#define rs (x<<1|1)
using namespace std;

const int N=1e5+10;

int n,m;
int ehd[N],to[N<<1],len[N<<1],lst[N<<1];
int fa[N],siz[N],son[N],top[N],id[N],dfn[N];
ll S,T,dis[N];

void insert(int x,int y,int w) {
	static int cnt=0;
	to[++cnt]=y,len[cnt]=w,lst[cnt]=ehd[x],ehd[x]=cnt;
	to[++cnt]=x,len[cnt]=w,lst[cnt]=ehd[y],ehd[y]=cnt;
}
void dfs1(int x,int p) {
	fa[x]=p; siz[x]=1;
	trvl(x,i,y) if(y!=p) {
		dis[y]=dis[x]+len[i];
		dfs1(y,x);siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]) son[x]=y;
	}
}
void dfs2(int x,int t) {
	static int cnt=0;
	top[dfn[id[x]=++cnt]=x]=t;
	if(son[x]) dfs2(son[x],t);
	trvl(x,i,y) if(son[x]!=y&&fa[x]!=y) dfs2(y,y);
}

int tag[N<<2],mx[N<<2];
ll c[N<<2],s[N<<2];

void psd(int x) {
	if(!tag[x]) return;
	c[ls]+=s[ls]*tag[x],mx[ls]+=tag[x],tag[ls]+=tag[x];
	c[rs]+=s[rs]*tag[x],mx[rs]+=tag[x],tag[rs]+=tag[x];
	tag[x]=0;
}
void upd(int x) {
	mx[x]=max(mx[ls],mx[rs]);
	c[x]=c[ls]+c[rs];
}
void build(int x,int l,int r) {
	if(l==r) {
		s[x]=dis[dfn[l]]-dis[fa[dfn[l]]];
		return;
	}
	int mid=(l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	s[x]=s[ls]+s[rs];
}
int find(int x,int l,int r) {
	if(l==r) return dfn[l];
	int mid=(l+r)>>1; psd(x);
	if(mx[rs]*2>T) return find(rs,mid+1,r);
	return find(ls,l,mid);
}
void mdf(int x,int l,int r,int L,int R,ll w) {
	if(L<=l&&r<=R) {
		c[x]+=s[x]*w,mx[x]+=w,tag[x]+=w;
		return;
	}
	int mid=(l+r)>>1; psd(x);
	if(L<=mid) mdf(ls,l,mid,L,R,w);
	if(mid<R) mdf(rs,mid+1,r,L,R,w);
	upd(x);
}
ll qry(int x,int l,int r,int L,int R) {
	if(L<=l&&r<=R) return c[x];
	int mid=(l+r)>>1; ll c=0; psd(x);
	if(L<=mid) c+=qry(ls,l,mid,L,R);
	if(mid<R) c+=qry(rs,mid+1,r,L,R);
	return c;
}
void modify(int x,int w) {
	for(; x; x=fa[top[x]]) mdf(1,1,n,id[top[x]],id[x],w);
}
ll query(int x,ll w=0) {
	for(; x; x=fa[top[x]]) w+=qry(1,1,n,id[top[x]],id[x]);
	return w;
}

int main() {
	scanf("%d%d",&n,&m);
	for(int x,y,w,i=n; --i; ) {
		scanf("%d%d%d",&x,&y,&w);
		insert(x,y,w);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	for(int x,w; m--; ) {
		scanf("%d%d",&x,&w);
		modify(x,w);
		S+=dis[x]*w;
		T+=w;
		x=find(1,1,n);
		printf("%lld\n",S+T*dis[x]-2*query(x));
	}
	return 0;
}
/*#include <bits/stdc++.h>
#define ll long long 
#define trvl(x,i,y) for(int i=ehd[x],y; y=to[i],i; i=lst[i]) 
#define ls (x<<1)
#define rs (x<<1|1)
using namespace std;

const int N=1e5+10;

int n,m;
int ehd[N],to[N<<1],len[N<<1],lst[N<<1];
int fa[N],siz[N],son[N],top[N],id[N],dfn[N];
ll S,T,dis[N];

void insert(int x,int y,int w) {
	static int cnt=0;
	to[++cnt]=y,len[cnt]=w,lst[cnt]=ehd[x],ehd[x]=cnt;
	to[++cnt]=x,len[cnt]=w,lst[cnt]=ehd[y],ehd[y]=cnt;
}
void dfs1(int x,int p) {
	fa[x]=p; siz[x]=1;
	trvl(x,i,y) if(y!=p) {
		dis[y]=dis[x]+len[i];
		dfs1(y,x);siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]) son[x]=y;
	}
}
void dfs2(int x,int t) {
	static int cnt=0;
	top[dfn[id[x]=++cnt]=x]=t;
	if(son[x]) dfs2(son[x],t);
	trvl(x,i,y) if(son[x]!=y&&fa[x]!=y) dfs2(y,y);
}

int tag[N<<2],mx[N<<2];
ll c[N<<2],s[N<<2];

void psd(int x) {
	if(!tag[x]) return;
	c[ls]+=s[ls]*tag[x],mx[ls]+=tag[x],tag[ls]+=tag[x];
	c[rs]+=s[rs]*tag[x],mx[rs]+=tag[x],tag[rs]+=tag[x];
	tag[x]=0;
}
void upd(int x) {
	mx[x]=max(mx[ls],mx[rs]);
	c[x]=c[ls]+c[rs];
}
void build(int x,int l,int r) {
	if(l==r) {
		s[x]=dis[dfn[l]]-dis[fa[dfn[l]]];
		return;
	}
	int mid=(l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	s[x]=s[ls]+s[rs];
}
int find(int x,int l,int r) {
	if(l==r) return dfn[l];
	int mid=(l+r)>>1; psd(x);
	if(mx[rs]*2>T) return find(rs,mid+1,r);
	return find(ls,l,mid);
}
void mdf(int x,int l,int r,int L,int R,ll w) {
	if(L<=l&&r<=R) {
		c[x]+=s[x]*w,mx[x]+=w,tag[x]+=w;
		return;
	}
	int mid=(l+r)>>1; psd(x);
	if(L<=mid) mdf(ls,l,mid,L,R,w);
	if(mid<R) mdf(rs,mid+1,r,L,R,w);
	upd(x);
}
ll qry(int x,int l,int r,int L,int R) {
	if(L<=l&&r<=R) return c[x];
	int mid=(l+r)>>1; ll c=0; psd(x);
	if(L<=mid) c+=qry(ls,l,mid,L,R);
	if(mid<R) c+=qry(rs,mid+1,r,L,R);
	return c;
}
	}
	return 0;
}*/

详细


Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 3ms
memory: 10544kb

input:

5000 2000
1 2 701
2 3 811
3 4 548
4 5 703
5 6 32
6 7 435
7 8 368
8 9 978
9 10 68
10 11 550
11 12 667
12 13 270
13 14 486
14 15 217
15 16 486
16 17 326
17 18 418
18 19 361
19 20 344
20 21 116
21 22 373
22 23 38
23 24 563
24 25 591
25 26 800
26 27 94
27 28 210
28 29 548
29 30 278
30 31 169
31 32 1000
...

output:

0
374968
4576211
78541526
81407158
733359655
739404699
838213298
1065206845
1189431593
1475358020
1629991988
1743120427
1961812417
2287462078
2447622580
2543297822
2576127476
2808245908
2852868056
2857502227
2989584009
2994671229
3152458761
3235015261
3312146239
3665063055
3858145241
3938231115
3951...

result:

ok 2000 numbers

Test #2:

score: 5
Accepted
time: 3ms
memory: 10528kb

input:

5000 2000
1 2 576
2 3 745
3 4 672
4 5 812
5 6 770
6 7 261
7 8 191
8 9 534
9 10 209
10 11 576
11 12 184
12 13 212
13 14 996
14 15 466
15 16 360
16 17 127
17 18 180
18 19 804
19 20 74
20 21 504
21 22 912
22 23 253
23 24 113
24 25 418
25 26 723
26 27 596
27 28 92
28 29 845
29 30 215
30 31 96
31 32 224
...

output:

0
73613694
170140746
179078334
292181448
320671932
559805316
565000766
968748170
1046415165
1095236507
1408798424
1434559333
1447063538
1447183079
1608177909
1664454034
2059658290
2127853227
2146078361
2146565603
2300717947
2308762347
2319215702
2366918634
2433191214
2742980794
2824719850
2946124350...

result:

ok 2000 numbers

Test #3:

score: 5
Accepted
time: 0ms
memory: 12484kb

input:

5000 2000
1 2 149
2 3 122
3 4 778
4 5 764
5 6 514
6 7 905
7 8 289
8 9 151
9 10 976
10 11 315
11 12 429
12 13 517
13 14 488
14 15 679
15 16 354
16 17 597
17 18 583
18 19 378
19 20 128
20 21 990
21 22 123
22 23 893
23 24 859
24 25 140
25 26 188
26 27 142
27 28 620
28 29 735
29 30 662
30 31 261
31 32 9...

output:

0
6320496
33522536
428473548
437840388
675964071
676487597
710721786
872521332
873371948
1028917316
1075544960
1099018670
1100251310
1565073127
1637098351
1969054465
2085992986
2091531280
2141596156
2144573878
2246681338
2358786877
2567264272
2584783924
2584857276
2597019034
2616119444
2653361360
26...

result:

ok 2000 numbers

Test #4:

score: 5
Accepted
time: 74ms
memory: 25640kb

input:

100000 100000
1 2 31
2 3 400
3 4 488
4 5 524
5 6 665
6 7 63
7 8 381
8 9 375
9 10 936
10 11 157
11 12 657
12 13 346
13 14 408
14 15 288
15 16 138
16 17 785
17 18 15
18 19 109
19 20 319
20 21 659
21 22 773
22 23 703
23 24 995
24 25 974
25 26 159
26 27 163
27 28 468
28 29 915
29 30 245
30 31 634
31 32 ...

output:

0
6131047858
21480683875
26198866115
34394354675
40395512345
51341331455
67874564019
71618614997
73055435654
77381239949
90674258871
95851989327
96579508327
100944938087
101903623262
112877152676
123463836842
124669913356
126059995783
127567705681
134029284017
136589942445
139878512179
157347017780
...

result:

ok 100000 numbers

Test #5:

score: 5
Accepted
time: 81ms
memory: 25896kb

input:

100000 100000
1 2 460
2 3 195
3 4 548
4 5 573
5 6 835
6 7 147
7 8 107
8 9 619
9 10 881
10 11 982
11 12 185
12 13 152
13 14 98
14 15 153
15 16 14
16 17 925
17 18 651
18 19 226
19 20 175
20 21 968
21 22 25
22 23 695
23 24 597
24 25 353
25 26 803
26 27 958
27 28 598
28 29 535
29 30 330
30 31 743
31 32 ...

output:

0
5077861896
8054222536
18096689075
41026587445
43960280457
47822042679
51732411460
57034416906
59647968521
77907477870
89752477166
92790891606
93261400458
94775492180
107116707524
114049909926
134519030206
139405954054
142481321713
148593746231
155066572211
158513969639
165884458319
167177734797
17...

result:

ok 100000 numbers

Test #6:

score: 5
Accepted
time: 133ms
memory: 17916kb

input:

100000 100000
1 2 121
2 3 761
1 4 427
1 5 833
1 6 28
6 7 336
5 8 977
8 9 347
1 10 124
3 11 13
10 12 525
4 13 407
7 14 546
3 15 279
2 16 707
11 17 835
13 18 300
17 19 138
6 20 763
3 21 607
17 22 118
22 23 192
7 24 865
21 25 86
23 26 98
10 27 691
25 28 643
20 29 285
10 30 526
24 31 194
8 32 227
31 33 ...

output:

0
2941172
11343512
12924462
17855554
21198058
24537382
28404326
31235630
37238909
41457497
44153177
46367587
48542539
48979206
53792475
57142878
60713086
63117418
69318778
72151672
74564208
76224438
80214018
81348649
82938589
85545349
85600149
88350213
94639672
97551658
98692936
100619614
101454654
...

result:

ok 100000 numbers

Test #7:

score: 5
Accepted
time: 81ms
memory: 19620kb

input:

100000 100000
1 2 76
1 3 842
1 4 532
1 5 416
2 6 353
3 7 770
4 8 416
5 9 241
6 10 682
7 11 639
8 12 302
9 13 785
10 14 784
11 15 607
12 16 889
13 17 792
14 18 891
15 19 663
16 20 88
17 21 854
18 22 681
19 23 95
20 24 81
21 25 175
22 26 329
23 27 558
24 28 422
25 29 850
26 30 919
27 31 674
28 32 474
...

output:

0
186618787
6508155951
13472406803
16694918463
20520668211
21111265504
21595553324
29191369128
31065479106
37893580638
42766863995
43311506798
44675938098
45227927586
53320789257
53524963982
58935150002
60142944888
64659903596
65420584367
77280628303
77307909463
78481309735
83871278773
84128866873
8...

result:

ok 100000 numbers

Test #8:

score: 5
Accepted
time: 175ms
memory: 17592kb

input:

100000 100000
1 2 788
1 3 928
2 4 165
2 5 545
3 6 762
3 7 259
4 8 786
4 9 494
5 10 881
5 11 172
6 12 371
6 13 908
7 14 884
7 15 398
8 16 995
8 17 914
9 18 464
9 19 684
10 20 300
10 21 706
11 22 398
11 23 134
12 24 818
12 25 927
13 26 557
13 27 323
14 28 344
14 29 394
15 30 990
15 31 819
16 32 904
16...

output:

0
13759914
16740024
18992578
22378132
23867055
28122747
30335307
31749537
36290022
45900805
51670102
52846582
56682532
59579852
63392221
69141725
76492224
79540010
80805718
81557318
82453646
86256913
87664693
92106913
98535653
100697649
104040813
109689267
116355009
119615745
127498560
129757164
133...

result:

ok 100000 numbers

Test #9:

score: 5
Accepted
time: 88ms
memory: 20672kb

input:

100000 100000
1 2 494
2 3 30
3 4 81
4 5 890
5 6 547
6 7 811
7 8 245
8 9 175
9 10 126
10 11 871
11 12 826
12 13 705
13 14 527
14 15 360
15 16 447
16 17 800
17 18 794
18 19 340
19 20 789
20 21 335
21 22 873
22 23 826
23 24 770
24 25 537
25 26 591
26 27 523
27 28 455
28 29 599
29 30 912
30 31 789
31 32...

output:

0
1321281934
3273383378
3369180605
6324071000
11387753393
18745823952
22935591248
27194379359
31351270559
32494406246
33396381766
34072411474
37223527246
39099575574
41785497186
44803578260
45378947720
46497749960
48673492462
49086117362
49277813912
52073090561
52873514537
53524372531
57807707386
58...

result:

ok 100000 numbers

Test #10:

score: 5
Accepted
time: 84ms
memory: 20476kb

input:

100000 100000
1 2 386
2 3 954
3 4 980
4 5 584
5 6 346
6 7 854
7 8 763
8 9 824
9 10 909
10 11 174
11 12 635
12 13 498
13 14 578
14 15 468
15 16 248
16 17 739
17 18 175
18 19 531
19 20 93
20 21 939
21 22 795
22 23 250
23 24 54
24 25 124
25 26 273
26 27 758
27 28 597
28 29 279
29 30 149
30 31 869
31 32...

output:

0
78865957
4115127510
4379359961
8983360627
13939646129
14288398979
16468313958
18551100144
18959731752
19152610166
19234982666
24437334130
25261985129
31606394262
31834422949
32934548011
33722573553
34486298079
36943386891
38024794281
41257000979
45122113856
45507935256
45796378632
46923479232
5062...

result:

ok 100000 numbers

Test #11:

score: 5
Accepted
time: 87ms
memory: 20204kb

input:

100000 100000
1 2 126
2 3 500
3 4 234
4 5 710
5 6 254
6 7 818
7 8 959
8 9 242
9 10 768
10 11 479
11 12 5
12 13 453
13 14 774
14 15 371
15 16 170
16 17 685
17 18 202
18 19 992
19 20 647
20 21 307
21 22 925
22 23 410
23 24 328
24 25 153
25 26 807
26 27 719
27 28 590
28 29 812
29 30 515
30 31 778
31 32...

output:

0
1253969844
5229750118
5379380578
5858844028
12369873436
15741380112
20703898255
21673011216
21888820632
29724710966
30908563293
33953914115
38813000925
39351511105
41280319631
45326385903
45742182247
50306675747
53409974179
54971177879
55888596031
56270503656
57825436602
62338502796
64719388762
66...

result:

ok 100000 numbers

Test #12:

score: 5
Accepted
time: 78ms
memory: 20368kb

input:

100000 100000
1 2 949
2 3 876
3 4 748
4 5 477
5 6 381
6 7 771
7 8 32
8 9 235
9 10 81
10 11 184
11 12 358
12 13 786
13 14 421
14 15 673
15 16 841
16 17 184
17 18 556
18 19 350
19 20 930
20 21 584
21 22 882
22 23 109
23 24 189
24 25 160
25 26 883
26 27 817
27 28 234
28 29 150
29 30 817
30 31 254
31 32...

output:

0
304984820
2281997048
12466568768
12520108998
13691643380
17463343165
23693172719
28824977982
30744150598
30798434766
33989910996
34157359694
34584584023
42490477460
42522644090
45220861285
46393085186
46719896553
49203102708
53611547580
55200953764
57270254512
61305124842
65499790394
65588963192
6...

result:

ok 100000 numbers

Test #13:

score: 5
Accepted
time: 82ms
memory: 20508kb

input:

100000 100000
1 2 973
2 3 609
3 4 712
4 5 864
5 6 415
6 7 296
7 8 367
8 9 407
9 10 987
10 11 396
11 12 942
12 13 406
13 14 109
14 15 178
15 16 95
16 17 60
17 18 647
18 19 895
19 20 347
20 21 857
21 22 887
22 23 800
23 24 280
24 25 166
25 26 144
26 27 929
27 28 138
28 29 118
29 30 408
30 31 703
31 32...

output:

0
2986347540
3168201690
11550098457
17548666603
23394406928
24522512878
24832805302
25060287514
28288496431
29902415603
30524820361
33984834226
34469686213
35383551177
35560277981
36970218269
37027057790
37877833342
39302189290
42448698748
42592281586
42664488721
45674186895
50309066697
52399500796
...

result:

ok 100000 numbers

Test #14:

score: 5
Accepted
time: 92ms
memory: 20464kb

input:

100000 100000
1 2 249
2 3 541
3 4 679
4 5 789
5 6 301
6 7 218
7 8 281
8 9 730
9 10 906
10 11 54
11 12 568
12 13 610
13 14 445
14 15 120
15 16 370
16 17 214
17 18 876
18 19 701
19 20 935
20 21 227
21 22 492
22 23 280
23 24 627
24 25 1
25 26 451
26 27 163
27 28 554
28 29 759
29 30 386
30 31 150
31 32 ...

output:

0
3238447212
8341633329
10887421952
11041128183
12087645024
15608366976
16211711102
19869737008
19871529761
21410538785
24425352635
29014974147
30571338229
30887324809
32643004552
32696537416
38084734902
41074320738
41295569207
48184220501
48491747205
56265076384
56411895040
57705667975
59114032770
...

result:

ok 100000 numbers

Test #15:

score: 5
Accepted
time: 88ms
memory: 20584kb

input:

100000 100000
1 2 24
2 3 258
3 4 408
4 5 587
5 6 611
6 7 919
7 8 412
8 9 229
9 10 579
10 11 931
11 12 77
12 13 824
13 14 174
14 15 492
15 16 513
16 17 904
17 18 903
18 19 147
19 20 219
20 21 753
21 22 578
22 23 658
23 24 288
24 25 816
25 26 592
26 27 988
27 28 433
28 29 643
29 30 392
30 31 663
31 32...

output:

0
999307404
1411796346
1603694586
6113469852
7333919801
8392705536
9800537846
13730835380
21044686739
21295307609
22381625868
24878573683
25644235879
29575952505
29851450655
30120747035
33631386101
35395187700
36018281584
37921990816
38117465656
40615307650
46257835846
46845717930
48127516895
536712...

result:

ok 100000 numbers

Test #16:

score: 5
Accepted
time: 84ms
memory: 20676kb

input:

100000 100000
1 2 567
2 3 812
3 4 146
4 5 65
5 6 419
6 7 86
7 8 920
8 9 832
9 10 130
10 11 53
11 12 894
12 13 247
13 14 137
14 15 712
15 16 282
16 17 393
17 18 618
18 19 535
19 20 799
20 21 227
21 22 355
22 23 181
23 24 767
24 25 350
25 26 42
26 27 710
27 28 378
28 29 428
29 30 595
30 31 234
31 32 6...

output:

0
1462648668
1515454152
2088763980
4957302576
11099174256
14282625186
15337271768
17956353805
20408388089
21309235552
21992732392
22206647595
28381153838
32288113405
33929294980
34281946986
35962761883
38141565428
39842434169
44723361829
44763171173
46733530779
48585768831
49217086682
50465660032
52...

result:

ok 100000 numbers

Test #17:

score: 5
Accepted
time: 81ms
memory: 20512kb

input:

100000 100000
1 2 408
2 3 524
3 4 486
4 5 497
5 6 598
6 7 638
7 8 752
8 9 365
9 10 217
10 11 192
11 12 711
12 13 282
13 14 659
14 15 134
15 16 240
16 17 128
17 18 651
18 19 975
19 20 113
20 21 159
21 22 861
22 23 111
23 24 957
24 25 814
25 26 386
26 27 25
27 28 791
28 29 446
29 30 151
30 31 790
31 3...

output:

0
1263938820
7270345185
8128446765
10093042289
10093862721
12106182006
12832223553
12839281513
13778947690
14187644160
15365810058
15972085556
17533102878
21959546746
28062675233
34245349178
36071719124
40904074136
51015317169
52663559100
53989171140
57275537652
57962040362
62936713861
63057655661
6...

result:

ok 100000 numbers

Test #18:

score: 5
Accepted
time: 90ms
memory: 20600kb

input:

100000 100000
1 2 716
2 3 57
3 4 220
4 5 368
5 6 120
6 7 843
7 8 313
8 9 211
9 10 444
10 11 886
11 12 40
12 13 811
13 14 938
14 15 14
15 16 745
16 17 725
17 18 247
18 19 592
19 20 92
20 21 483
21 22 645
22 23 393
23 24 294
24 25 247
25 26 25
26 27 667
27 28 432
28 29 852
29 30 644
30 31 694
31 32 86...

output:

0
2198838135
2413557794
2570744805
4364595247
5336151391
9290260266
9739531088
10675484372
14545357290
14806687416
15728146769
16137699473
17234331308
21037825658
27084393640
30859507382
36542086813
37380421401
37466905866
37721979386
39053728616
40039727470
40923999574
42196787114
43358509643
46469...

result:

ok 100000 numbers

Test #19:

score: 5
Accepted
time: 88ms
memory: 20668kb

input:

100000 100000
1 2 405
2 3 375
3 4 830
4 5 199
5 6 993
6 7 711
7 8 797
8 9 864
9 10 402
10 11 809
11 12 852
12 13 250
13 14 5
14 15 268
15 16 403
16 17 63
17 18 589
18 19 502
19 20 568
20 21 663
21 22 922
22 23 605
23 24 824
24 25 585
25 26 169
26 27 951
27 28 65
28 29 116
29 30 71
30 31 561
31 32 31...

output:

0
2833279047
5339468667
6114151293
7888087483
11362555469
12046619353
14522326953
14544718923
15271452909
17580768093
17880598899
19060583814
19621031928
21583771464
26577362310
27698655041
34632601581
35112296563
40060831399
41369555289
41863084194
44353710894
46310575770
48113831260
48500230210
49...

result:

ok 100000 numbers

Test #20:

score: 5
Accepted
time: 90ms
memory: 20796kb

input:

100000 100000
1 2 246
2 3 34
3 4 21
4 5 38
5 6 476
6 7 769
7 8 511
8 9 127
9 10 165
10 11 71
11 12 638
12 13 416
13 14 29
14 15 681
15 16 594
16 17 708
17 18 560
18 19 948
19 20 433
20 21 773
21 22 963
22 23 302
23 24 194
24 25 676
25 26 414
26 27 229
27 28 790
28 29 474
29 30 517
30 31 279
31 32 53...

output:

0
1164404528
1245792644
1889518238
3428702738
5588795218
6252036268
7150173620
8899286165
9018353568
11536993048
11680853449
17650834973
19610253399
24375113989
25951200739
26836631233
27606157858
29875820476
35083388824
35602735934
37792882763
41040758805
43554414675
43559406712
50495783857
5100908...

result:

ok 100000 numbers