AtCoder Beginner Contest 337 (ABCDEG题)

A - Scoreboard

Problem Statement

Team Takahashi and Team Aoki played N N N matches.
In the i i i-th match ( 1 ≤ i ≤ N ) (1\leq i\leq N) (1iN), Team Takahashi scored X i X _ i Xi points, and Team Aoki scored Y i Y _ i Yi points.
The team with the higher total score from the N N N matches wins.
Print the winner.
If the two teams have the same total score, it is a draw.

Constraints

1 ≤ N ≤ 100 1\leq N\leq 100 1N100
0 ≤ X i ≤ 100 ( 1 ≤ i ≤ N ) 0\leq X _ i\leq 100\ (1\leq i\leq N) 0Xi100 (1iN)
0 ≤ Y i ≤ 100 ( 1 ≤ i ≤ N ) 0\leq Y _ i\leq 100\ (1\leq i\leq N) 0Yi100 (1iN)
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
X 1 X _ 1 X1 Y 1 Y _ 1 Y1
X 2 X _ 2 X2 Y 2 Y _ 2 Y2
⋮ \vdots
X N X _ N XN Y N Y _ N YN

Output

If Team Takahashi wins, print Takahashi; if Team Aoki wins, print Aoki; if it is a draw, print Draw.

Sample Input 1

4
10 2
10 1
10 2
3 2

Sample Output 1

Takahashi

In four matches, Team Takahashi scored 33 33 33 points, and Team Aoki scored 7 7 7 points.
Team Takahashi wins, so print Takahashi.

Sample Input 2

6
5 4
4 5
2 4
1 6
7 1
3 2

Sample Output 2

Draw

Both teams scored 22 22 22 points.
It is a draw, so print Draw.

Sample Input 3

4
0 0
10 10
50 50
0 100

Sample Output 3

Aoki

One or both teams may score no points in a match.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int N, X, Y, S1 = 0, S2 = 0;cin >> N;while (N --)cin >> X >> Y, S1 += X, S2 += Y;if (S1 == S2) puts("Draw");else if (S1 > S2) puts("Takahashi");else puts("Aoki");return 0;
}

B - Extended ABC

Problem Statement

We define Extended A strings, Extended B strings, Extended C strings, and Extended ABC strings as follows:
A string S S S is an Extended A string if all characters in S S S are A.
A string S S S is an Extended B string if all characters in S S S are B.
A string S S S is an Extended C string if all characters in S S S are C.
A string S S S is an Extended ABC string if there is an Extended A string S A S_A SA, an Extended B string S B S_B SB, and an Extended C string S C S_C SC such that the string obtained by concatenating S A , S B , S C S_A, S_B, S_C SA,SB,SC in this order equals S S S.
For example, ABC, A, and AAABBBCCCCCCC are Extended ABC strings, but ABBAAAC and BBBCCCCCCCAAA are not.
Note that the empty string is an Extended A string, an Extended B string, and an Extended C string.
You are given a string S S S consisting of A, B, and C.
If S S S is an Extended ABC string, print Yes; otherwise, print No.

Constraints

S S S is a string consisting of A, B, and C.
1 ≤ ∣ S ∣ ≤ 100 1\leq|S|\leq 100 1S100 ( ∣ S ∣ |S| S is the length of the string S S S.)

Input

The input is given from Standard Input in the following format:

S S S

Output

If S S S is an Extended ABC string, print Yes; otherwise, print No.

Sample Input 1

AAABBBCCCCCCC

Sample Output 1

Yes

AAABBBCCCCCCC is an Extended ABC string because it is a concatenation of an Extended A string of length 3 3 3, AAA, an Extended B string of length 3 3 3, BBB, and an Extended C string of length 7 7 7, CCCCCCC, in this order.
Thus, print Yes.

Sample Input 2

ACABABCBC

Sample Output 2

No

There is no triple of Extended A string S A S_A SA, Extended B string S B S_B SB, and Extended C string S C S_C SC such that the string obtained by concatenating S A S_A SA, S B S_B SB, and S C S_C SC in this order equals ACABABCBC.
Therefore, print No.

Sample Input 3

A

Sample Output 3

Yes

Sample Input 4

ABBBBBBBBBBBBBCCCCCC

Sample Output 4

Yes

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);string S;cin >> S;S.erase(unique(S.begin(), S.end()), S.end());if (S == "ABC" || S == "A" || S == "B" || S == "C" || S == "AB" || S == "AC" || S == "BC") puts("Yes");else puts("No");return 0;
}

C - Lining Up 2

Problem Statement

