Codeforces Round 969 (Div. 2 ABCDE题) 视频讲解

A. Dora’s Set

Problem Statement

Dora has a set s s s containing integers. In the beginning, she will put all integers in [ l , r ] [l, r] [l,r] into the set s s s. That is, an integer x x x is initially contained in the set if and only if l ≤ x ≤ r l \leq x \leq r lxr. Then she allows you to perform the following operations:

  • Select three distinct integers a a a, b b b, and c c c from the set s s s, such that gcd ⁡ ( a , b ) = gcd ⁡ ( b , c ) = gcd ⁡ ( a , c ) = 1 † \gcd(a, b) = \gcd(b, c) = \gcd(a, c) = 1^\dagger gcd(a,b)=gcd(b,c)=gcd(a,c)=1.
  • Then, remove these three integers from the set s s s.

What is the maximum number of operations you can perform?

† ^\dagger Recall that gcd ⁡ ( x , y ) \gcd(x, y) gcd(x,y) means the greatest common divisor of integers x x x and y y y.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \leq t \leq 500 1t500) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers l l l and r r r ( 1 ≤ l ≤ r ≤ 1000 1 \leq l \leq r \leq 1000 1lr1000) — the range of integers in the initial set.

Output

For each test case, output a single integer — the maximum number of operations you can perform.

Example

input
8
1 3
3 7
10 21
2 8
51 60
2 15
10 26
1 1000
output
1
1
3
1
2
3
4
250

Note

In the first test case, you can choose a = 1 a = 1 a=1, b = 2 b = 2 b=2, c = 3 c = 3 c=3 in the only operation, since gcd ⁡ ( 1 , 2 ) = gcd ⁡ ( 2 , 3 ) = gcd ⁡ ( 1 , 3 ) = 1 \gcd(1, 2) = \gcd(2, 3) = \gcd(1, 3) = 1 gcd(1,2)=gcd(2,3)=gcd(1,3)=1, and then there are no more integers in the set, so no more operations can be performed.

In the second test case, you can choose a = 3 a = 3 a=3, b = 5 b = 5 b=5, c = 7 c = 7 c=7 in the only operation.

In the third test case, you can choose a = 11 a = 11 a=11, b = 19 b = 19 b=19, c = 20 c = 20 c=20 in the first operation, a = 13 a = 13 a=13, b = 14 b = 14 b=14, c = 15 c = 15 c=15 in the second operation, and a = 10 a = 10 a=10, b = 17 b = 17 b=17, c = 21 c = 21 c=21 in the third operation. After the three operations, the set s s s contains the following integers: 12 12 12, 16 16 16, 18 18 18. It can be proven that it’s impossible to perform more than 3 3 3 operations.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int l, r;cin >> l >> r;int res = (r - l + 1) / 4;if ((r - l + 1) % 4 == 3 && ((r - 2) & 1)) res ++;cout << res << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

B. Index and Maximum Value

Problem Statement

After receiving yet another integer array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an at her birthday party, Index decides to perform some operations on it.

Formally, there are m m m operations that she is going to perform in order. Each of them belongs to one of the two types:

  • + l r \texttt{+ l r} + l r. Given two integers l l l and r r r, for all 1 ≤ i ≤ n 1 \leq i \leq n 1in such that l ≤ a i ≤ r l \leq a_i \leq r lair, set a i : = a i + 1 a_i := a_i + 1 ai:=ai+1.
  • - l r \texttt{- l r} - l r. Given two integers l l l and r r r, for all 1 ≤ i ≤ n 1 \leq i \leq n 1in such that l ≤ a i ≤ r l \leq a_i \leq r lair, set a i : = a i − 1 a_i := a_i - 1 ai:=ai1.

For example, if the initial array a = [ 7 , 1 , 3 , 4 , 3 ] a = [7, 1, 3, 4, 3] a=[7,1,3,4,3], after performing the operation + 2 4 \texttt{+} \space 2 \space 4 + 2 4, the array a = [ 7 , 1 , 4 , 5 , 4 ] a = [7, 1, 4, 5, 4] a=[7,1,4,5,4]. Then, after performing the operation - 1 10 \texttt{-} \space 1 \space 10 - 1 10, the array a = [ 6 , 0 , 3 , 4 , 3 ] a = [6, 0, 3, 4, 3] a=[6,0,3,4,3].

