#P1600. [NOIP 2016 提高组] 天天爱跑步

    ID: 588 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2016线段树NOIp 提高组最近公共祖先,LCA树链剖分,树剖Link-Cut Tree,LCT差分

[NOIP 2016 提高组] 天天爱跑步

Description

Student Xiao C thinks running is very interesting, so he decides to make a game called "Love Running Everyday." "Love Running Everyday" is a nurturing-style game that requires players to log in on time every day to complete a check-in task.

The map in this game can be viewed as a tree with nn nodes and n1n-1 edges. Each edge connects two nodes, and there exists exactly one path between any two nodes. The nodes on the tree are numbered with consecutive positive integers from 11 to nn.

There are mm players. The ii-th player starts at sis_i and ends at tit_i. When the daily check-in task starts, all players simultaneously depart from their starting points at second 00, moving at a speed of one edge per second, continuously along the shortest path toward their destination. Once a player reaches the destination, that player is considered to have completed the check-in task. (Since the map is a tree, each person's path is unique.)

Xiao C wants to know the game's activity level, so an observer is placed at every node. The observer at node jj chooses to observe players at second wjw_j. A player can be observed by this observer if and only if the player also arrives at node jj exactly at second wjw_j. Xiao C wants to know how many players each observer will observe.

Note: We assume that once a player reaches the destination, the player finishes the game and cannot wait to be observed later. That is, for a player whose destination is node jj: if the player reaches the destination before second wjw_j, then the observer at node jj cannot observe that player; if the player arrives at the destination exactly at second wjw_j, then the observer at node jj can observe that player.

Input Format

The first line contains two integers nn and mm. Here nn is the number of nodes in the tree and also the number of observers, and mm is the number of players.

Each of the next n1n-1 lines contains two integers uu and vv, indicating that there is an edge between node uu and node vv.

The next line contains nn integers, where the jj-th integer is wjw_j, meaning the observation time at node jj.

Each of the next mm lines contains two integers sis_i and tit_i, representing a player's starting point and destination.

For all testdata, it is guaranteed that 1si,tin1 \leq s_i, t_i \leq n, 0wjn0 \leq w_j \leq n.

Output Format

Output one line with nn integers. The jj-th integer indicates how many players can be observed by the observer at node jj.

6 3
2 3
1 2 
1 4 
4 5 
4 6 
0 2 5 1 2 3 
1 5 
1 3 
2 6 
2 0 0 1 1 1 
5 3 
1 2 
2 3 
2 4 
1 5 
0 1 0 3 0 
3 1 
1 4
5 5 
1 2 1 0 1 

Hint

Sample 1 Explanation.

  • For node 11, w1=0w_1 = 0. Therefore, only players whose starting point is node 11 will be observed. Players 11 and 22 are observed, for a total of 22 players.
  • For node 22, no player is at this node at second 22, so 00 players are observed.
  • For node 33, no player is at this node at second 55, so 00 players are observed.
  • For node 44, player 11 is observed, so 11 player is observed.
  • For node 55, player 11 is observed, so 11 player is observed.
  • For node 66, player 33 is observed, so 11 player is observed.

Subtasks.

The size and characteristics of each testpoint are as follows.

Tip: The one's digit of the numbers in the constraints can help determine which type of data it is.

Testpoint ID n=n= m=m= Convention
121 \sim 2 991991 All players' starting points equal their own destinations, i.e., i, si=ti\forall i,\ s_i=t_i.
343 \sim 4 992992 All wj=0w_j=0.
55 993993 None.
686 \sim 8 9999499994 i[1,n1]\forall i \in [1,n-1], there is an edge between ii and i+1i+1. That is, the tree degenerates into a chain 1,2,,n1,2,\dots,n connected in order.
9129 \sim 12 9999599995 All si=1s_i=1.
131613 \sim 16 9999699996 All ti=1t_i=1.
171917 \sim 19 9999799997 None.
2020 299998299998

Notes.

(Tip: Because the original note is old, it may not fully reflect the current situation. We have made some updates to this note. The original text can be viewed on this clipboard.)

In the final evaluation, the stack size usage will not have a separate limit, but in our working environment there is a default limit of 1MiB1 \text{MiB}. This may cause a stack overflow crash when the number of function calls is large. Deep recursion in your program often leads to this problem. If your program needs a large stack, please pay close attention to this issue.

We can use some methods to change the stack size limit.

  • Linux

We can enter the following command in the terminal: ulimit -s 1048576. This command changes the stack size limit to 1048576KiB=1GiB1048576\text{KiB}=1 \text{GiB}.

For example, for the following program sample.cpp:

#include <bits/stdc++.h>
using namespace std;
int f[1000005];
void dfs(int a){
	if(a == 0){
		f[a] = 0;
		return;
	}
	dfs(a - 1);
	f[a] = f[a - 1] + 1;
}
int main(){
	dfs(1000000);
	return 0;
}

Compile the above source code with g++ sample.cpp -o sample into the executable sample, and run it using ./sample.

If you run this program without using the command ulimit -s 1048576, sample will crash due to stack overflow; if you run it after using the above command, the program will not crash.

In particular, when you open multiple terminals, they do not share this setting. You need to run the command in each of them.

Please note that the stack usage is counted toward the total memory usage and is subject to the memory limit together with other parts of the program.

  • Windows

If you are using Dev-C++ on Windows, select Tools - Compiler Options and fill in the following command in the area as shown: -Wl,--stack=1073741824. After filling it in, make sure the box “Add the following commands when compiling” is checked.

Here, 1073741824 is in units of B/Bytes\text{B/Bytes}.

Translated by ChatGPT 5