There are N N N people standing in a line: person 1 1 1, person 2 2 2, … \ldots , person N N N.
You are given the arrangement of the people as a sequence A = ( A 1 , A 2 , … , A N ) A=(A _ 1,A _ 2,\ldots,A _ N) A=(A1,A2,,AN) of length N N N.
A i ( 1 ≤ i ≤ N ) A _ i\ (1\leq i\leq N) Ai (1iN) represents the following information:
if A i = − 1 A _ i=-1 Ai=1, person i i i is at the front of the line;
if A i ≠ − 1 A _ i\neq -1 Ai=1, person i i i is right behind person A i A _ i Ai.
Print the people’s numbers in the line from front to back.

Constraints

1 ≤ N ≤ 3 × 1 0 5 1\leq N\leq3\times10 ^ 5 1N3×105
A i = − 1 A _ i=-1 Ai=1 or 1 ≤ A i ≤ N ( 1 ≤ i ≤ N ) 1\leq A _ i\leq N\ (1\leq i\leq N) 1AiN (1iN)
There is exactly one way to arrange the N N N people consistent with the information given.
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A _ 1 A1 A 2 A _ 2 A2 … \ldots A N A _ N AN

Output

If person s 1 s _ 1 s1, person s 2 s _ 2 s2, … \ldots , person s N s _ N sN are standing in the line in this order, print s 1 s _ 1 s1, s 2 s _ 2 s2, … \ldots , and s N s _ N sN in this order, separated by spaces.

Sample Input 1

6
4 1 -1 5 3 2

Sample Output 1

3 5 4 1 2 6

If person 3 3 3, person 5 5 5, person 4 4 4, person 1 1 1, person 2 2 2, and person 6 6 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:
person 1 1 1 is standing right behind person 4 4 4,
person 2 2 2 is standing right behind person 1 1 1,
person 3 3 3 is at the front of the line,
person 4 4 4 is standing right behind person 5 5 5,
person 5 5 5 is standing right behind person 3 3 3, and
person 6 6 6 is standing right behind person 2 2 2.
Thus, print 3 3 3, 5 5 5, 4 4 4, 1 1 1, 2 2 2, and 6 6 6 in this order, separated by spaces.

Sample Input 2

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

Sample Output 2

1 2 3 4 5 6 7 8 9 10

Sample Input 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

Sample Output 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 3e5 + 10;int N;
int A[SIZE], Next[SIZE];signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(Next, -1, sizeof Next);cin >> N;for (int i = 1; i <= N; i ++){cin >> A[i];if (A[i] == -1) Next[0] = i;else Next[A[i]] = i;}for (int i = 0; Next[i] != -1; i = Next[i])cout << Next[i] << " ";return 0;
}

D - Cheating Gomoku Narabe

Problem Statement

There is a grid with H H H rows and W W W columns. Let ( i , j ) (i, j) (i,j) denote the cell at the i i i-th row from the top and the j j j-th column from the left.
Each cell contains one of the characters o, x, and .. The characters written in each cell are represented by H H H strings S 1 , S 2 , … , S H S_1, S_2, \ldots, S_H S1,S2,,SH of length W W W; the character written in cell ( i , j ) (i, j) (i,j) is the j j j-th character of the string S i S_i Si.
For this grid, you may repeat the following operation any number of times, possibly zero:
Choose one cell with the character . and change the character in that cell to o.
Determine if it is possible to have a sequence of K K K horizontally or vertically consecutive cells with o written in all cells (in other words, satisfy at least one of the following two conditions). If it is possible, print the minimum number of operations required to achieve this.
There is an integer pair ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H 1 \leq i \leq H 1iH and 1 ≤ j ≤ W − K + 1 1 \leq j \leq W-K+1 1jWK+1 such that the characters in cells ( i , j ) , ( i , j + 1 ) , … , ( i , j + K − 1 ) (i, j), (i, j+1), \ldots, (i, j+K-1) (i,j),(i,j+1),,(i,j+K1) are all o.
There is an integer pair ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H − K + 1 1 \leq i \leq H-K+1 1iHK+1 and 1 ≤ j ≤ W 1 \leq j \leq W 1jW such that the characters in cells ( i , j ) , ( i + 1 , j ) , … , ( i + K − 1 , j ) (i, j), (i+1, j), \ldots, (i+K-1, j) (i,j),(i+1,j),,(i+K1,j) are all o.

Constraints

H H H, W W W, and K K K are integers.
1 ≤ H 1 \leq H 1H
1 ≤ W 1 \leq W 1W
H × W ≤ 2 × 1 0 5 H \times W \leq 2 \times 10^5 H×W2×105
1 ≤ K ≤ max ⁡ { H , W } 1 \leq K \leq \max\lbrace H, W \rbrace 1Kmax{H,W}
S i S_i Si is a string of length W W W consisting of the characters o, x, and ..

