UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196960#3441. 路径X_X1008744ms41808kbC++112.4kb2023-11-04 10:53:502023-11-04 13:03:02

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+4;
int n,m,s,MOD,c,x,y,z,a[N],h[N],idx;
int sz[N],fa[N],son[N],d[N];
int dfn[N],top[N],now,rk[N];
int Mx[N],Mn[N],Ans[N];
struct node{
	int v,ne;
}e[N<<1];
void add(int x,int y){
	e[++idx]={y,h[x]};
	h[x]=idx;
}
void dfs1(int u,int fath){
	sz[u]=1;
	fa[u]=fath;
	d[u]=d[fath]+1;
	for(int i=h[u];i;i=e[i].ne){
		int v=e[i].v;
		if(v==fath) continue;
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[son[u]]) son[u]=v;
	}
}
void dfs2(int u,int rt){
	dfn[u]=++now;
    rk[now]=u;
    top[u]=rt;
    if(son[u]) dfs2(son[u],rt);
    for(int i=h[u];i;i=e[i].ne){
    	int v=e[i].v;
    	if(v!=son[u]&&v!=fa[u]) dfs2(v,v);
	}
}
struct Node{
	int l,r,mx,mn=1e9,ans;
}tr[N<<2];
void up(int u){
	tr[u].mx=max(tr[u<<1].mx,tr[u<<1|1].mx);
	tr[u].mn=min(tr[u<<1].mn,tr[u<<1|1].mn);
	tr[u].ans=max(max(tr[u<<1].ans,tr[u<<1|1].ans),tr[u<<1].mx-tr[u<<1|1].mn);
}
void build(int u,int l,int r){
	tr[u].l=l,tr[u].r=r;
	if(l==r) tr[u].mx=tr[u].mn=a[rk[l]];
	else{
		int mid=l+r>>1;
		build(u<<1,l,mid);
		build(u<<1|1,mid+1,r);
		up(u);
	}
}
Node Q(int u,int l,int r){
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u];
	int mid=tr[u].l+tr[u].r>>1;
    Node res;
    res.mx=res.ans=0,res.mn=1e9;
	if(l<=mid) res=Q(u<<1,l,r);
	if(r>=mid+1){
		Node F=Q(u<<1|1,l,r);
		res.ans=max(max(res.ans,F.ans),res.mx-F.mn);
		res.mx=max(res.mx,F.mx);
		res.mn=min(res.mn,F.mn);
    }
	return res;
}
void dfs3(int u){
	Node F=Q(1,dfn[top[u]],dfn[u]);
	//cout<<u<<' '<<dfn[top[u]]<<' '<<dfn[u]<<endl;
	Mx[u]=F.mx,Mn[u]=F.mn,Ans[u]=F.ans;
	for(int i=h[u];i;i=e[i].ne){
		int v=e[i].v;
		if(v==fa[u]) continue;
		dfs3(v);
	}
}
int TQ(int u,int v){
	int res=0,mn=1e9;
	while(top[u]!=top[v]){
		res=max(res,max(Mx[u]-mn,Ans[u]));
		mn=min(mn,Mn[u]);
		u=fa[top[u]];
	}
	Node F=Q(1,dfn[v],dfn[u]);
	//cout<<F.mx<<' '<<F.mn<<' '<<F.ans<<endl;
	res=max(res,max(F.mx-mn,F.ans));
	return res;
}
int main()
{
	//freopen("ex_path4.in","r",stdin);
	//freopen("hh.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
    }
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    dfs1(1,0);
    dfs2(1,1);
    build(1,1,n);
	dfs3(1);
	//for(int i=1;i<=n;i++) cout<<i<<' '<<Mx[i]<<' '<<Mn[i]<<' '<<Ans[i]<<endl;
	while(m--){
    	scanf("%d%d",&x,&y);
    	printf("%d\n",TQ(x,y));
	}
	return 0;
}

详细

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

Test #1:

score: 5
Accepted
time: 4ms
memory: 16864kb

input:

9 6
1 2
1 3
2 4
4 5
1 6
5 7
4 8
6 9
3 5 3 1 3 6 10 8 6
5 2
9 1
9 1
6 6
4 4
8 8

output:

4
0
0
0
0
0

result:

ok 6 lines

Test #2:

score: 5
Accepted
time: 5ms
memory: 16864kb

input:

10 10
1 2
2 3
2 4
4 5
5 6
6 7
3 8
1 9
8 10
7 4 4 8 8 4 10 6 1 2
8 3
4 1
7 5
9 9
6 4
10 10
10 3
3 1
1...

output:

0
3
4
0
4
0
4
3
0
0

result:

ok 10 lines

Test #3:

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

input:

