QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#233672#5094. 小 H 分糖果zyz07100 ✓612ms47208kbC++173.5kb2023-10-31 21:14:542023-10-31 21:14:54

Judging History

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

  • [2023-10-31 21:14:54]
  • 评测
  • 测评结果:100
  • 用时:612ms
  • 内存:47208kb
  • [2023-10-31 21:14:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
using i128=__int128;
ostream& operator<<(ostream& os,i128 x){
	if(x>=10) os<<x/10;
	return os<<int(x%10);
}
const int N=1e5+5,LogN=18,V=1e9;
int n,q,a[N],fa[N],dfn[N],dfx,siz[N],dep[N],anc[N][LogN];
vector<int> e[N];
void dfs(int u){
	dfn[u]=++dfx;
	siz[u]=1;
	dep[u]=dep[fa[u]]+1;
	anc[u][0]=fa[u];
	For(j,1,LogN-1){
		anc[u][j]=anc[anc[u][j-1]][j-1];
	}
	for(int v:e[u]){
		if(v!=fa[u]){
			fa[v]=u;
			dfs(v);
			siz[u]+=siz[v];
		}
	}
}
int lca(int u,int v){
	if(dep[u]<dep[v]) swap(u,v);
	for(int x=dep[u]-dep[v],i=0;x;x>>=1,++i){
		if(x&1) u=anc[u][i];
	}
	if(u==v) return u;
	Dec(i,LogN-1,0){
		if(anc[u][i]!=anc[v][i]){
			u=anc[u][i];
			v=anc[v][i];
		}
	}
	return anc[u][0];
}
struct Func{
	ll a,b;
	i128 c;
	Func operator+=(const Func& f){
		a+=f.a;
		b+=f.b;
		c+=f.c;
		return *this;
	}
	Func operator-() const{
		return {-a,-b,-c};
	}
	Func operator+(const Func& f) const{
		return Func(*this)+=f;
	}
	Func operator-(const Func& f) const{
		return Func(*this)+=-f;
	}
};
struct BIT{
	Func t[N];
	vector<int> pos;
	void add(int p,Func x){
		pos.push_back(p);
		for(;p<=n;p+=p&-p) t[p]+=x;
	}
	void range_add(int l,int r,Func x){
		add(l,x);
		add(r+1,-x);
	}
	Func query(int p){
		Func res{};
		for(;p;p&=p-1) res+=t[p];
		return res;
	}
	Func chain_query(int u,int v,int lca){
		return query(dfn[u])+query(dfn[v])-query(dfn[lca])-query(dfn[fa[lca]]);
	}
	void clear(){
		for(int p:pos){
			for(;p<=n;p+=p&-p) t[p]={};
		}
		pos.clear();
	}
}bit;
struct Oper{
	int tp,u,v;
	ll x,y;
}op[N*2];
int plca[N*2];
ll val[N*2];
Func sum[N*2];
void solve(ll l,ll r,const vector<int>& ops){
	if(l>r||!ops.size()) return;
	ll mid=(l+r)/2;
	bit.clear();
	vector<int> lops,rops;
	for(int i:ops){
		auto [tp,u,v,x,y]=op[i];
		if(tp==1){
			if(mid<=y&&y<=r){
				bit.range_add(dfn[u],dfn[u]+siz[u]-1,{-1,-y,-y*y});
			}
			if(mid<=x&&x<=r){
				bit.range_add(dfn[u],dfn[u]+siz[u]-1,{1,x,x*x});
			}
			if((l<=x&&x<mid)||(l<=y&&y<mid)){
				lops.push_back(i);
			}
			if((mid<x&&x<=r)||(mid<y&&y<=r)){
				rops.push_back(i);
			}
		}else{
			Func f=bit.chain_query(u,v,plca[i]);
			if(sum[i].b+f.b-(f.a+sum[i].a)*mid<=x){
				val[i]=mid;
				sum[i]+=f;
				lops.push_back(i);
			}else{
				rops.push_back(i);
			}
		}
	}
	solve(l,mid-1,lops);
	solve(mid+1,r,rops);
}
i128 ans[N*2];
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n>>q;
	For(i,1,n-1){
		int u,v;
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	For(i,1,n) cin>>a[i];
	dfs(1);
	For(i,1,n){
		op[i]={1,i,0,a[i],ll(1e18)};
	}
	i128 sum2=0;
	For(i,1,n) sum2+=1LL*a[i]*a[i];
	For(i,n+1,n+q){
		cin>>op[i].tp>>op[i].u;
		if(op[i].tp==1){
			cin>>op[i].x;
			op[i].y=a[op[i].u];
			sum2-=op[i].y*op[i].y;
			a[op[i].u]=op[i].x;
			sum2+=op[i].x*op[i].x;
		}else{
			cin>>op[i].v>>op[i].x;
			plca[i]=lca(op[i].u,op[i].v);
			ans[i]=sum2;
		}
	}
	vector<int> ops(n+q);
	iota(range(ops),1);
	solve(0,V,ops);
	For(i,1,n+q){
		if(op[i].tp==2){
			if(!val[i]){
				cout<<ans[i]-sum[i].c<<'\n';
				continue;
			}
			ll left=op[i].x-(sum[i].b-val[i]*sum[i].a);
			cout<<ans[i]-sum[i].c+i128(val[i])*val[i]*(sum[i].a-left)+i128(val[i]-1)*(val[i]-1)*left<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 16268kb

input:

866 841
1 864
4 153
9 559
10 410
11 336
12 417
14 666
18 241
21 184
22 849
23 40
25 783
26 189
28 329
29 216
31 864
34 581
40 131
42 625
45 744
47 723
50 633
51 447
52 454
53 88
55 619
60 259
62 680
67 126
72 371
73 742
80 196
81 536
82 647
85 254
87 172
88 489
93 708
94 227
95 340
96 7
7 91
97 594
...

output:

285125508
285374449
285871392
285072359
284419704
284843737
284692039
284936099
285944374
285174668
285019779
284651455
287282253
287175619
284878507
285369672
284880507
285404741
284913527
286053317
288622563
286960150
287194443
288326074
286937403
287883097
288535226
288195055
288643208
288632989
...

result:

ok 419 lines

Test #2:

score: 0
Accepted
time: 4ms
memory: 14200kb

input:

951 896
6 886
8 809
9 392
12 590
13 643
16 322
17 613
37 605
42 354
44 594
47 905
49 623
53 240
56 546
58 751
60 919
62 74
63 303
64 105
68 578
69 41
41 110
71 402
74 528
75 616
76 83
77 230
83 289
84 354
88 340
89 65
91 785
92 831
93 286
96 101
97 841
99 556
100 700
101 574
102 652
103 534
105 107
...

output:

323762325
323477550
323340853
323950611
323726768
322803839
322828150
323927499
323452292
322435378
323945420
323767611
322557229
323137116
322736140
322900873
322078759
322286746
322383436
322825503
321513631
320299707
320738958
320752861
319979668
320430841
319960294
319658294
317899682
318822085
...

result:

ok 443 lines

Subtask #2:

score: 15
Accepted

Dependency #1:

100%
Accepted

Test #3:

score: 15
Accepted
time: 3ms
memory: 12080kb

input:

903 809
2 4
5 604
6 396
10 637
12 593
22 855
26 586
29 170
34 64
37 559
42 413
44 789
46 584
49 83
52 642
55 688
59 899
61 614
68 572
74 707
79 278
80 32
83 315
86 779
87 323
89 901
91 720
92 463
93 592
98 348
99 644
100 465
102 377
103 835
104 479
107 757
111 207
112 458
116 43
43 623
121 538
124 1...

output:

276805063584166248522
273874231351955459944
277417224646898680385
276526352730593552950
276097972273805693659
277000626431768466948
275360041448269408386
277555271820331288115
275027390500562660091
275359996505750169694
273988679638576452218
277452768032389764786
279717343951191633999
27734436115806...

result:

ok 399 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 12000kb

input:

971 875
1 259
4 861
6 917
7 397
8 943
10 11
19 105
20 319
23 330
31 665
32 25
34 247
38 794
39 356
44 169
46 552
51 776
52 74
54 491
55 657
56 132
62 94
63 534
65 867
66 398
68 628
69 909
71 791
75 769
80 98
83 540
84 553
86 404
89 28
91 929
95 732
96 82
82 121
98 460
100 565
102 640
108 849
109 582...

output:

314553601458474340784
315008648334632896915
309593803308875213443
314381387084951628984
314980104154667100244
315415916743855708963
311283117410815259761
310699439139678733298
315534307669378140861
310218082581673983393
313516726064555225372
312528429437654618149
311275276237153741600
31488347816476...

result:

ok 423 lines

Test #5:

score: 0
Accepted
time: 6ms
memory: 12284kb

input:

863 950
4 210
7 429
10 484
12 11
13 524
14 856
17 369
21 616
22 608
24 348
26 312
30 676
32 235
33 217
36 659
40 51
42 410
45 415
54 142
64 294
66 332
70 129
71 530
75 635
76 741
77 749
86 731
88 170
90 107
93 523
94 503
95 493
96 202
99 700
100 23
103 51
51 387
109 495
113 823
114 41
116 91
91 492
...

output:

270446496979315412554
271870043667332946529
272456686591499140527
269284545566217062767
270990125394886733462
267756299196219401987
271960154369666524587
269381518805814804923
269742070489494972424
271327427174666570120
269616849392122791048
271332644790428015294
270212908721540092053
26902859752821...

result:

ok 485 lines

Subtask #3:

score: 15
Accepted

Test #6:

score: 15
Accepted
time: 481ms
memory: 44944kb

input:

87080 98363
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

27217464773998101198216
27222683135365131711066
27215685950441383375941
27221607244120669838311
27219047117137492446677
27222635053035794978138
27218848172360084265818
27217641965048032442370
27217075857038185043354
27219505943263517662069
27219987830714690994915
27216425553487126261338
272156845500...

result:

ok 49026 lines

Test #7:

score: 0
Accepted
time: 475ms
memory: 47208kb

input:

84870 94829
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

26588824183614252611956
26590972957572618361173
26588072360121209836363
26591857847070561616545
26589232564845670209408
26592122980539987339833
26587754880131177015120
26589297513918827289821
26587809270957143483620
26589079923893149835091
26592765548568891098479
26589309295038049830001
265880100172...

result:

ok 47183 lines

Test #8:

score: 0
Accepted
time: 435ms
memory: 45032kb

input:

96634 80387
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

30077825779968347451702
30074759816356090849186
30079514925011418879668
30076799133737016237726
30078990001739930674611
30080891961908067423095
30078486689897323991187
30075141829412423915885
30080001764736455079465
30073677391611280633894
30079391586644084223862
30080833751860317298636
300797099430...

result:

ok 40329 lines

Subtask #4:

score: 60
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #9:

score: 60
Accepted
time: 611ms
memory: 40448kb

input:

94940 96559
2 48868
6 41587
9 60037
10 4957
15 63113
24 2993
25 43059
26 23799
27 58856
31 92716
33 50875
34 864
37 46030
40 78345
42 75973
44 68250
45 3555
46 730
49 13522
50 26971
51 30585
52 29379
58 10570
60 66269
64 55951
66 89776
69 93660
70 28512
71 16310
75 91744
78 5146
81 8033
89 14642
90 ...

output:

29617178670453413164069
29614639318060085437952
29618450865028118043190
29619985987394160724901
29615742988245945792595
29614901462561753867573
29620072586799466410363
29617293585334506103852
29613760321829811476882
29613323124665517085193
29616445678681741866923
29616375117753770090809
296200415481...

result:

ok 48420 lines

Test #10:

score: 0
Accepted
time: 572ms
memory: 37312kb

input:

92909 88568
2 38837
7 23606
9 31973
11 64196
14 19356
16 87451
18 60393
19 28396
22 15565
27 20808
30 31200
32 82517
36 85991
40 1741
43 67907
44 44085
47 81860
48 59854
49 4207
57 66676
63 19475
64 89400
66 72124
67 30741
68 16760
69 40672
75 14963
80 48002
83 38296
84 59995
87 48302
90 66261
95 54...

output:

29039622114245355680205
29042133490584794491465
29042134529391585277903
29043130284501190417689
29038323779622901253059
29038172876950039396616
29041633851326663678067
29039713609184724254856
29040451531799550028003
29037990896347268862283
29044209917096062244550
29039063517923986375692
290388827470...

result:

ok 44141 lines

Test #11:

score: 0
Accepted
time: 572ms
memory: 37972kb

input:

88744 92712
3 67939
5 55525
7 25803
13 322
15 23495
16 35032
20 60161
24 38954
25 22935
26 43589
27 74706
29 84238
36 81344
37 20516
39 23718
40 1353
41 3892
46 44001
52 4453
58 8226
61 83456
62 1653
64 81309
66 67462
67 26215
68 53895
72 81735
74 36346
75 71003
76 49809
78 32549
83 79252
85 38584
9...

output:

27763054595703095010201
27761805282892389166313
27764806021233989502588
27762758324079708001229
27762687641324201886042
27764698845215222931581
27760748590866105864059
27765789999503636423441
27760753381114745348807
27764624914565139257370
27767536395536852424149
27765886676720128824474
277595630205...

result:

ok 46266 lines

Test #12:

score: 0
Accepted
time: 559ms
memory: 37556kb

input:

87431 91680
1 59814
8 22323
18 63293
23 21500
24 58151
27 75135
28 11619
33 33172
40 72604
43 71930
47 38032
50 26106
53 32630
54 32589
58 12590
59 33261
63 48390
65 80802
66 19442
67 66358
68 38040
77 42300
78 916
79 75574
80 74249
86 29286
88 1567
90 11586
91 35241
92 82326
95 67126
96 13765
99 32...

output:

27448286964824733288286
27454701479347516441235
27454683374314189835413
27450427530118816516890
27454182879158178477726
27450605619170491641834
27452383594650539202498
27453683353626530822166
27448782822680690469135
27449222418501090065613
27448200864936013863620
27448627249380693570952
274488471815...

result:

ok 45868 lines

Test #13:

score: 0
Accepted
time: 572ms
memory: 36972kb

input:

93272 87053
3 31927
7 62563
10 34002
11 29436
15 19960
16 91684
19 89565
35 73854
36 17945
37 28837
44 89458
47 76049
48 88980
49 63436
56 47941
61 64922
63 89481
65 57411
66 32260
67 17906
68 18377
69 86046
72 81015
73 63269
74 89489
76 79843
82 17663
84 69510
86 19800
91 54311
93 81787
100 15886
1...

output:

29056221183093330184048
29054578698086899936242
29056873096627921857932
29054100774377459167276
29053368754182788898645
29057915476879102172383
29052070168127844866662
29058877045318046901426
29054546189420990732244
29054343071624366408399
29053842299767929116179
29053181536617017689691
290541306698...

result:

ok 44002 lines

Test #14:

score: 0
Accepted
time: 612ms
memory: 37912kb

input:

91781 98478
11 85856
14 35582
17 80758
20 46792
25 21529
28 61873
29 70714
38 89243
39 29334
40 14347
41 33114
44 15241
46 76597
47 48533
50 48548
52 2933
53 60696
60 32621
61 3335
65 35458
70 29797
75 36143
81 69545
83 11982
86 13122
87 88531
88 90561
89 45416
91 89678
94 64469
95 4507
98 28377
99 ...

output:

28782422324521922802692
28783485938350732626529
28782324523862606694578
28784240647247276024732
28781181414723843486104
28783718988571615902711
28785180056432206354752
28783356322626866296471
28781825028549123211986
28781759226580819919330
28782789159180147345780
28784157446331230828818
287806200961...

result:

ok 48964 lines

Test #15:

score: 0
Accepted
time: 577ms
memory: 39572kb

input:

93213 89341
4 11442
6 92167
7 2029
12 11358
15 52454
16 47587
17 27944
19 15138
20 81244
22 83430
27 79509
28 53639
30 24318
31 81336
32 26528
36 3215
41 35781
42 15199
46 78708
50 18980
52 45224
55 44885
56 32390
57 27984
58 51298
65 35507
66 8448
67 90285
69 40700
70 60887
75 35715
77 32757
80 907...

output:

29043053959560611054703
29043054128600290562510
29048275452985925406602
29046971102706073425294
29050100427123367378639
29044646619898448468758
29049591167415590909610
29046103278712073850379
29045579754278491219218
29042202732501369837641
29049207340917792067709
29045368481138270925337
290430347108...

result:

ok 44761 lines

Test #16:

score: 0
Accepted
time: 578ms
memory: 40020kb

input:

86352 92008
1 38667
2 45309
3 67754
8 33843
11 80412
12 32254
16 38109
17 67281
18 66786
19 3687
20 76923
21 19289
23 54126
24 48815
26 34836
28 81321
30 62104
33 6162
36 66324
38 53561
43 29458
44 51318
46 8004
47 75640
49 38883
50 45940
52 83225
53 82615
58 23857
60 33360
61 47053
64 39959
66 2457...

output:

26919472717180992779798
26923422460084460889372
26924624775718779936282
26925390000186177814769
26922758087798637204713
26921327665801769141078
26924888035032055581404
26922820368063934854488
26919460585026531484754
26918012499378054617380
26923927370492849031024
26919429263740070163331
269185468438...

result:

ok 46086 lines

Test #17:

score: 0
Accepted
time: 587ms
memory: 39632kb

input:

91116 94811
1 41802
2 86232
3 31911
4 7752
7 10810
9 10937
11 80400
17 54222
21 42714
23 84748
25 89183
27 35219
28 4378
30 75535
32 64973
35 77831
36 25837
41 56131
43 53660
45 37676
51 8641
54 66526
58 19375
59 65526
61 22689
63 77258
64 36297
67 84056
69 85347
76 48487
79 48715
82 75266
84 79641
...

output:

28524841437969999026756
28524730842815138931479
28522518167491416799247
28522900030885217210116
28524187824885394125378
28521206116268588659718
28524360654186650438070
28526370954315383738635
28521577693228326042169
28518869352363367730919
28523753738707556280807
28525486314794016092302
285246107721...

result:

ok 47310 lines

Test #18:

score: 0
Accepted
time: 578ms
memory: 39548kb

input:

92001 90905
2 72450
3 79373
4 38376
5 74386
8 40588
12 23276
18 19026
20 30337
22 67690
25 52798
28 41193
30 49515
31 73296
33 80541
36 26244
40 69211
42 14213
43 26382
46 61504
48 86976
52 23274
54 30276
55 9572
62 67353
68 23350
74 73074
76 32254
82 5134
87 21512
100 42670
101 9446
102 16125
105 3...

output:

28753531593818960221305
28755452153078058981045
28756488437372589743203
28754186128573213841630
28752492814332455875443
28754322994569829301533
28748122959601083831333
28751489131434185482263
28749857375014599205167
28747869025547444982246
28747960519871593650245
28747844642196770855430
287532134737...

result:

ok 45599 lines

Test #19:

score: 0
Accepted
time: 579ms
memory: 37792kb

input:

97536 90246
1 63763
11 17565
12 66863
15 35761
16 27712
17 44633
22 20388
23 81366
24 16327
25 62672
26 69802
28 89880
31 31709
32 20852
34 97096
39 59942
40 5712
41 81032
43 28706
44 12823
50 1851
54 85613
55 32343
58 96581
59 77105
60 35634
64 24391
66 46307
70 1730
74 89402
77 52010
79 18663
80 3...

output:

30446158886087439313103
30444238811141455938001
30448976960676601675962
30450645669532366751580
30450427672450688805644
30443476923634315303490
30445737022610695394253
30449701334575138715542
30450377103762831825565
30447853474445403217816
30444666594695168431279
30448318437622756328971
304451096474...

result:

ok 45217 lines

Test #20:

score: 0
Accepted
time: 592ms
memory: 40380kb

input:

91063 94256
6 31211
7 51732
11 60290
18 8659
20 75005
25 9129
30 15266
31 15929
32 84152
36 41661
37 69614
38 47267
50 69414
52 15045
53 1760
54 40154
55 17463
57 73471
63 22569
65 37167
68 14176
70 50264
73 41799
75 84985
79 16888
80 61940
84 75801
85 43080
86 49200
91 18889
95 63647
97 14364
106 8...

output:

28477806633488164383323
28477809851374200523548
28482080204253616390033
28479494567476408232367
28484267957719413758288
28479113071027811783871
28479296714954786985761
28477290167093573917491
28478596033860629678079
28479540502811276708690
28477973442211842447807
28482087790586485421657
284765832171...

result:

ok 47172 lines