Input

The input is given from Standard Input in the following format:

H H H W W W K K K
S 1 S_1 S1
S 2 S_2 S2
⋮ \vdots
S H S_H SH

Output

If it is impossible to satisfy the condition in the problem statement, print -1. Otherwise, print the minimum number of operations required to do so.

Sample Input 1

3 4 3
xo.x
..o.
xx.o

Sample Output 1

2

By operating twice, for example, changing the characters in cells ( 2 , 1 ) (2, 1) (2,1) and ( 2 , 2 ) (2, 2) (2,2) to o, you can satisfy the condition in the problem statement, and this is the minimum number of operations required.

Sample Input 2

4 2 3
.o
.o
.o
.o

Sample Output 2

0

The condition is satisfied without performing any operations.

Sample Input 3

3 3 3
x..
..x
.x.

Sample Output 3

-1

It is impossible to satisfy the condition, so print -1.

Sample Input 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

Sample Output 4

3

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int H, W, K;
int S1[SIZE], S2[SIZE];signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> H >> W >> K;char X;std::vector<vector<int>> G(H + 1, vector<int>(W + 1));for (int i = 1; i <= H; i ++)for (int j = 1; j <= W; j ++){cin >> X;if (X == 'x') G[i][j] = 0;else if (X == '.') G[i][j] = 1;else G[i][j] = 2;}int Result = 1e18;for (int i = 1; i <= H; i ++){for (int j = 1; j <= W; j ++)S1[j] = S1[j - 1] + (G[i][j] == 0), S2[j] = S2[j - 1] + (G[i][j] == 2);for (int j = K; j <= W; j ++)if (S1[j] - S1[j - K] == 0)Result = min(Result, K - S2[j] + S2[j - K]);}for (int i = 1; i <= W; i ++){for (int j = 1; j <= H; j ++)S1[j] = S1[j - 1] + (G[j][i] == 0), S2[j] = S2[j - 1] + (G[j][i] == 2);for (int j = K; j <= H; j ++)if (S1[j] - S1[j - K] == 0)Result = min(Result, K - S2[j] + S2[j - K]);}if (Result == 1e18) cout << -1 << endl;else cout << Result << endl;return 0;
}

E - Bad Juice

Problem Statement

This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).
There are N N N bottles of juice, numbered 1 1 1 to N N N. It has been discovered that exactly one of these bottles has gone bad. Even a small sip of the spoiled juice will cause stomach upset the next day.
Takahashi must identify the spoiled juice by the next day. To do this, he decides to call the minimum necessary number of friends and serve them some of the N N N bottles of juice. He can give any number of bottles to each friend, and each bottle of juice can be given to any number of friends.
Print the number of friends to call and how to distribute the juice, then receive information on whether each friend has an upset stomach the next day, and print the spoiled bottle’s number.

Constraints

N N N is an integer.
2 ≤ N ≤ 100 2 \leq N \leq 100 2N100

Input/Output

This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).
Before the interaction, the judge secretly selects an integer X X X between 1 1 1 and N N N as the spoiled bottle’s number. The value of X X X is not given to you. Also, the value of X X X may change during the interaction as long as it is consistent with the constraints and previous outputs.
First, the judge will give you N N N as input.

N N N

You should print the number of friends to call, M M M, followed by a newline.

M M M

Next, you should perform the following procedure to print M M M outputs.
For i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,,M, the i i i-th output should contain the number K i K_i Ki of bottles of juice you will serve to the i i i-th friend, and the K i K_i Ki bottles’ numbers in ascending order, A i , 1 , A i , 2 , … , A i , K i A_{i, 1}, A_{i, 2}, \ldots, A_{i, K_i} Ai,1,Ai,2,,Ai,Ki, separated by spaces, followed by a newline.

K i K_i Ki A i , 1 A_{i, 1} Ai,1 A i , 2 A_{i, 2} Ai,2 … \ldots A i , K i A_{i, K_i} Ai,Ki

Then, the judge will inform you whether each friend has a stomach upset the next day by giving you a string S S S of length M M M consisting of 0 and 1.

S S S

For i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,,M, the i i i-th friend has a stomach upset if and only if the i i i-th character of S S S is 1.
You should respond by printing the number of the spoiled juice bottle X ′ X' X, followed by a newline.

X ′ X' X

