#P10795. 『SpOI - R1』Lamborghini (Demo)

    ID: 11844 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>点分治树上启发式合并Kruskal 重构树O2优化线段树合并

『SpOI - R1』Lamborghini (Demo)

Problem Description

You are given an unrooted tree. Each node ii has two attributes ti,vit_i, v_i.

Define fi,jf_{i,j} for the directed path iji \to j as follows:

  • If the node with the smallest txt_x on the path iji \to j is xx, and vjvxviv_j \leq v_x \leq v_i, then fi,j=xf_{i,j} = x.
  • Otherwise, fi,j=0f_{i,j} = 0.

Compute i=1nj=1nfi,j\sum\limits_{i=1}^n \sum\limits_{j=1}^n f_{i,j}.

Input Format

The first line contains an integer nn.

The second line contains nn integers separated by spaces; the ii-th integer denotes tit_i of node ii.

The third line contains nn integers separated by spaces; the ii-th integer denotes viv_i of node ii.

The next n1n - 1 lines each contain two integers a,ba, b, describing an edge aba \leftrightarrow b in the tree.

Output Format

Output one line with one integer, the answer.

3
1 2 3
1 3 5
1 2
2 3
10
5
1 3 5 8 10
1 5 3 2 8
2 1
3 1
4 1
5 3
22

Hint

Sample #1 Explanation

  • f(1,1)=1f(1,1)=1.
  • f(1,2)=0f(1,2)=0.
  • f(1,3)=0f(1,3)=0.
  • f(2,1)=1f(2,1)=1.
  • f(2,2)=2f(2,2)=2.
  • f(2,3)=0f(2,3)=0.
  • f(3,1)=1f(3,1)=1.
  • f(3,2)=2f(3,2)=2.
  • f(3,3)=3f(3,3)=3.

Therefore, i=13j=13f(i,j)=10\sum\limits_{i=1}^3 \sum\limits_{j=1}^3 f(i,j)=10.

Constraints

This problem enables subtask bundling and subtask dependencies.

For 100%100\% of the data, all tt are pairwise distinct, 1n1051 \leq n \leq 10^5, and 1ti,vi1091 \leq t_i, v_i \leq 10^9.

Subtask nn\leq ti,vit_i,v_i\leq Special Property Score Dependencies
1 300300 10510^5 None 1515 None
2 50005000 1
3 10510^5 10910^9 AA None
4 BB
5 None 4040 1,2,3,4

Special property AA: Node 11 is fixed as the root of the tree. For any pair of nodes (x,y)(x,y) with xyx \neq y, if xx is an ancestor of yy, then tx<tyt_x < t_y.

Special property BB: i[1,n),ai=i,bi=i+1\forall i\in[1,n), a_i=i, b_i=i+1.

Translated by ChatGPT 5