UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#148183#240. treechsuhan1001713ms1272kbC++2.5kb2022-04-16 14:16:282022-04-16 14:16:31

answer

#include<bits/stdc++.h>
#define ll long long
#define inf 1e9
#define rep(i,l,r) for(register int i=l;i<=r;++i)
#define per(i,r,l) for(register int i=r;i>=l;--i)
using namespace std;
const int N=1010,Mod=998244353; 

struct SGT{
	int maxx,minn;
}sgt[N<<2];
struct edge{
	int nxt,to;
}e[N<<1];
int head[N],num;
int n;
int siz[N],res[N],dep[N],son[N];
int w[N],wx[N],idx[N],top[N],cnt;
ll ans;
inline void add(int from,int to){
	e[++num].nxt=head[from];
	e[num].to=to;
	head[from]=num;
} 
inline void dfs1(int u,int fa){
	siz[u]=fa;
	res[u]=fa;
	dep[u]=dep[fa]+1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==fa)continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])son[u]=v;
	}
}
inline void dfs2(int u,int topf){
	idx[u]=++cnt;
	wx[cnt]=w[u];
	top[u]=topf;
	if(!son[u])return;
	dfs2(son[u],topf);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==res[u]||v==son[u])continue;
		dfs2(v,v);
	}
}
inline void pushup(int p){
	sgt[p].maxx=max(sgt[p<<1].maxx,sgt[p<<1|1].maxx);
	sgt[p].minn=min(sgt[p<<1].minn,sgt[p<<1|1].minn);
}
inline void build(int p,int l,int r){
	if(l==r){
		sgt[p].maxx=sgt[p].minn=wx[l];
		return;
	}
	int mid=l+r>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	pushup(p);
}
inline int query_max(int p,int l,int r,int x,int y){
	if(l>=x&&r<=y)return sgt[p].maxx;
	int ret=0;
	int mid=l+r>>1;
	if(x<=mid)ret=max(ret,query_max(p<<1,l,mid,x,y));
	if(y>mid)ret=max(ret,query_max(p<<1|1,mid+1,r,x,y));
	return ret;
}
inline int query_min(int p,int l,int r,int x,int y){
	if(l>=x&&r<=y)return sgt[p].minn;
	int ret=inf;
	int mid=l+r>>1;
	if(x<=mid)ret=min(ret,query_min(p<<1,l,mid,x,y));
	if(y>mid)ret=min(ret,query_min(p<<1|1,mid+1,r,x,y));
	return ret;
}
inline int ask(int x,int y){
	int maxx=0,minn=inf;
	while (top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		maxx=max(maxx,query_max(1,1,n,idx[top[x]],idx[x]));
		minn=min(minn,query_min(1,1,n,idx[top[x]],idx[x]));
		x=res[top[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	maxx=max(maxx,query_max(1,1,n,idx[x],idx[y]));
	minn=min(minn,query_min(1,1,n,idx[x],idx[y]));
	return maxx*minn%Mod;
}
int main(){
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",&w[i]);
	rep(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs1(1,0);
	dfs2(1,1);
	//rep(i,1,n)cout<<idx[i]<<' ';cout<<endl;
	build(1,1,n);
	//cout<<query_max(1,1,n,1,2)<<endl;
	//cout<<ask(idx[1],idx[3])<<endl;
	rep(i,1,n)
	    rep(j,i,n)
	        ans+=ask(i,j),ans%=Mod;
	printf("%lld\n",ans);
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

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

input:

100
2528 1561 4852 424 4447 4434 2406 4123 77 2984 1184 2905 4549 593 3861 1028 1042 4466 509 672 62...

output:

35659331

result:

ok single line: '35659331'

Test #2:

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

input:

100
2567 4470 2614 2040 2150 1019 1988 1763 1608 4743 4409 3433 3072 2541 3023 2477 178 1193 2478 30...

output:

429450371

result:

ok single line: '429450371'

Test #3:

score: 10
Accepted
time: 2ms
memory: 1248kb

input:

100
2584 2676 3631 1284 955 653 4782 2127 862 1771 3669 3654 2694 4736 680 864 3196 15 1631 2375 307...

output:

974015017

result:

ok single line: '974015017'

Test #4:

score: 10
Accepted
time: 248ms
memory: 1272kb

input:

1000
2610 360 2705 2183 168 4299 1914 1477 1883 354 2486 4006 965 1035 1609 3497 1132 1345 3509 3964...

output:

340207682

result:

ok single line: '340207682'

Test #5:

score: 10
Accepted
time: 247ms
memory: 1272kb

input:

1000
2665 4243 1484 3043 1675 517 2057 2249 436 1909 2740 4755 1878 2410 3428 3332 3286 1893 1863 66...

output:

888632951

result:

ok single line: '888632951'

Test #6:

score: 10
Accepted
time: 251ms
memory: 1268kb

input:

1000
2678 1700 2404 993 166 3456 1739 540 946 2317 2148 4931 2129 3060 3892 2148 4486 58 4186 1462 1...

output:

553091177

result:

ok single line: '553091177'

Test #7:

score: 10
Accepted
time: 233ms
memory: 1272kb

input:

1000
2695 2674 3421 237 1203 3090 2301 904 200 4345 1408 151 1752 2487 1549 535 2504 3880 3340 3014 ...

output:

326754139

result:

ok single line: '326754139'

Test #8:

score: 10
Accepted
time: 250ms
memory: 1272kb

input:

1000
2708 132 4342 3186 4693 1029 1983 1963 711 2520 817 327 4771 3137 2014 4351 1472 2045 2895 3808...

output:

859568466

result:

ok single line: '859568466'

Test #9:

score: 10
Accepted
time: 241ms
memory: 1268kb

input:

1000
2721 357 263 1136 415 1735 4433 253 1221 696 225 504 23 3786 2478 3168 440 210 217 4603 3776 19...

output:

798473199

result:

ok single line: '798473199'

Test #10:

score: 10
Accepted
time: 241ms
memory: 1268kb

input:

1000
2737 1331 1279 380 4220 3601 4995 617 3243 2724 4486 724 2413 982 135 4322 3458 4032 4371 3923 ...

output:

988851990

result:

ok single line: '988851990'