Then, terminate the program immediately.
If the M M M you printed is the minimum necessary number of friends to identify the spoiled juice out of the N N N bottles, and the X ′ X' X you printed matches the spoiled bottle’s number X X X, then your program is considered correct.

Notes

Each output should end with a newline and flushing Standard Output. Otherwise, you may receive a TLE verdict.
The verdict is indeterminate if your program prints an invalid output during the interaction or terminates prematurely. In particular, note that if a runtime error occurs during the execution of the program, the verdict may be WA or TLE instead of RE.
Terminate the program immediately after printing X ′ X' X. Otherwise, the verdict is indeterminate.
The judge for this problem is adaptive, meaning that the value of X X X may change as long as it is consistent with the constraints and previous outputs.

Sample Input/Output

Below is an interaction with N = 3 N = 3 N=3.

InputOutputDescription
3The number of bottles, $N$, is given.
2Print the number of friends to call, $M$.
2 1 2Serve juice $1$ and juice $2$ to the first friend.
1 2Serve juice $2$ to the second friend.
10The string $S$ is given, indicating whether each friend has a stomach upset the next day.
1Print the spoiled bottle's number.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int N;cin >> N;int M = 0;while ((1ll << M) < N) M ++;cout << M << endl;for (int i = 0; i < M; i ++){std::vector<int> A;for (int j = 1; j <= N; j ++)if (j >> i & 1)A.push_back(j);cout << A.size() << " ";for (auto c : A) cout << c << " ";cout << endl;}string S;cin >> S;int Result = 0;for (int i = 0; i < M; i ++)if (S[i] == '1')Result += (1ll << i);if (Result == 0) cout << N << endl;else cout << Result << endl;return 0;
}

F - Usual Color Ball Problems

Problem Statement

You are given positive integers N N N, M M M, K K K, and a sequence of positive integers of length N N N, ( C 1 , C 2 , … , C N ) (C_1, C_2, \ldots, C_N) (C1,C2,,CN). For each r = 0 , 1 , 2 , … , N − 1 r = 0, 1, 2, \ldots, N-1 r=0,1,2,,N1, print the answer to the following problem.

There is a sequence of $N$ colored balls. For $i = 1, 2, \ldots, N$, the color of the $i$-th ball from the beginning of the sequence is $C_i$. Additionally, there are $M$ empty boxes numbered $1$ to $M$. Determine the total number of balls in the boxes after performing the following steps. First, perform the following operation $r$ times. Move the frontmost ball in the sequence to the end of the sequence. Then, repeat the following operation as long as at least one ball remains in the sequence. If there is a box that already contains at least one but fewer than $K$ balls of the same color as the frontmost ball in the sequence, put the frontmost ball into that box. If there is no such box, If there is an empty box, put the frontmost ball into the one with the smallest box number. If there are no empty boxes, eat the frontmost ball without putting it into any box.
## Constraints

All input values are integers.
1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
1 ≤ M , K ≤ N 1 \leq M, K \leq N 1M,KN
1 ≤ C i ≤ N 1 \leq C_i \leq N 1CiN

Input

The input is given from Standard Input in the following format:

N N N M M M K K K
C 1 C_1 C1 C 2 C_2 C2 … \ldots C N C_N CN

Output

Print the answer X r X_r Xr to the problem for each r = 0 , 1 , 2 , … , N − 1 r = 0, 1, 2, \ldots, N-1 r=0,1,2,,N1 over N N N lines as follows:

X 0 X_0 X0
X 1 X_1 X1
⋮ \vdots
X N − 1 X_{N-1} XN1

Sample Input 1

7 2 2
1 2 3 5 2 5 4

Sample Output 1

3
3
3
4
4
3
2

For example, let us explain the procedure for r = 1 r = 1 r=1.
First, perform the operation “Move the frontmost ball in the sequence to the end of the sequence” once, and the sequence of ball colors becomes ( 2 , 3 , 5 , 2 , 5 , 4 , 1 ) (2, 3, 5, 2, 5, 4, 1) (2,3,5,2,5,4,1).
Then, proceed with the operation of putting balls into boxes as follows:
First operation: The color of the frontmost ball is 2 2 2. There is no box with at least one but fewer than two balls of color 2 2 2, so put the frontmost ball into the empty box with the smallest box number, box 1 1 1.
Second operation: The color of the frontmost ball is 3 3 3. There is no box with at least one but fewer than two balls of color 3 3 3, so put the frontmost ball into the empty box with the smallest box number, box 2 2 2.
Third operation: The color of the frontmost ball is 5 5 5. There is no box with at least one but fewer than two balls of color 5 5 5 and no empty boxes, so eat the frontmost ball.
Fourth operation: The color of the frontmost ball is 2 2 2. There is a box, box 1 1 1, with at least one but fewer than two balls of color 2 2 2, so put the frontmost ball into box 1 1 1.
Fifth operation: The color of the frontmost ball is 5 5 5. There is no box with at least one but fewer than two balls of color 5 5 5 and no empty boxes, so eat the frontmost ball.
Sixth operation: The color of the frontmost ball is 4 4 4. There is no box with at least one but fewer than two balls of color 4 4 4 and no empty boxes, so eat the frontmost ball.
Seventh operation: The color of the frontmost ball is 1 1 1. There is no box with at least one but fewer than two balls of color 1 1 1 and no empty boxes, so eat the frontmost ball.
The final total number of balls in the boxes is 3 3 3, so the answer to the problem for r = 1 r = 1 r=1 is 3 3 3.