Index is curious about the maximum value in the array a a a. Please help her find it after each of the m m m operations.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105, 1 ≤ m ≤ 1 0 5 1 \leq m \leq 10^5 1m105) — the length of the array and the number of operations.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the initial array a a a.

Then m m m lines follow, each line corresponds to the operation, in the following format: c l r \texttt{c l r} c l r ( c ∈ { + , - } c \in \{\texttt +, \texttt -\} c{+,-}, l l l and r r r are integers, 1 ≤ l ≤ r ≤ 1 0 9 1 \leq l \leq r \leq 10^9 1lr109) — the description of the operation.

Note that the elements a i a_i ai may not satisfy 1 ≤ a i ≤ 1 0 9 1\le a_i\le 10^9 1ai109 after some operations, as it is shown in the example.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105, and the sum of m m m over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case, output one single line containing m m m integers, with the i i i-th of them describing the maximum value of the array after the i i i-th operation.

Example

input
5
5 5
1 2 3 2 1
+ 1 3
- 2 3
+ 1 2
+ 2 4
- 6 8
5 5
1 3 3 4 5
+ 1 4
+ 2 3
- 4 5
- 3 3
- 2 6
5 5
1 1 1 1 1
+ 2 3
- 4 5
+ 1 6
- 2 5
+ 1 8
1 1
1
- 1 1
1 1
1000000000
+ 1000000000 1000000000
output
4 4 4 5 5
5 5 4 4 3
1 1 2 1 2
0
1000000001

Note

In the first test case, the process of the operations is listed below:

  • After the first operation, the array becomes equal [ 2 , 3 , 4 , 3 , 2 ] [2,3,4,3,2] [2,3,4,3,2]. The maximum value is 4 4 4.
  • After the second operation, the array becomes equal [ 1 , 2 , 4 , 2 , 1 ] [1,2,4,2,1] [1,2,4,2,1]. The maximum value is 4 4 4.
  • After the third operation, the array becomes equal [ 2 , 3 , 4 , 3 , 2 ] [2,3,4,3,2] [2,3,4,3,2]. The maximum value is 4 4 4.
  • After the fourth operation, the array becomes equal [ 3 , 4 , 5 , 4 , 3 ] [3,4,5,4,3] [3,4,5,4,3]. The maximum value is 5 5 5.
  • After the fifth operation, the array becomes equal [ 3 , 4 , 5 , 4 , 3 ] [3,4,5,4,3] [3,4,5,4,3]. The maximum value is 5 5 5.

In the second test case, the process of the operations is listed below:

  • After the first operation, the array becomes equal [ 2 , 4 , 4 , 5 , 5 ] [2,4,4,5,5] [2,4,4,5,5]. The maximum value is 5 5 5.
  • After the second operation, the array becomes equal [ 3 , 4 , 4 , 5 , 5 ] [3,4,4,5,5] [3,4,4,5,5]. The maximum value is 5 5 5.
  • After the third operation, the array becomes equal [ 3 , 3 , 3 , 4 , 4 ] [3,3,3,4,4] [3,3,3,4,4]. The maximum value is 4 4 4.
  • After the fourth operation, the array becomes equal [ 2 , 2 , 2 , 4 , 4 ] [2,2,2,4,4] [2,2,2,4,4]. The maximum value is 4 4 4.
  • After the fifth operation, the array becomes equal [ 1 , 1 , 1 , 3 , 3 ] [1,1,1,3,3] [1,1,1,3,3]. The maximum value is 3 3 3.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int n, m;cin >> n >> m;std::vector<int> a(n);int mx = 0;for (int i = 0; i < n; i ++)cin >> a[i], mx = max(mx, a[i]);while (m -- ) {char op;int l, r;cin >> op >> l >> r;if (op == '+') {if (mx >= l && mx <= r) mx ++;} else {if (mx >= l && mx <= r) mx --;}cout << mx << " ";}cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

C. Dora and C++

Problem Statement

Dora has just learned the programming language C++!

However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array c c c with n n n elements. Dora has two integers a a a and b b b. In one operation, she can choose one of the following things to do.

  • Choose an integer i i i such that 1 ≤ i ≤ n 1 \leq i \leq n 1in, and increase c i c_i ci by a a a.
  • Choose an integer i i i such that 1 ≤ i ≤ n 1 \leq i \leq n 1in, and increase c i c_i ci by b b b.

