#P16844. [GKS 2021 #B] Truck Delivery

    ID: 17088 Type: RemoteJudge 5000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>线段树2021树链剖分可持久化线段树Google Kick Start

[GKS 2021 #B] Truck Delivery

题目描述

Charles is a truck driver in the city of Googleland. Googleland is built in the form of a tree with NN nodes, where each node represents a city and each edge represents a road between 22 cities. The cities are numbered 11 to NN. The capital of Googleland is city 11. Each day Charles picks up a load of weight WW in city CC and wants to deliver it to city 11 using the simple path (which is unique) between the cities. Each road ii has a toll which charges amount AiA_i if the weight of the load is greater than or equal to a load-limit LiL_i.

Charles works for QQ days, where for each day Charles will be given the starting city CC and weight of the load WW. For each day, find the greatest common divisor of all the toll charges that Charles pays for that day. If Charles did not have to pay in any of the tolls, the answer is 00.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains the 22 integers NN and QQ.

The next N−1N - 1 lines describe the roads. The ii-th of these lines contains the 44 space-separated integers XX, YY, LiL_i and AiA_i, indicating a road between cities XX and YY with load-limit LiL_i and toll charge AiA_i.

The next QQ lines describe the queries. The jj-th of these lines contains the 22 space-separated integers CjC_j and WjW_j representing the starting city and weight of the load on the jj-th day.

输出格式

For each test case, output 11 line containing Case #xx: followed by yy, where xx is the test case number (starting from 11) and yy is a list of the answers for QQ days in order, separated by spaces.

2
7 5
2 1 2 4
2 3 7 8
3 4 6 2
5 3 9 9
2 6 1 5
7 1 5 7
5 10
5 8
4 1
6 1
7 6
3 2
1 2 2 10
3 2 3 5
3 2
3 3
Case #1: 1 4 0 5 7
Case #2: 10 5

提示

In Sample Case #11

On the first day, Charles should pay toll charges in the roads between cities (5,3)(5, 3), (3,2)(3, 2) and (2,1)(2, 1). The answer will be gcd⁡(9,8,4)=1\gcd(9, 8, 4) = 1.

On the second day, Charles should pay toll charges in the roads between cities (3,2)(3, 2) and (2,1)(2, 1). The answer will be gcd⁡(8,4)=4\gcd(8, 4) = 4.

On the third day, Charles need not pay toll charges in any of the cities. Thus, the answer will be 00.

In Sample Case #22

On the first day, Charles should pay toll charges in the roads between cities (2,1)(2, 1). The answer will be 1010.

On the second day, Charles should pay toll charges in the roads between cities (3,2)(3, 2) and (2,1)(2, 1). The answer will be gcd⁡(5,10)=5\gcd(5, 10) = 5.

Limits

1≤T≤1001 \le T \le 100.

1≤Li≤2×1051 \le L_i \le 2 \times 10^5, for all ii.

1≤Ai≤10181 \le A_i \le 10^{18}, for all ii.

All LiL_i are distinct.

2≤Cj≤N2 \le C_j \le N, for all jj.

1≤Wj≤2×1051 \le W_j \le 2 \times 10^5, for all jj.

It is guaranteed that given roads form a tree.

Test Set 11

2≤N≤10002 \le N \le 1000.

1≤Q≤10001 \le Q \le 1000.

Test Set 22

2≤N≤5×1042 \le N \le 5 \times 10^4 and 1≤Q≤1051 \le Q \le 10^5 for at most 2020 test cases.

For the remaining cases, 2≤N≤10002 \le N \le 1000 and 1≤Q≤10001 \le Q \le 1000.