Sample Input 2

20 5 4
20 2 20 2 7 3 11 20 3 8 7 9 1 11 8 20 2 18 11 18

Sample Output 2

14
14
14
14
13
13
13
11
8
9
9
11
13
14
14
14
14
14
14
13

Solution

这题没研究,就不写了


Code

G - Tree Inversion

Problem Statement

You are given a tree T T T with N N N vertices: vertex 1 1 1, vertex 2 2 2, … \ldots , vertex N N N.
The i i i-th edge ( 1 ≤ i < N ) (1\leq i\lt N) (1i<N) connects vertices u i u_i ui and v i v_i vi.
For a vertex u u u in T T T, define f ( u ) f(u) f(u) as follows:
f ( u ) ≔ f(u)\coloneqq f(u):= The number of pairs of vertices v v v and w w w in T T T that satisfy the following two conditions:
Vertex w w w is contained in the path connecting vertices u u u and v v v.
v < w v\lt w v<w.
Here, vertex w w w is contained in the path connecting vertices u u u and v v v also when u = w u=w u=w or v = w v=w v=w.
Find the values of f ( 1 ) , f ( 2 ) , … , f ( N ) f(1),f(2),\ldots,f(N) f(1),f(2),,f(N).

Constraints

2 ≤ N ≤ 2 × 1 0 5 2\leq N\leq2\times10^5 2N2×105
1 ≤ u i ≤ N ( 1 ≤ i ≤ N ) 1\leq u_i\leq N\ (1\leq i\leq N) 1uiN (1iN)
1 ≤ v i ≤ N ( 1 ≤ i ≤ N ) 1\leq v_i\leq N\ (1\leq i\leq N) 1viN (1iN)
The given graph is a tree.
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
u 1 u_1 u1 v 1 v_1 v1
u 2 u_2 u2 v 2 v_2 v2
⋮ \vdots
u N − 1 u_{N-1} uN1 v N − 1 v_{N-1} vN1

Output

Print the values of f ( 1 ) , f ( 2 ) , … , f ( N ) f(1),f(2),\ldots,f(N) f(1),f(2),,f(N) in this order, separated by spaces.

Sample Input 1

7
1 2
2 3
2 4
4 5
4 6
6 7

Sample Output 1

0 1 3 4 8 9 15

The given tree is as follows:

For example, f ( 4 ) = 4 f(4)=4 f(4)=4.
Indeed, for u = 4 u=4 u=4, four pairs ( v , w ) = ( 1 , 2 ) , ( 1 , 4 ) , ( 2 , 4 ) , ( 3 , 4 ) (v,w)=(1,2),(1,4),(2,4),(3,4) (v,w)=(1,2),(1,4),(2,4),(3,4) satisfy the conditions.

Sample Input 2

15
14 9
9 1
1 6
6 12
12 2
2 15
15 4
4 11
11 13
13 3
3 8
8 10
10 7
7 5

Sample Output 2

36 29 32 29 48 37 45 37 44 42 33 36 35 57 35

The given tree is as follows:

The value of f ( 14 ) f(14) f(14) is 57 57 57, which is the inversion number of the sequence ( 14 , 9 , 1 , 6 , 12 , 2 , 15 , 4 , 11 , 13 , 3 , 8 , 10 , 7 , 5 ) (14,9,1,6,12,2,15,4,11,13,3,8,10,7,5) (14,9,1,6,12,2,15,4,11,13,3,8,10,7,5).

Sample Input 3

24
7 18
4 2
5 8
5 15
6 5
13 8
4 6
7 11
23 16
6 18
24 16
14 21
20 15
16 18
3 16
11 10
9 11
15 14
12 19
5 1
9 17
5 22
11 19

Sample Output 3