Note that a a a and b b b are constants, and they can be the same.

Let’s define a range of array d d d as max ⁡ ( d i ) − min ⁡ ( d i ) \max(d_i) - \min(d_i) max(di)min(di). For instance, the range of the array [ 1 , 2 , 3 , 4 ] [1, 2, 3, 4] [1,2,3,4] is 4 − 1 = 3 4 - 1 = 3 41=3, the range of the array [ 5 , 2 , 8 , 2 , 2 , 1 ] [5, 2, 8, 2, 2, 1] [5,2,8,2,2,1] is 8 − 1 = 7 8 - 1 = 7 81=7, and the range of the array [ 3 , 3 , 3 ] [3, 3, 3] [3,3,3] is 3 − 3 = 0 3 - 3 = 0 33=0.

After any number of operations (possibly, 0 0 0), Dora calculates the range of the new array. You need to help Dora minimize this value, but since Dora loves exploring all by herself, you only need to tell her the minimized value.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases. The description of test cases follows.

The first line of each test case contains three integers n n n, a a a, and b b b ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105, 1 ≤ a , b ≤ 1 0 9 1 \leq a, b \leq 10^9 1a,b109) — the length of the array c c c and the constant values, respectively.

The second line of each test case contains n n n integers c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,,cn ( 1 ≤ c i ≤ 1 0 9 1 \leq c_i \leq 10^9 1ci109) — the initial elements of the array c c c.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case, output a single integer — the minimum possible range of the array after any number of operations.

Example

input
10
4 5 5
1 3 4 4
4 2 3
1 3 4 6
4 7 7
1 1 2 6
3 15 9
1 9 5
3 18 12
1 4 5
7 27 36
33 13 23 12 35 24 41
10 6 9
15 5 6 9 8 2 12 15 3 8
2 1 1000000000
1 1000000000
6 336718728 709848696
552806726 474775724 15129785 371139304 178408298 13106071
6 335734893 671469786
138885253 70095920 456876775 9345665 214704906 375508929
output
3
0
3
2
3
5
1
0
17
205359241

Note

In the first test case, we can increase c 1 = 1 c_1 = 1 c1=1 by a = 5 a = 5 a=5. The array c c c will become [ 6 , 3 , 4 , 4 ] [6, 3, 4, 4] [6,3,4,4], and the range is 3 3 3. Note that there is more than one way to reach the answer.

