QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#588313#5418. Color the TreeN_z_AC ✓41ms28012kbC++147.3kb2024-09-25 09:24:512024-09-25 09:24:53

Judging History

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

  • [2024-09-25 09:24:53]
  • 评测
  • 测评结果:AC
  • 用时:41ms
  • 内存:28012kb
  • [2024-09-25 09:24:51]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
struct time_helper{
#ifdef LOCAL
clock_t time_last;time_helper(){time_last=clock();}void test(){auto time_now=clock();std::cerr<<"time:"<<1.*(time_now-time_last)/CLOCKS_PER_SEC<<";all_time:"<<1.*time_now/CLOCKS_PER_SEC<<std::endl;time_last=time_now;}~time_helper(){test();}
#else
void test(){}
#endif
}time_helper;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
int print_precision=10;bool print_T_endl=1;char print_between=' ';
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader;struct Writer;template<size_t id>struct read_tuple{template<typename...T>static void read(Reader&stream,std::tuple<T...>&x){read_tuple<id-1>::read(stream,x);stream>>get<id-1>(x);}};template<>struct read_tuple<0>{template<typename...T>static void read([[maybe_unused]]Reader&stream,[[maybe_unused]]std::tuple<T...>&x){}};template<size_t id>struct print_tuple{template<typename...T>static void print(Writer&stream,const std::tuple<T...>&x){print_tuple<id-1>::print(stream,x);putchar(print_between);stream<<get<id-1>(x);}};template<>struct print_tuple<1>{template<typename...T>static void print(Writer&stream,const std::tuple<T...>&x){stream<<get<0>(x);}};template<>struct print_tuple<0>{template<typename...T>static void print([[maybe_unused]]Writer&stream,[[maybe_unused]]const std::tuple<T...>&x){}};
struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename...T>Reader&operator>>(std::tuple<T...>&x){read_tuple<sizeof...(T)>::read(*this,x);return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}template<typename T1,typename T2>Reader&operator>>(std::pair<T1,T2>&x){*this>>x.first>>x.second;return *this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';
struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(const T&x){for(auto q:x){*this<<q;if(!is_class<decltype(q)>::value)*this<<print_between;}if(!is_class<typename T::value_type>::value&&print_T_endl)*this<<'\n';return *this;}template<typename...T>Writer&operator<<(const std::tuple<T...>&x){print_tuple<sizeof...(T)>::print(*this,x);if(print_T_endl)*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-print_precision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<print_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<print_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(const T&c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}template<typename T1,typename T2>Writer&operator<<(const std::pair<T1,T2>&x){*this<<x.first<<print_between<<x.second;if(print_T_endl)*this<<'\n';return *this;}Writer&operator<<(const std::string&str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<class Fun>class y_combinator_result{Fun fun_;public:template<class T>explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}template<class ...Args>decltype(auto) operator()(Args &&...args){return fun_(std::ref(*this), std::forward<Args>(args)...);}};template<class Fun>decltype(auto) y_combinator(Fun &&fun){return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));}