20 20 41 20 21 20 28 28 43 44 36 63 40 46 34 40 59 28 53 53 66 42 62 63

Solution

具体见文后视频。


Code


#include <bits/stdc++.h>
#define lowbit(x) x & -x
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int N;
std::vector<int> G[SIZE];
int DFS_Order[SIZE], Id = 0, Sz[SIZE];
int Count[SIZE], Pos[SIZE];
int Root[SIZE], idx, Answer[SIZE];
struct Node
{int l, r;int cnt;
}Tree[SIZE * 21];int Build(int l, int r)
{int p = ++ idx;if (l == r) return p;int mid = l + r >> 1;Tree[p].l = Build(l, mid), Tree[p].r = Build(mid + 1, r);return p;
}int Insert(int p, int l, int r, int x)
{int q = ++ idx;Tree[q] = Tree[p];if (l == r){Tree[q].cnt ++;return q;}int mid = l + r >> 1;if (x <= mid) Tree[q].l = Insert(Tree[p].l, l, mid, x);else Tree[q].r = Insert(Tree[p].r, mid + 1, r, x);Tree[q].cnt = Tree[Tree[q].l].cnt + Tree[Tree[q].r].cnt;return q;
}int Query(int q, int p, int l, int r, int k)
{int Cnt = Tree[Tree[q].l].cnt - Tree[Tree[p].l].cnt;if (l == r) return Tree[q].cnt - Tree[p].cnt;int mid = l + r >> 1;if (k <= mid) return Query(Tree[q].l, Tree[p].l, l, mid, k);else return Cnt + Query(Tree[q].r, Tree[p].r, mid + 1, r, k);
}struct Segment
{struct Node{int l, r;LL Sum, Lazy;}Tree[SIZE << 2];void Pushup(int u) { Tree[u].Sum = Tree[u << 1].Sum + Tree[u << 1 | 1].Sum; }void Pushdown(int u){if (Tree[u].Lazy){Tree[u << 1].Sum += (LL)(Tree[u << 1].r - Tree[u << 1].l + 1) * Tree[u].Lazy;Tree[u << 1].Lazy += Tree[u].Lazy;Tree[u << 1 | 1].Sum += (LL)(Tree[u << 1 | 1].r - Tree[u << 1 | 1].l + 1) * Tree[u].Lazy;Tree[u << 1 | 1].Lazy += Tree[u].Lazy;Tree[u].Lazy = 0;}}void Build(int u, int l, int r){Tree[u] = {l, r};if (l == r) return;int mid = l + r >> 1;Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);}void Modify(int u, int l, int r, int d){if (Tree[u].l >= l && Tree[u].r <= r){Tree[u].Sum += (LL)(Tree[u].r - Tree[u].l + 1) * d;Tree[u].Lazy += d;return;}Pushdown(u);int mid = Tree[u].l + Tree[u].r >> 1;if (mid >= l) Modify(u << 1, l, r, d);if (mid < r) Modify(u << 1 | 1, l, r, d);Pushup(u);}long long Query(int u, int l, int r){if (Tree[u].l >= l && Tree[u].r <= r)return Tree[u].Sum;Pushdown(u);long long mid = Tree[u].l + Tree[u].r >> 1, Result = 0;if (mid >= l) Result = Query(u << 1, l, r);if (mid < r) Result += Query(u << 1 | 1, l, r);return Result;}
}Result;void DFS(int u, int fa)
{Sz[u] = 1;for (auto c : G[u]){if (c == fa) continue;DFS_Order[ ++ Id] = c, Pos[c] = Id;DFS(c, u);Sz[u] += Sz[c];}
}
void DFS2(int u, int fa)
{for (auto c : G[u]) { if (c == fa) continue; DFS2(c, u); }int C1 = Query(Root[Pos[u] + Sz[u] - 1], Root[Pos[u] - 1], 1, N, u);// cout << u << ":" << Pos[u] << " " << Pos[u] + Sz[u] - 1 << " " << C1 << endl;if (Pos[u] > 1) Result.Modify(1, 1, Pos[u] - 1, C1 - 1);if (Pos[u] + Sz[u] <= N) Result.Modify(1, Pos[u] + Sz[u], N, C1 - 1);Result.Modify(1, Pos[u], Pos[u] + Sz[u] - 1, u - C1);for (auto c : G[u]){if (c == fa) continue;int C2 = Query(Root[Pos[c] + Sz[c] - 1], Root[Pos[c] - 1], 1, N, u);// cout << u << " " << c << " " << C2 << endl;if (Pos[u] <= Pos[c] - 1) Result.Modify(1, Pos[u], Pos[c] - 1, C2);if (Pos[c] + Sz[c] <= Pos[u] + Sz[u] - 1) Result.Modify(1, Pos[c] + Sz[c], Pos[u] + Sz[u] - 1, C2);}
}bool Cmp(PII A, PII B) { return A.second < B.second; }signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N;int u, v;for (int i = 1; i < N; i ++)cin >> u >> v, G[u].push_back(v), G[v].push_back(u);Result.Build(1, 1, N);DFS_Order[ ++ Id] = 1, Pos[1] = Id;DFS(1, -1);// for (int i = 1; i <= N; i ++)// 	cout << DFS_Order[i] << " ";// cout << endl;Root[0] = Build(1, N);for (int i = 1; i <= N; i ++)Root[i] = Insert(Root[i - 1], 1, N, DFS_Order[i]);DFS2(1, -1);for (int i = 1; i <= N; i ++)Answer[DFS_Order[i]] = Result.Query(1, i, i);for (int i = 1; i <= N; i ++)cout << Answer[i] << " ";return 0;
}