In the second test case, we can increase c 1 = 1 c_1 = 1 c1=1 by a = 2 a = 2 a=2 and then increase c 1 = 3 c_1 = 3 c1=3 by b = 3 b = 3 b=3. Also, we can increase c 2 = 3 c_2 = 3 c2=3 by b = 3 b = 3 b=3 and increase c 3 = 4 c_3 = 4 c3=4 by a = 2 a = 2 a=2. The array c c c will become [ 6 , 6 , 6 , 6 ] [6, 6, 6, 6] [6,6,6,6], and the range is 0 0 0.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve() {int n, m;cin >> n >> m;std::vector<PII> a(n);for (int i = 0; i < n; i ++) cin >> a[i].fi;for (int i = 0; i < n; i ++) cin >> a[i].se;sort(a.begin(), a.end());int res = min(m / a[n - 1].fi, a[n - 1].se) * a[n - 1].fi;for (int i = 0; i < n - 1; i ++) {if (a[i + 1].fi > a[i].fi + 1) { res = max(res, min(m / a[i].fi, a[i].se) * a[i].fi); continue;}int c1 = min(a[i].se, m / a[i].fi), c2 = min(a[i + 1].se, (m - c1 * a[i].fi) / a[i + 1].fi);int add = min({a[i + 1].se - c2, c1, m - c1 * a[i].fi - c2 * a[i + 1].fi});res = max(res, c1 * a[i].fi + c2 * a[i + 1].fi + add);}cout << res << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

D. Iris and Game on the Tree

Problem Statement

Iris has a tree rooted at vertex 1 1 1. Each vertex has a value of 0 \mathtt 0 0 or 1 \mathtt 1 1.

Let’s consider a leaf of the tree (the vertex 1 1 1 is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of 10 \mathtt{10} 10 and 01 \mathtt{01} 01 substrings in it.

Take the following tree as an example. Green vertices have a value of 1 \mathtt 1 1 while white vertices have a value of 0 \mathtt 0 0.
在这里插入图片描述

  • Let’s calculate the weight of the leaf 5 5 5: the formed string is 10110 \mathtt{10110} 10110. The number of occurrences of substring 10 \mathtt{10} 10 is 2 2 2, the number of occurrences of substring 01 \mathtt{01} 01 is 1 1 1, so the difference is 2 − 1 = 1 2 - 1 = 1 21=1.
  • Let’s calculate the weight of the leaf 6 6 6: the formed string is 101 \mathtt{101} 101. The number of occurrences of substring 10 \mathtt{10} 10 is 1 1 1, the number of occurrences of substring 01 \mathtt{01} 01 is 1 1 1, so the difference is 1 − 1 = 0 1 - 1 = 0 11=0.

The score of a tree is defined as the number of leaves with non-zero weight in the tree.

But the values of some vertices haven’t been decided and will be given to you as ? \texttt{?} ?. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value ? \texttt{?} ? and changes its value to 0 \mathtt{0} 0 or 1 \mathtt{1} 1, with Iris going first. The game continues until there are no vertices with value ? \mathtt{?} ? left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.

Assuming that both girls play optimally, please determine the final score of the tree.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5\cdot 10^4 1t5104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \leq n \leq 10^5 2n105) — the number of vertices in the tree.

The following n − 1 n - 1 n1 lines each contain two integers u u u and v v v ( 1 ≤ u , v ≤ n 1 \leq u, v \leq n 1u,vn) — denoting an edge between vertices u u u and v v v.

It’s guaranteed that the given edges form a tree.

The last line contains a string s s s of length n n n. The i i i-th character of s s s represents the value of vertex i i i. It’s guaranteed that s s s only contains characters 0 \mathtt{0} 0, 1 \mathtt{1} 1 and ? \mathtt{?} ?.

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test case, output a single integer — the final score of the tree.

Example

input
6
4
1 2
1 3
4 1
0101
4
1 2
3 2
2 4
???0
5
1 2
1 3
2 4
2 5
?1?01
6
1 2
2 3
3 4
5 3
3 6
?0???
5
1 2
1 3
1 4
1 5
11?1?
2
2 1
??
output
2
1
1
2
1
0

Note

In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf:

  • From vertex 1 1 1 to vertex 2 2 2. The string formed by the path is 01 \mathtt{01} 01, so the weight of the leaf is 0 − 1 = − 1 0-1=-1 01=1.
  • From vertex 1 1 1 to vertex 3 3 3. The string formed by the path is 00 \mathtt{00} 00, so the weight of the leaf is 0 − 0 = 0 0-0=0 00=0.
  • From vertex 1 1 1 to vertex 4 4 4. The string formed by the path is 01 \mathtt{01} 01, so the weight of the leaf is 0 − 1 = − 1 0-1=-1 01=1.

Thus, there are two leaves with non-zero weight, so the score of the tree is 2 2 2.

In the second test case, one of the sequences of optimal choices for the two players can be:

  • Iris chooses to change the value of the vertex 3 3 3 to 1 \mathtt 1 1.
  • Dora chooses to change the value of the vertex 1 1 1 to 0 \mathtt 0 0.
  • Iris chooses to change the value of the vertex 2 2 2 to 0 \mathtt 0 0.

The final tree is as follows:
在这里插入图片描述

The only leaf with non-zero weight is 3 3 3, so the score of the tree is 1 1 1. Note that this may not be the only sequence of optimal choices for Iris and Dora.

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const long double eps = 1e-8;void solve() {int n;cin >> n;std::vector<int> a(n + 1, 0);for (int i = 1; i <= n; i ++) cin >> a[i];int res = 0, lst = 0;for (int i = 2; i <= n; i ++) {if (a[i - 1] == 1) continue;int j = floor(log2((long double)log(a[i]) * 1.0 / log(a[i - 1])));if (j >= lst) { lst = 0; continue; }if (a[i] == 1) {cout << -1 << endl;return;}int k = ceil((long double)lst * 1.0 - log2(log(a[i]) * 1.0 / log(a[i - 1])));res += k, lst = k;}cout << res << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

E. Iris and the Tree

Problem Statement

Given a rooted tree with the root at vertex 1 1 1. For any vertex i i i ( 1 < i ≤ n 1 < i \leq n 1<in) in the tree, there is an edge connecting vertices i i i and p i p_i pi ( 1 ≤ p i < i 1 \leq p_i < i 1pi<i), with a weight equal to t i t_i ti.

Iris does not know the values of t i t_i ti, but she knows that ∑ i = 2 n t i = w \displaystyle\sum_{i=2}^n t_i = w i=2nti=w and each of the t i t_i ti is a non-negative integer.

The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.
在这里插入图片描述

The tree in this picture satisfies the condition. For example, in the subtree of vertex 2 2 2, the vertex numbers are 2 , 3 , 4 , 5 2, 3, 4, 5 2,3,4,5, which are consecutive integers.
在这里插入图片描述

The tree in this picture does not satisfy the condition, as in the subtree of vertex 2 2 2, the vertex numbers 2 2 2 and 4 4 4 are not consecutive integers.

We define dist ⁡ ( u , v ) \operatorname{dist}(u, v) dist(u,v) as the length of the simple path between vertices u u u and v v v in the tree.

Next, there will be n − 1 n - 1 n1 events:

  • Iris is given integers x x x and y y y, indicating that t x = y t_x = y tx=y.

After each event, Iris wants to know the maximum possible value of dist ⁡ ( i , i m o d n + 1 ) \operatorname{dist}(i, i \bmod n + 1) dist(i,imodn+1) independently for each i i i ( 1 ≤ i ≤ n 1\le i\le n 1in). She only needs to know the sum of these n n n values. Please help Iris quickly get the answers.

Note that when calculating the maximum possible values of dist ⁡ ( i , i m o d n + 1 ) \operatorname{dist}(i, i \bmod n + 1) dist(i,imodn+1) and dist ⁡ ( j , j m o d n + 1 ) \operatorname{dist}(j, j \bmod n + 1) dist(j,jmodn+1) for i ≠ j i \ne j i=j, the unknown edge weights may be different.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and w w w ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105, 0 ≤ w ≤ 1 0 12 0 \leq w \leq 10^{12} 0w1012) — the number of vertices in the tree and the sum of the edge weights.

The second line of each test case contains n − 1 n - 1 n1 integers p 2 , p 3 , … , p n p_2, p_3, \ldots, p_n p2,p3,,pn ( 1 ≤ p i < i 1 \leq p_i < i 1pi<i) — the description of the edges of the tree.

Then follow n − 1 n-1 n1 lines indicating the events. Each line contains two integers x x x and y y y ( 2 ≤ x ≤ n 2 \leq x \leq n 2xn, 0 ≤ y ≤ w 0 \leq y \leq w 0yw), indicating that t x = y t_x = y tx=y.

It is guaranteed that all x x x in the events are distinct. It is also guaranteed that the sum of all y y y equals w w w.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output one line containing n − 1 n-1 n1 integers, each representing the answer after each event.

Example

input
4
2 1000000000000
1
2 1000000000000
4 9
1 1 1
2 2
4 4
3 3
6 100
1 2 3 2 1
6 17
3 32
2 4
4 26
5 21
10 511
1 2 2 4 2 1 1 8 8
3 2
6 16
10 256
9 128
2 1
5 8
8 64
4 4
7 32
output
2000000000000
25 18 18
449 302 247 200 200
4585 4473 2681 1567 1454 1322 1094 1022 1022

Note

In the first test case, dist ⁡ ( 1 , 2 ) = dist ⁡ ( 2 , 1 ) = t 2 = w = 1 0 12 \operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12} dist(1,2)=dist(2,1)=t2=w=1012, so dist ⁡ ( 1 , 2 ) + dist ⁡ ( 2 , 1 ) = 2 ⋅ 1 0 12 \operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12} dist(1,2)+dist(2,1)=21012.