994 991
1 2
1 3
2 4
4 5
4 6
3 7
2 8
1 9
4 10
1 11
6 12
6 13
5 14
14 15
4 16
14 17
17 18
2 19
7 20
20...

output:

0
76
61
0
55
0
65
28
60
81
24
0
41
0
73
0
23
35
58
7
0
0
22
0
91
60
82
71
60
60
41
68
70
0
20
0
0
60...

result:

ok 991 lines

Test #4:

score: 5
Accepted
time: 5ms
memory: 16928kb

input:

997 994
1 2
1 3
3 4
2 5
2 6
4 7
1 8
5 9
1 10
5 11
7 12
4 13
13 14
5 15
5 16
11 17
7 18
9 19
18 20
3 ...

output:

936455963
0
803375903
588789236
913091021
0
0
684383890
245669194
600498485
674412258
308855627
3654...

result:

ok 994 lines

Test #5:

score: 5
Accepted
time: 4ms
memory: 16928kb

input:

995 991
1 2
2 3
3 4
2 5
5 6
6 7
7 8
7 9
8 10
5 11
4 12
9 13
6 14
5 15
15 16
8 17
4 18
2 19
15 20
18 ...

output:

72176025
48853349
70299185
88661370
87049799
15849234
72703417
93734580
62320986
48853349
61235577
4...

result:

ok 991 lines

Test #6:

score: 5
Accepted
time: 785ms
memory: 41808kb

input:

199965 500000
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
1...

output:

1
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
1
0
1
0
0
0
0
0
0
0
1
0
0
...

result:

ok 500000 lines

Test #7:

score: 5
Accepted
time: 678ms
memory: 41800kb

input:

199908 500000
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
1...

output:

1
0
0
1
0
0
1
0
1
0
1
1
0
1
0
1
0
1
1
0
0
0
0
0
0
1
0
0
1
0
0
0
0
0
0
1
0
0
1
0
1
1
1
1
0
0
1
1
0
1
...

result:

ok 500000 lines

Test #8:

score: 5
Accepted
time: 882ms
memory: 41808kb

input:

200000 500000
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
1...

output:

1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
0
0
1
0
0
1
1
0
1
1
0
0
1
0
1
1
0
0
0
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
...

result:

ok 500000 lines

Test #9:

score: 5
Accepted
time: 698ms
memory: 41804kb

input:

199904 500000
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
1...

output:

999977286
996605780
999977286
999622957
999676473
999949729
999981864
999972427
999944922
999860383
...

result:

ok 500000 lines

Test #10:

score: 5
Accepted
time: 662ms
memory: 41808kb

input:

199997 500000
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
1...

output:

999931525
999491336
999980026
999980026
999927984
999892075
999968575
999974219
999931525
999909809
...

result:

ok 500000 lines

Test #11:

score: 5
Accepted
time: 801ms
memory: 41808kb

input:

200000 500000
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
1...

output:

499961752
499996063
499992026
499955230
499914810
499992026
499992026
499991276
499991276
499991276
...

result:

ok 500000 lines

Test #12:

score: 5
Accepted
time: 783ms
memory: 41808kb

input:

200000 500000
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
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 500000 lines

Test #13:

score: 5
Accepted
time: 155ms
memory: 26228kb

input:

100000 100000
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
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 lines

Test #14:

score: 5
Accepted
time: 147ms
memory: 25304kb

input:

90000 100000
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...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 lines

Test #15:

score: 5
Accepted
time: 148ms
memory: 26232kb

input:

100000 99997
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...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 99997 lines

Test #16:

score: 5
Accepted
time: 153ms
memory: 26236kb

input:

100000 100000
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
1...

output:

980141843
721394611
962653038
980141843
980141843
980141843
721394611
900188017
944620554
0
94462055...

result:

ok 100000 lines

Test #17:

score: 5
Accepted
time: 628ms
memory: 35556kb

input:

200000 500000
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
1...

output:

999932770
999932770
999932770
999491751
999932770
999932770
999929872
999751134
999929872
999932770
...

result:

ok 500000 lines

Test #18:

score: 5
Accepted
time: 785ms
memory: 35556kb

input:

200000 500000
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
1...

output:

999839397
999912566
999701804
998588309
999681031
999912566
998235075
999912566
999864609
999864609
...

result:

ok 500000 lines

Test #19:

score: 5
Accepted
time: 643ms
memory: 35556kb

input:

200000 500000
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
1...

output:

999944553
999921999
999921999
999944553
996578852
999944553
999944553
999793801
999819961
999924992
...

result:

ok 500000 lines

Test #20:

score: 5
Accepted
time: 778ms
memory: 35560kb

input:

200000 500000
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
1...

output:

999934844
999937704
999934844
999929931
999839169
999934844
999934844
999934844
999929931
999993948
...

result:

ok 500000 lines

Extra Test:

score: 0
Extra Test Passed