视频题解

Atcoder Beginner Contest 337(A ~ E + G)


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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大数据关联规则挖掘:Apriori算法的深度探讨

文章目录 大数据关联规则挖掘&#xff1a;Apriori算法的深度探讨一、简介什么是关联规则挖掘&#xff1f;什么是频繁项集&#xff1f;什么是支持度与置信度&#xff1f;Apriori算法的重要性应用场景 二、理论基础项和项集支持度&#xff08;Support&#xff09;置信度&#xff…

第135期 一周游历(上)(20240120)

数据库管理135期 2024-01-20 第135期 一周游历(上)&#xff08;20240120&#xff09;1 PolarDB开发者大会2 工作3 Oracle甲骨文4 Oracle ACE总结 第135期 一周游历(上)&#xff08;20240120&#xff09; 作者&#xff1a;胖头鱼的鱼缸&#xff08;尹海文&#xff09; Oracle AC…

JS-WebAPIs-本地存储(五)

• 本地存储介绍 以前我们页面写的数据一刷新页面就没有了&#xff0c;是不是&#xff1f;随着互联网的快速发展&#xff0c;基于网页的应用越来越普遍&#xff0c;同时也变的越来越复杂&#xff0c;为了满足各种各样的需求&#xff0c;会经常 性在本地存储大量的数据&#xf…

运筹说 第107期 | 排队论创始人——阿格纳·克拉鲁普·爱尔朗

前面我们已经了解了非线性规划的相关内容&#xff0c;相信大家一定也有所收获&#xff0c;下面我们将带着大家继续了解排队论的相关内容&#xff0c;在本次文章中我们将一起走近排队论的奠基人——阿格纳克拉鲁普爱尔朗&#xff08;Agner Krarup Erlang&#xff09;&#xff0c…

基于Elasticsearch+Logstash+Kibana+Filebeat的日志收集分析及可视化

sudo rm /var/lib/dpkg/lock* sudo dpkg --configure -a apt update tail -f /var/log/car.log 1.1、项目概述 海量的业务应用&#xff0c;也带来了海量的日志数据&#xff0c;给业务应用的运维带来了新的挑战。例如&#xff0c;我们常用的网约车应用&#xff0c;单个平台…

I.MX8QM EC20适配

Android SDK&#xff1a;imx8_13.0.0_1.2.0(android 13 u-boot 2022.04 kernel 5.15.74) 一、内核修改 添加PID、VID。在“drivers/usb/serial/option.c”中可以看到EC25的PID和VID已经添加&#xff0c;而且这个EC25的PID和VID与EC20的相同&#xff0c;所以这里可以不用修改…

Traceroute 详解

前言 如果您是网络管理员&#xff0c;系统管理员或任何系统操作团队的一员&#xff0c;那么您可能已经听说过名为TRACEROUTE的工具。默认情况下&#xff0c;它是大多数操作系统中都提供的非常方便的工具。 网络管理员和系统管理员在日常活动中最常使用此工具。它基本上是一个…

二叉树 - 堆 | 数据结构中的小技巧大作用

&#x1f4f7; 江池俊&#xff1a; 个人主页 &#x1f525;个人专栏&#xff1a; ✅数据结构冒险记 ✅C语言进阶之路 &#x1f305; 有航道的人&#xff0c;再渺小也不会迷途。 文章目录 一、堆的概念及介绍二、结构图示三、堆的代码实现&#xff08;图解&#xff09;3.1 创…

HackTheBox - Medium - Linux - Noter