In the second test case, the tree after Iris found out all t x t_x tx is shown below:
在这里插入图片描述

dist ⁡ ( 1 , 2 ) = t 2 \operatorname{dist}(1, 2) = t_2 dist(1,2)=t2, dist ⁡ ( 2 , 3 ) = t 2 + t 3 \operatorname{dist}(2, 3) = t_2 + t_3 dist(2,3)=t2+t3, dist ⁡ ( 3 , 4 ) = t 3 + t 4 \operatorname{dist}(3, 4) = t_3 + t_4 dist(3,4)=t3+t4, dist ⁡ ( 4 , 1 ) = t 4 \operatorname{dist}(4, 1) = t_4 dist(4,1)=t4. After the first event, she found out that t 2 = 2 t_2 = 2 t2=2, so dist ⁡ ( 1 , 2 ) = 2 \operatorname{dist}(1, 2) = 2 dist(1,2)=2. At the same time:

  • dist ⁡ ( 2 , 3 ) \operatorname{dist}(2, 3) dist(2,3) is maximized if t 3 = 7 t_3 = 7 t3=7, t 4 = 0 t_4 = 0 t4=0. Then dist ⁡ ( 2 , 3 ) = 9 \operatorname{dist}(2, 3) = 9 dist(2,3)=9.
  • dist ⁡ ( 3 , 4 ) \operatorname{dist}(3, 4) dist(3,4) and dist ⁡ ( 4 , 1 ) \operatorname{dist}(4, 1) dist(4,1) are maximized if t 3 = 0 t_3 = 0 t3=0, t 4 = 7 t_4 = 7 t4=7. Then dist ⁡ ( 3 , 4 ) = dist ⁡ ( 4 , 1 ) = 7 \operatorname{dist}(3, 4) = \operatorname{dist}(4, 1) = 7 dist(3,4)=dist(4,1)=7.