void init();void solve(int tc);
main()
{
	init();int t=1;
	cin>>t;
	for(int tc=1;tc<=t;tc++)solve(tc);
}
void init()
{
}
void solve([[maybe_unused]]int tc)
{
	int n;
	cin>>n;
	vector<vector<int>>a(21,vector<int>(n));
	cin>>a[0];
	for(int y=1;1<<y<=n;y++)
	for(int x=0;x+(1<<y)-1<n;x++)
	a[y][x]=min(a[y-1][x],a[y-1][x+(1<<y-1)]);
	auto query=[&](int l,int r)
	{
		int k=__lg(r-l+1);
		return min(a[k][l],a[k][r-(1<<k)+1]);
	};
	vector<vector<int>>e(n+1);
	for(int x=1;x<n;x++)
	{
		int u,v;
		cin>>u>>v;
		e[u].emplace_back(v);
		e[v].emplace_back(u);
	}
	vector<int>h(n+1),mxs(n+1);
	y_combinator([&](auto dfs,int u,int f)->void
	{
		h[u]=1,mxs[u]=0;
		for(auto v:e[u])
		if(v!=f)
		{
			dfs(v,u);
			if(h[v]>=h[u])h[u]=h[v]+1,mxs[u]=v;
		}
	})(1,0);
	long long ans=0;
	int dc=0;
	vector<int>dfn(n+1);
	vector<long long>res(n+1);
	y_combinator([&](auto dfs,int u,int f,int dep)->void
	{
		res[dfn[u]=++dc]=query(0,dep);
		if(mxs[u])dfs(mxs[u],u,dep+1);
		int mxh=0;
		for(auto v:e[u])
		if(v!=f&&v!=mxs[u])
		{
			dfs(v,u,dep+1);
			mxh=max(mxh,h[v]);
			for(int x=0;x<h[v];x++)
			res[dfn[u]+x+1]+=res[dfn[v]+x];
		}
		for(int x=1;x<=mxh;x++)
		res[dfn[u]+x]=min<long long>(res[dfn[u]+x],query(x,x+dep));
	})(1,0,0);
	for(int x=1;x<=h[1];x++)
	ans+=res[x];
	cout<<ans<<endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: 0
Accepted
time: 21ms
memory: 3740kb

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:

180
168
222
230
156
240
225
126
100
81
155
73
154
127
149
124
228
230
132
187
153
170
78
282
195
286
191
211
119
197
211
233
88
252
239
233
173
180
195
121
109
148
180
175
226
210
182
97
199
59
56
31
115
204
203
172
139
208
53
140
189
170
173
137
233
94
163
273
80
350
156
133
146
159
240
269
137
222...

result:

ok 3000 numbers

Test #3:

score: 0
Accepted
time: 18ms
memory: 3924kb

input:

300
474
5 24 21 41 15 23 43 48 32 19 27 40 10 49 40 6 18 41 43 31 45 18 35 36 12 10 23 45 28 23 14 43 37 45 12 16 20 17 49 13 22 8 30 19 27 40 22 14 30 47 16 39 25 48 21 26 50 8 14 26 9 30 41 15 44 24 16 46 50 39 25 47 24 45 21 18 26 21 5 39 15 10 47 48 11 44 44 33 23 14 35 39 35 30 38 9 13 15 39 5 ...

output:

329
183
264
219
323
220
348
342
410
395
80
201
299
144
207
408
360
215
283
104
320
394
277
210
273
285
242
253
265
379
360
322
202
351
195
196
266
270
171
342
239
283
286
300
331
317
345
268
173
296
275
224
480
330
264
162
199
378
254
214
231
293
229
259
241
268
380
419
233
185
364
341
328
237
320
3...

result:

ok 300 numbers

Test #4:

score: 0
Accepted
time: 16ms
memory: 4812kb

input:

30
4926
18 13 47 7 21 39 28 48 21 44 14 18 39 13 46 33 6 49 9 7 10 29 29 25 38 15 16 42 41 41 40 14 26 13 6 19 17 31 24 18 30 24 48 46 38 21 28 42 29 50 33 28 18 26 18 42 13 23 35 20 32 6 17 25 44 46 35 36 50 24 7 29 34 14 41 41 8 33 22 46 7 6 22 40 24 8 44 36 38 13 37 37 25 22 7 43 50 33 19 44 24 4...

output:

427
520
443
359
427
408
371
415
482
436
269
530
478
485
435
453
418
503
443
453
405
425
347
564
297
435
559
453
213
395

result:

ok 30 numbers

Test #5:

score: 0
Accepted
time: 22ms
memory: 3780kb

input:

3000
74
555233197 518812085 998593787 821753058 967306600 663279435 696954042 885219300 489323226 146136486 447383587 785317885 838306349 708275482 715157265 298848995 400280904 374077023 673881523 207667786 885945020 459791675 992519779 327830583 821713695 253985403 926395863 419409783 138881726 80...

output:

6742611216
5794349776
3087356867
4707144715
2761702533
3246645261
4802134565
2999820393
4887036613
2784978973
3593730307
4783057633
4621084176
4331196830
4242984461
2287799528
3027767371
3699192818
3888960419
6398323452
2766114996
1734720583
6543430036
1955540148
5464479116
3177069662
5145942113
302...

result:

ok 3000 numbers

Test #6:

score: 0
Accepted
time: 20ms
memory: 3872kb

input:

300
621
259262308 372414267 976777900 567821544 262206094 972740633 932600104 702535786 494092920 919901107 797100568 708295156 632473907 101958470 952065075 970482879 183543308 323078517 719011818 352232578 159576652 124505381 125133768 492132730 331846050 577415810 369370004 871034176 529186574 44...

output:

8143086197
8197999468
5370721620
5343127707
5868323006
7992625789
5749423188
5019336842
5319894438
5228239187
5391752908
6084605805
6792215852
6057910407
8471127525
2719747215
6909535671
5100581420
5878004843
5586237425
6343902433
9390109727
5651124389
5472179570
7945151774
5064107530
4433748186
571...

result:

ok 300 numbers

Test #7:

score: 0
Accepted
time: 19ms
memory: 4640kb

input:

30
5308
560111855 290003681 946208440 140658046 860834453 480249720 506770353 922783074 600720525 693059141 436061359 545671168 528534807 339705109 831632761 570564203 113225613 578123930 293066534 269996029 765346927 443717770 933144287 856263710 475170893 174188152 464281143 864607591 443380284 12...

output:

8829755982
7996435040
9259425768
7684533044
9842457103
3917213508
5939555066
8695995697
9431906955
7466353560
8322921019
8970732656
8099619221
9390765699
6773331885
8521621715
9998520099
7876760589
6482847050
10167157889
8563826262
5569616375
7783052317
7313404561
7224267995
8986870714
9082031438
99...

result:

ok 30 numbers

Test #8:

score: 0
Accepted
time: 21ms
memory: 5668kb

input:

30
235
99 26 36 76 38 12 81 57 32 53 24 100 83 36 73 40 99 67 25 59 13 53 26 96 88 91 70 75 50 28 43 91 28 80 21 10 28 96 81 46 93 48 47 65 16 51 39 13 17 68 87 47 11 53 35 59 95 17 12 28 42 72 69 93 10 99 55 36 17 10 17 82 46 47 30 13 33 46 47 82 26 70 89 11 84 15 75 82 23 15 26 21 33 100 80 68 59 ...

output:

1853
53585
70793
41175
65095
19429
62735
44418
35618
52989
22194
74287
66783
60324
23354
10188
45849
43317
47709
44425
17639
2392
67454
75522
52049
63546
17778
37186
1857
31275

result:

ok 30 numbers

Test #9:

score: 0
Accepted
time: 31ms
memory: 28012kb

input:

3
99260
99 92 50 79 91 45 21 68 66 95 60 65 65 45 85 36 33 49 93 97 17 80 84 82 53 62 68 77 54 84 19 75 37 54 64 80 88 60 26 31 73 14 50 19 31 91 28 49 49 92 98 41 30 21 42 83 51 79 48 51 41 10 73 83 61 43 51 95 80 19 46 45 43 62 86 52 62 100 22 98 25 67 76 59 55 42 76 18 17 63 38 92 73 22 58 93 65 ...

output:

745947
689647
711794

result:

ok 3 number(s): "745947 689647 711794"

Test #10:

score: 0
Accepted
time: 39ms
memory: 24832kb

input:

3
100000
736164847 712451679 953221063 129734069 649878938 636159027 756625444 636178736 261073374 499660659 102302453 703591271 759851774 246224168 542866587 140617030 541228236 263272492 844843580 256780933 617601578 765332709 439622302 345560268 242255574 736020813 919249591 429525347 775345503 8...

output:

8765474998668
8767125090439
8759555000012

result:

ok 3 number(s): "8765474998668 8767125090439 8759555000012"

Test #11:

score: 0
Accepted
time: 30ms
memory: 5812kb

input:

30
10000
820875351 110118090 318290090 291550265 156728512 898695407 702936634 537529650 492026966 990954215 887311683 471855239 487268950 596796482 921910579 683211841 356504873 821436540 819581602 702676749 720024595 328497612 866905494 831557624 659171036 168505311 122782601 291094304 671588990 9...

output:

883467120694
893610662749
883906059936
882107337810
879231409121
884351970198
892461121041
888728631421
876218733693
882844635398
886784539966
883172718934
884651829402
884399545744
890807049327
887954375238
883852918523
888403782419
887498755532
880151218208
892907290650
882172390896
881732561237
8...

result:

ok 30 numbers

Test #12:

score: 0
Accepted
time: 37ms
memory: 19284kb

input:

3
100000
909474963 414166441 677161271 688542123 650201469 390309276 856663547 621207079 459811934 582838909 425785542 857661802 918712852 367645535 521783456 937759651 260632908 430905661 671167895 796755368 996221059 593819531 523770923 894242006 779434511 193459764 316358533 460721669 825011706 3...

output:

57147733548
44853213726
44403945508

result:

ok 3 number(s): "57147733548 44853213726 44403945508"

Test #13:

score: 0
Accepted
time: 41ms
memory: 19028kb

input:

3
100000
784731820 441612049 231013785 411550408 129294588 649753537 481462845 676592818 778982959 179403366 119330183 246561078 480033332 904236648 531363073 453276112 858901112 261361645 385753122 773663421 838636681 867032978 217985662 757527556 801360921 400949426 431795344 842282949 460946349 5...

output:

164667798192
128371764672
111980717406

result:

ok 3 number(s): "164667798192 128371764672 111980717406"

Test #14:

score: 0
Accepted
time: 38ms
memory: 19580kb

input:

3
100000
596147745 223984585 321060496 836040854 531932227 164460971 662224682 418129867 444375231 885587796 208707375 692215658 908699579 849426347 686769155 299416570 320642243 432987479 452056946 250651084 751940769 780605821 386414554 290323159 222811216 656912068 831464659 621638040 952600162 9...

output:

435962040433
558704468744
578552805560

result:

ok 3 number(s): "435962040433 558704468744 578552805560"

Test #15:

score: 0
Accepted
time: 32ms
memory: 3828kb

input:

300
1000
306155973 502827111 154815976 498580847 143323927 427577658 729017009 385469320 282879354 730478149 292829273 716249730 785070076 560330250 598364372 939399616 585039212 850280897 722508936 220628755 135815134 579721227 353260095 293228175 586309693 503298178 847459502 536849421 625285745 6...

output:

10262465475
19119326129
12062920235
12137743449
13594380675
17341639220
10991714214
18829859456
17577744690
14603342312
17177860522
19397559314
22251073639
13630162409
15953666923
23921433778
23214301266
24629614692
13126058238
26897894988
10714431253
15557242432
18578687240
13481669075
18359475028
...

result:

ok 300 numbers

Test #16:

score: 0
Accepted
time: 16ms
memory: 19228kb

input:

3
100000
114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 114514 1145...

output:

229028
229028
229028

result:

ok 3 number(s): "229028 229028 229028"