Noter Noter 是一种中型 Linux 机器&#xff0c;其特点是利用了 Python Flask 应用程序&#xff0c;该应用程序使用易受远程代码执行影响的“节点”模块。由于“MySQL”守护进程以用户“root”身份运行&#xff0c;因此可以通过利用“MySQL”的用户定义函数来利用它来获得RCE并…

快速上手的AI工具-文心3.5vs文心4.0

前言 大家好晚上好&#xff0c;现在AI技术的发展&#xff0c;它已经渗透到我们生活的各个层面。对于普通人来说&#xff0c;理解并有效利用AI技术不仅能增强个人竞争力&#xff0c;还能在日常生活中带来便利。无论是提高工作效率&#xff0c;还是优化日常任务&#xff0c;AI工…

一篇文章看懂云渲染,云渲染是什么?云渲染如何计费?云渲染怎么选择

云渲染是近年兴起的新行业&#xff0c;很多初学者对它不是很了解&#xff0c;云渲染是什么&#xff1f;为什么要选择云渲染&#xff1f;它是如何计费的又怎么选择&#xff1f;这篇文章我们就带大家了解下吧。 云渲染是什么 云渲染简单来说就是把本地的渲染工作迁移到云端进行的…

以超市数据微案例-fineBI可视化分析

一、入门案例&#xff1a; 2.分析思路&#xff1a; 数据清晰界面中添加毛利额计算 **所以在新增步骤之后&#xff0c;必须点击保存并更新&#xff0c;否则可视化界面中无法使用最新的数据 4、数据可视化分析 1&#xff09;销售额最高的十大商品种类 为1-8月超市数据&#xff…

响应式编程

Reactive-Stream Reactive Streams是JVM面向流的库的标准和规范 1、处理可能无限数量的元素 2、有序 3、在组件之间异步传递元素 4、强制性非阻塞,背压模式 在Java中,常用的背压机制是响应式流编程中的反压(Reactive Streams Backpressure)。反压是一种生产者-消费者模型,…

【刷题笔记4】

动态规划题目汇总 斐波那契数列&#xff1a;1&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;5&#xff0c;8&#xff0c;13…… 递归一把解决三类问题&#xff1a;1.数据定义是按照递归的&#xff08;斐波那契数列&#xff09;。2.问题解法是按递归算法实现的。 3.数据…

JUC-Java内存模型JMM

JMM概述 Java Meory Model java内存模型。在不同的硬件和不同的操作系统上&#xff0c;对内存的访问方式是不一样的。这就造成了同一套java代码运行在不同的操作系统上会出问题。JMM就屏蔽掉硬件和操作系统的差异&#xff0c;增加java代码的可移植性。这是一方面。 另一方面JM…

ios适配虚拟home键

在H5开发过程中遇到一个兼容性问题。iphone手机的虚拟home键会对屏幕底部的内容造成遮挡。要处理此问题&#xff0c;需要清楚安全区域这个概念。 安全区域 根据刘海和虚拟Home键&#xff0c;Apple为其设备提供了屏幕安全区域的视觉规范 竖屏&#xff1a;竖屏的时候&#xff…

UE5 C++学习笔记 常用宏的再次理解

1.随意创建一个类&#xff0c;他都有UCLASS()。GENERATED_BODY()这样的默认的宏。 UCLASS() 告知虚幻引擎生成类的反射数据。类必须派生自UObject. &#xff08;告诉引擎我是从远古大帝UObject中&#xff0c;继承而来&#xff0c;我们是一家人&#xff0c;只是我进化了其他功能…

矩阵和矩阵如何相乘?

矩阵与矩阵相乘遵循特定的数学规则。为了相乘&#xff0c;第一个矩阵的列数必须等于第二个矩阵的行数。矩阵乘法的结果是一个新矩阵&#xff0c;其行数等于第一个矩阵的行数&#xff0c;列数等于第二个矩阵的列数。矩阵乘法不满足交换律&#xff0c;即 AB≠BA。 例子&#xff…

131. 分割回文串 - 力扣(LeetCode)

问题描述 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。 回文串 是正着读和反着读都一样的字符串。 输入示例 s "aab"输出示例 [["a","a","b"],["…

Unity 面试篇|(九)操作系统与网络篇 【全面总结 | 持续更新】

目录 1. 客户端与服务器交互方式有几种&#xff1f;2. OSI七层模型有哪些&#xff0c;每一层的作用3. UDP/TCP含义&#xff0c;区别4. TCP/IP协议栈各个层次及分别的功能&#xff1f;5. 写出WWW的几个方法&#xff1f;6. Socket粘包7. Socket的封包、拆包8. Socket 客户端 队列…