Thus, the answer is 2 + 9 + 7 + 7 = 25 2 + 9 + 7 + 7 = 25 2+9+7+7=25.

After the second event, she found out that t 4 = 4 t_4 = 4 t4=4, then t 3 = w − t 2 − t 4 = 4 t_3 = w - t_2 - t_4 = 4 t3=wt2t4=4. dist ⁡ ( 1 , 2 ) = 2 \operatorname{dist}(1, 2) = 2 dist(1,2)=2, dist ⁡ ( 2 , 3 ) = 2 + 3 = 5 \operatorname{dist}(2, 3) = 2 + 3 = 5 dist(2,3)=2+3=5, dist ⁡ ( 3 , 4 ) = 3 + 4 = 7 \operatorname{dist}(3, 4) = 3 + 4 = 7 dist(3,4)=3+4=7, dist ⁡ ( 4 , 1 ) = 4 \operatorname{dist}(4, 1) = 4 dist(4,1)=4. Thus, the answer is 2 + 5 + 7 + 4 = 18 2 + 5 + 7 + 4 = 18 2+5+7+4=18.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 2e5 + 10;int n, w;
std::vector<int> g[N];
int idx[N], cnt[N], dep[N], tot[N], fa[N][25];void dfs(int u) {idx[u] = u;for (int i = 1; i < 25; i ++) fa[u][i] = fa[fa[u][i - 1]][i - 1];for (auto v : g[u]) dep[v] = dep[u] + 1, dfs(v), idx[u] = max(idx[u], idx[v]);
}
int lca(int u, int v) {if (dep[u] < dep[v]) swap(u, v);for (int i = 24; i >= 0; i --)if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];if (u == v) return u;for (int i = 24; i >= 0; i --)if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];return fa[u][0];
}void solve() {cin >> n >> w;for (int i = 1; i <= n; i ++) g[i].clear(), idx[i] = cnt[i] = dep[i] = tot[i] = 0;for (int i = 2; i <= n; i ++) {cin >> fa[i][0];g[fa[i][0]].push_back(i);}dfs(1);for (int i = 1; i <= n; i ++)cnt[i] = dep[i] + dep[i % n + 1] - 2 * dep[lca(i, i % n + 1)];int res = w * n, sum = 0, st = 0;for (int i = 1; i < n; i ++) {int x, y;cin >> x >> y, sum += y;res -= (n - 2 - st) * y;int lst = (x + n - 1) % n;if (!lst) lst = n;// cout << idx[x] << endl;cnt[lst] --, cnt[idx[x]] --;tot[lst] += y, tot[idx[x]] += y;if (!cnt[lst]) res += (tot[lst] - (w - (sum - tot[lst]))), st ++;if (!cnt[idx[x]]) res += (tot[idx[x]] - (w - (sum - tot[idx[x]]))), st ++;//, cerr << tot[idx[x]] << endl;cout << res << " ";}cout << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

视频讲解

Codeforces Round 969 (Div. 2)(A ~ E 题讲解)


最后祝大家早日在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1522277.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

std::future和std::promise详解(原理、应用、源码)

在编程实践中&#xff0c;我们常常需要使用异步调用。通过异步调用&#xff0c;我们可以将一些耗时、阻塞的任务交给其他线程来执行&#xff0c;从而保证当前线程的快速响应能力。还有一些场景可以通过将一个任务&#xff0c;分成多个部分然后将这部分交给多个线程来进行并发执…

通过FFmpeg和URL查看流的编码格式

FFmpeg下载后会有三个执行文件&#xff0c;跳转到FFmpeg所在文件夹 查看视频流URL地址的编码格式命令&#xff1a; // 在下载ffmpeg的文件夹中执行如下命令&#xff0c;可查看流的编码格式&#xff0c;这里的测试流是H264编码ffprobe http://devimages.apple.com/iphone/sample…

华为打造“新能源航母”,能否挑翻BBA?

华为在汽车行业的动作&#xff0c;频繁让它聚焦在市场镁光灯之下。 前有享界S9陷入飞坡争议&#xff0c;后有智界R7即将登台&#xff0c;后面还有尊界等待亮相&#xff0c;一波又一波的操作令人眼花缭乱。在新能源浪潮之下&#xff0c;BBA的日子并不好过&#xff0c;华为及其他…

VMware Fusion 13.6 发布下载,新增功能概览

VMware Fusion 13.6 发布下载&#xff0c;新增功能概览 VMware Fusion 13.6 for Mac - 领先的免费桌面虚拟化软件 适用于基于 Intel 处理器和搭载 Apple 芯片的 Mac 的桌面虚拟化软件 请访问原文链接&#xff1a;https://sysin.org/blog/vmware-fusion-13/&#xff0c;查看最…

JDBC |封装JDBCUtils|PreparedStatement|事务|批处理|数据库连接池| Blob类型数据的读写|Apache—DBUtils简介

一.概述 在Java中&#xff0c;数据库存取技术可分为如下几类&#xff1a; JDBC直接访问数据库JDO技术&#xff08;Java Data Object&#xff09;第三方O/R工具&#xff0c;如Hibernate, Mybatis 等 JDBC是java访问数据库的基石&#xff0c;JDO, Hibernate等只是更好的封装了J…

单片机-初识单片机(keil安装以及编写简单驱动)(一)

目录 一、嵌入式介绍 1.嵌入式系统&#xff1a; 2.嵌入式操作系统 3.单片机&#xff1a; 二、STM32F103ZET6简介 1.单片机的组成&#xff1a; 2.单片机外观&#xff1a; 3.ARM公司 4.ST公司--意法半导体 三、资料部分 1.安装工具&#xff1a; 2.破解软件&#xff1…

哲学概述1(马克)

哲学题目大原则&#xff1a; 一、哲学与世界观 世界观&#xff1a;人们对于生活于其中的世界以及与世界关系的根本观点、根本看法方法论&#xff1a;是人们认识世界、改造世界的一般方法&#xff0c;是人们用啥样的方式、方法来观察事物和处理问题哲学&#xff1a;是理论化、…

最新车型库大全|阿里云实现调用API接口

整体请求流程&#xff1a; 介绍&#xff1a; 本次解析通过阿里云云市场的云服务来实现查询车型库大全查询&#xff0c;首先需要选择一家可以提供查询的商品。 [探数API]车型库查询_API专区_云市场-阿里云 步骤1: 选择商品 如图点击免费试用&#xff0c;即可免费申请该接口数…

使用合同比对工具时,有哪些常见问题和解决方案?

在使用合同比对工具的过程中&#xff0c;企业可能会面临一系列挑战&#xff0c;这些问题可能会影响工具的效率和效果。以下是一些常见的问题&#xff1a; 1.兼容性问题&#xff1a;在不同的工作环境中&#xff0c;合同文档可能以不同的格式存在&#xff0c;如PDF、Word、Excel…

常见接口限流算法

常见接口限流算法 今天面试时&#xff0c;面试官对我之前实习时实现的限流功能很感兴趣&#xff0c;发起了夺命连问… 正好趁此机会好好整理一下&#xff0c;很棒。 常用的限流算法 固定窗口 实现思想 固定窗口的实现原理是&#xff1a;在指定周期内累加访问次数&#xf…

BP神经网络学习内容分享:学习过程中常见的问题

BP神经网络是一种常用的机器学习算法&#xff0c;它在各个领域都有广泛的应用。然而&#xff0c;在学习BP神经网络的过程中&#xff0c;往往会遇到一些困难和问题。本文将介绍一些学习BP神经网络常见问题&#xff0c;并提供解决方法供参考。 一、过拟合问题 BP神经网络的一个常…

iPhone短信误删了?别急,这几招帮你轻松恢复!

在快节奏的生活中&#xff0c;我们频繁地使用iPhone进行各种操作&#xff0c;包括发送和接收短信。然而&#xff0c;有时候一个不小心&#xff0c;重要的短信就可能被误删&#xff0c;让人焦急万分。别担心&#xff0c;今天就来分享几个实用的方法&#xff0c;帮助你找回那些“…

VScode 使用记录

插件 1、代码提示插件&#xff1a;Codeium 安装说明&#xff1a;Codeium&#xff1a;强大且免费的AI智能编程助手 - Su的技术博客 (verysu.com) 用google账号登陆&#xff0c;跳转按照官网给的三个步骤来 step1&#xff1a;复制token&#xff1b; step2&#xff1a;在文件页…

重生之我们在ES顶端相遇第10 章- 分分分词器的基本使用

文章目录 思维导图0. 前言1. 光速上手1.1 指定分词器1.2 测试分词器 2. 分词流程(重要)2.1 基本介绍2.2 深入如何测试分词器 3. 自定义一个简单的分词器 思维导图 0. 前言 分词器在 ES 搜索使用中非常关键&#xff0c;一个好的分词器能够提高搜索的质量&#xff0c;让用户搜索…

mysql中的mysql 库不存在,进行恢复

mysql中的mysql 库不存在&#xff0c;进行恢复 解决方法&#xff1a; 关闭数据库 service mysqld stop 以跳过权限认证方式启动mysql mysqld_safe --defaults-file/etc/my.cnf --skip-grant-tables & 在输入&#xff1a;mysql -u root 在输入&#xff1a;use mysql 在输…

【C++ Primer Plus习题】9.1

问题: 解答: main.cpp #include <iostream> #include <string> #include "golf.h" using namespace std;#define SIZE 5int main() {golf ann;setgolf(ann, "AnnBirdfree", 24);golf andy;setgolf(andy);showgolf(ann);showgolf(andy);return…

JVM1-初识JVM

目录 什么是JVM JVM的功能 解释和运行 内存管理 即时编译 Java性能低的主要原因和跨平台特性 常见的JVM 什么是JVM JVM 全称是 Java Virtual Machine&#xff0c;中文译名&#xff1a;Java虚拟机 JVM本质上是一个运行在计算机上的程序&#xff0c;它的职责是运行Java字…

自建远程桌面RustDesk服务器(CentOS配置,保姆级案例)

安装环境: 系统:Centos7 网络:连接互联网 一、环境准备: ①变更国内yum源(方便安装包下载) 备份源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载国内(阿里)源文件: curl -o /etc/yum.repos.d/CentOS-Base.repo htt…

蔡英丽医生:“斑块克星”三种食物,轻松守护心血管健康

在这个快节奏的时代&#xff0c;心血管疾病悄然成为威胁我们健康的“隐形杀手”。尤其是血管斑块&#xff0c;它不仅悄悄堵塞着我们的生命通道&#xff0c;还可能引发心脏病、中风等严重后果。但别担心&#xff0c;今天我们就来揭秘那些藏在日常餐桌上的“斑块克星”&#xff0…

CMU 10423 Generative AI:lec1

文章目录 1 概述2 内容摘录AIGC的主要应用大模型训练时&#xff0c;分布式训练有哪几种方式&#xff1f;NLP模型和CV模型发展历史本课程触及的主题课程前提、评分标准、阅读材料、5个作业、大项目课程学习目标 3 阅读材料3.1 Sequence Modeling: Recurrent and Recursive Nets.…