Atcoder Beginner Contest 332 (A - F 题)

目录

  • [A - Online Shopping](https://atcoder.jp/contests/abc332/tasks/abc332_a)
    • Problem Statement
      • Constraints
      • Input
      • Output
      • Sample Input 1
      • Sample Output 1
      • Sample Input 2
      • Sample Output 2
      • Sample Input 3
      • Sample Output 3
    • Solution
    • Code
  • [B - Glass and Mu](https://atcoder.jp/contests/abc332/tasks/abc332_b)
    • Problem Statement
      • Constraints
      • Input
      • Output
      • Sample Input 1
      • Sample Output 1
      • Sample Input 2
      • Sample Output 2
    • Solution
    • Code
  • [C - T-shirts](https://atcoder.jp/contests/abc332/tasks/abc332_c)
    • Problem Statement
      • Constraints
      • Output
      • Sample Input 1
      • Sample Output 1
      • Sample Input 2
      • Sample Output 2
      • Sample Input 3
      • Sample Output 3
    • Solution
    • Code
  • [D - Swapping Puzzle](https://atcoder.jp/contests/abc332/tasks/abc332_d)
    • Problem Statement
      • Constraints
      • Input
      • Output
      • Sample Input 1
      • Sample Output 1
      • Sample Input 2
      • Sample Output 2
      • Sample Input 3
      • Sample Output 3
      • Sample Input 4
      • Sample Output 4
    • Solution
    • Code
  • [E - Lucky bag](https://atcoder.jp/contests/abc332/tasks/abc332_e)
    • Problem Statement
      • Constraints
      • Input
      • Output
      • Sample Input 1
      • Sample Output 1
    • Solution
    • Code
  • [F - Random Update Query](https://atcoder.jp/contests/abc332/tasks/abc332_f)
    • Problem Statement
      • Constraints
      • Input
      • Output
      • Sample Input 1
      • Sample Output 1
      • Sample Input 2
      • Sample Output 2
      • Sample Input 3
      • Sample Output 3
    • Solution
    • Code
  • 视频题解

A - Online Shopping

Problem Statement

AtCoder Inc. sells merchandise through its online shop.

Takahashi has decided to purchase N N N types of products from there.
For each integer i i i from 1 1 1 to N N N, the i i i-th type of product has a price of P i P_i Pi yen each, and he will buy Q i Q_i Qi of this.

Additionally, he must pay a shipping fee.
The shipping fee is 0 0 0 yen if the total price of the products purchased is S S S yen or above, and K K K yen otherwise.

He will pay the total price of the products purchased plus the shipping fee.
Calculate the amount he will pay.

Constraints

  • 1 ≤ N ≤ 100 1\leq N\leq 100 1N100
  • 1 ≤ S ≤ 10000 1\leq S\leq 10000 1S10000
  • 1 ≤ K ≤ 10000 1\leq K\leq 10000 1K10000
  • 1 ≤ P i ≤ 10000 1\leq P_i\leq 10000 1Pi10000
  • 1 ≤ Q i ≤ 100 1\leq Q_i\leq 100 1Qi100
  • All input values are integers.

Input

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

N N N S S S K K K
P 1 P_1 P1 Q 1 Q_1 Q1
P 2 P_2 P2 Q 2 Q_2 Q2
⋮ \vdots
P N P_N PN Q N Q_N QN

Output

Print the amount Takahashi will pay for online shopping.

Sample Input 1

2 2000 500
1000 1
100 6

Sample Output 1

2100

Takahashi buys one product for 1000 1000 1000 yen and six products for 100 100 100 yen each.
Thus, the total price of the products is 1000 × 1 + 100 × 6 = 1600 1000\times 1+100\times 6=1600 1000×1+100×6=1600 yen.
Since the total amount for the products is less than 2000 2000 2000 yen, the shipping fee will be 500 500 500 yen.
Therefore, the amount Takahashi will pay is 1600 + 500 = 2100 1600+500=2100 1600+500=2100 yen.

Sample Input 2

3 2000 500
1000 1
100 6
5000 1

Sample Output 2

6600

The total price of the products is 1000 × 1 + 100 × 6 + 5000 × 1 = 6600 1000\times 1+100\times 6+5000\times 1=6600 1000×1+100×6+5000×1=6600 yen.
Since the total amount for the products is not less than 2000 2000 2000 yen, the shipping fee will be 0 0 0 yen.
Therefore, the amount Takahashi will pay is 6600 + 0 = 6600 6600+0=6600 6600+0=6600 yen.

Sample Input 3

2 2000 500
1000 1
1000 1

Sample Output 3

2000

There may be multiple products with the same price per item.

Solution

具体见文后视频。


Code

#include <iostream>
#define int long longusing namespace std;typedef pair<int, int> PII;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int N, S, K;cin >> N >> S >> K;int Sum = 0, P, Q;for (int i = 1; i <= N; i ++)cin >> P >> Q, Sum += P * Q;if (Sum < S) cout << Sum + K << endl;else cout << Sum << endl;return 0;
}

B - Glass and Mu

Problem Statement

AtCoder Inc. sells glasses and mugs.

Takahashi has a glass with a capacity of G G G milliliters and a mug with a capacity of M M M milliliters.
Here, G < M G<M G<M.

Initially, both the glass and the mug are empty.
After performing the following operation K K K times, determine how many milliliters of water are in the glass and the mug, respectively.

  • When the glass is filled with water, that is, the glass contains exactly G G G milliliters of water, discard all the water from the glass.
  • Otherwise, if the mug is empty, fill the mug with water.
  • Otherwise, transfer water from the mug to the glass until the mug is empty or the glass is filled with water.

Constraints

  • 1 ≤ K ≤ 100 1\leq K\leq 100 1K100
  • 1 ≤ G < M ≤ 1000 1\leq G<M\leq 1000 1G<M1000
  • G G G, M M M, and K K K are integers.

Input

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

K K K G G G M M M

Output

Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K K K times.

Sample Input 1

5 300 500

Sample Output 1

200 500

The operation will be performed as follows. Initially, both the glass and the mug are empty.

  • Fill the mug with water. The glass has 0 0 0 milliliters, and the mug has 500 500 500 milliliters of water.
  • Transfer water from the mug to the glass until the glass is filled. The glass has 300 300 300 milliliters, and the mug has 200 200 200 milliliters of water.
  • Discard all the water from the glass. The glass has 0 0 0 milliliters, and the mug has 200 200 200 milliliters of water.
  • Transfer water from the mug to the glass until the mug is empty. The glass has 200 200 200 milliliters, and the mug has 0 0 0 milliliters of water.
  • Fill the mug with water. The glass has 200 200 200 milliliters, and the mug has 500 500 500 milliliters of water.

Thus, after five operations, the glass has 200 200 200 milliliters, and the mug has 500 500 500 milliliters of water. Hence, print 200 200 200 and 500 500 500 in this order, separated by a space.

Sample Input 2

5 100 200

Sample Output 2

0 0

Solution

具体见文后视频。


Code

#include <iostream>
#define int long longusing namespace std;typedef pair<int, int> PII;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int K, M, G;cin >> K >> G >> M;int A = 0, B = 0;while (K --){if (A == G) A = 0;else if (B == 0) B = M;else{int Push = min(G - A, B);A += Push, B -= Push;}}cout << A << " " << B << endl;return 0;
}

C - T-shirts

Problem Statement

AtCoder Inc. sells T-shirts with its logo.

You are given Takahashi’s schedule for N N N days as a string S S S of length N N N consisting of 0, 1, and 2.
Specifically, for an integer i i i satisfying 1 ≤ i ≤ N 1\leq i\leq N 1iN,

  • if the i i i-th character of S S S is 0, he has no plan scheduled for the i i i-th day;
  • if the i i i-th character of S S S is 1, he plans to go out for a meal on the i i i-th day;
  • if the i i i-th character of S S S is 2, he plans to attend a competitive programming event on the i i i-th day.

Takahashi has M M M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.

  • On days he goes out for a meal, he will wear a plain or logo T-shirt.
  • On days he attends a competitive programming event, he will wear a logo T-shirt.
  • On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
  • Once he wears a T-shirt, he cannot wear it again until he washes it.

Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N N N days. If he does not need to buy new T-shirts, print 0 0 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.

Constraints

  • 1 ≤ M ≤ N ≤ 1000 1\leq M\leq N\leq 1000 1MN1000
  • S S S is a string of length N N N consisting of 0, 1, and 2.
  • N N N and M M M are integers.

Output

Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print $ 0$.

Sample Input 1

6 1
112022

Sample Output 1

2

If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:

  • On the first day, he wears a logo T-shirt to go out for a meal.
  • On the second day, he wears a plain T-shirt to go out for a meal.
  • On the third day, he wears a logo T-shirt to attend a competitive programming event.
  • On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
  • On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
  • On the sixth day, he wears a logo T-shirt to attend a competitive programming event.

If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2 2 2.

Sample Input 2

3 1
222

Sample Output 2

3

Sample Input 3

2 1
01

Sample Output 3

0

He does not need to buy new T-shirts.

Solution

具体见文后视频。


Code

#include <iostream>
#define int long longusing namespace std;typedef pair<int, int> PII;signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int N, M;string S;cin >> N >> M >> S;S = ' ' + S;int Plain = M, Logo = 0, TL = 0;for (int i = 1; i <= N; i ++)if (S[i] == '1'){if (Plain)Plain --;elseif (Logo) Logo --;else TL ++;}else if (S[i] == '2'){if (Logo) Logo --;else TL ++;}elsePlain = M, Logo = TL;cout << TL << endl;return 0;
}

D - Swapping Puzzle

Problem Statement

You are given two grids, A and B, each with H H H rows and W W W columns.

For each pair of integers ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H 1 \leq i \leq H 1iH and 1 ≤ j ≤ W 1 \leq j \leq W 1jW, let ( i , j ) (i, j) (i,j) denote the cell in the i i i-th row and j j j-th column. In grid A, cell ( i , j ) (i, j) (i,j) contains the integer A i , j A_{i, j} Ai,j. In grid B, cell ( i , j ) (i, j) (i,j) contains the integer B i , j B_{i, j} Bi,j.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer i i i satisfying 1 ≤ i ≤ H − 1 1 \leq i \leq H-1 1iH1 and swap the i i i-th and ( i + 1 ) (i+1) (i+1)-th rows in grid A.
  • Choose an integer i i i satisfying 1 ≤ i ≤ W − 1 1 \leq i \leq W-1 1iW1 and swap the i i i-th and ( i + 1 ) (i+1) (i+1)-th columns in grid A.

Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.

Here, grid A is identical to grid B if and only if, for all pairs of integers ( i , j ) (i, j) (i,j) satisfying 1 ≤ i ≤ H 1 \leq i \leq H 1iH and 1 ≤ j ≤ W 1 \leq j \leq W 1jW, the integer written in cell ( i , j ) (i, j) (i,j) of grid A is equal to the integer written in cell ( i , j ) (i, j) (i,j) of grid B.

Constraints

  • All input values are integers.
  • 2 ≤ H , W ≤ 5 2 \leq H, W \leq 5 2H,W5
  • 1 ≤ A i , j , B i , j ≤ 1 0 9 1 \leq A_{i, j}, B_{i, j} \leq 10^9 1Ai,j,Bi,j109

Input

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

H H H W W W
A 1 , 1 A_{1, 1} A1,1 A 1 , 2 A_{1, 2} A1,2 ⋯ \cdots A 1 , W A_{1, W} A1,W
A 2 , 1 A_{2, 1} A2,1 A 2 , 2 A_{2, 2} A2,2 ⋯ \cdots A 2 , W A_{2, W} A2,W
⋮ \vdots
A H , 1 A_{H, 1} AH,1 A H , 2 A_{H, 2} AH,2 ⋯ \cdots A H , W A_{H, W} AH,W
B 1 , 1 B_{1, 1} B1,1 B 1 , 2 B_{1, 2} B1,2 ⋯ \cdots B 1 , W B_{1, W} B1,W
B 2 , 1 B_{2, 1} B2,1 B 2 , 2 B_{2, 2} B2,2 ⋯ \cdots B 2 , W B_{2, W} B2,W
⋮ \vdots
B H , 1 B_{H, 1} BH,1 B H , 2 B_{H, 2} BH,2 ⋯ \cdots B H , W B_{H, W} BH,W

Output

If it is impossible to make grid A identical to grid B, output -1. Otherwise, print the minimum number of operations required to make grid A identical to grid B.

Sample Input 1

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

Sample Output 1

3

Swapping the fourth and fifth columns of the initial grid A yields the following grid:

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

Then, swapping the second and third rows yields the following grid:

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

Finally, swapping the second and third columns yields the following grid, which is identical to grid B:

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

You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3 3 3.

Sample Input 2

2 2
1 1
1 1
1 1
1 1000000000

Sample Output 2

\-1

There is no way to perform the operation to make grid A match grid B, so print -1.

Sample Input 3

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

Sample Output 3

0

Grid A is already identical to grid B at the beginning.

Sample Input 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

Sample Output 4

20

Solution

具体见文后视频。


Code

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef vector<vector<int>> VVI;int N, M;
VVI A, B;
map<VVI, int> Vis, Dist;int BFS()
{queue<VVI> Q;Q.push(A), Vis[A] = 1;while (Q.size()){auto T = Q.front();Q.pop();if (T == B) return Dist[T];VVI Source = T;for (int i = 1; i < N; i ++){for (int j = 1; j <= M; j ++)swap(T[i][j], T[i + 1][j]);if (!Vis.count(T)) Q.push(T), Vis[T] = 1, Dist[T] = Dist[Source] + 1;T = Source;}for (int j = 1; j < M; j ++){for (int i = 1; i <= N; i ++)swap(T[i][j], T[i][j + 1]);if (!Vis.count(T)) Q.push(T), Vis[T] = 1, Dist[T] = Dist[Source] + 1;T = Source;}}return -1;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N >> M;A.resize(N + 1), B.resize(N + 1);for (int i = 1; i <= N; i ++)A[i].resize(M + 1), B[i].resize(M + 1);for (int i = 1; i <= N; i ++)for (int j = 1; j <= M; j ++)cin >> A[i][j];for (int i = 1; i <= N; i ++)for (int j = 1; j <= M; j ++)cin >> B[i][j];cout << BFS() << endl;return 0;
}

E - Lucky bag

Problem Statement

AtCoder Inc. sells merchandise on its online shop.

There are N N N items remaining in the company. The weight of the i i i-th item ( 1 ≤ i ≤ N ) (1\leq i\leq N) (1iN) is W i W_i Wi.

Takahashi will sell these items as D D D lucky bags.
He wants to minimize the variance of the total weights of the items in the lucky bags.
Here, the variance is defined as V = 1 D ∑ i = 1 D ( x i − x ˉ ) 2 V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2 V=D1i=1D(xixˉ)2, where x 1 , x 2 , … , x D x_1,x_2,\ldots,x_D x1,x2,,xD are the total weights of the items in the lucky bags, and x ˉ = 1 D ( x 1 + x 2 + ⋯ + x D ) \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) xˉ=D1(x1+x2++xD) is the average of x 1 , x 2 , … , x D x_1,x_2,\ldots,x_D x1,x2,,xD.

Find the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
It is acceptable to have empty lucky bags (in which case the total weight of the items in that bag is defined as 0 0 0),
but each item must be in exactly one of the D D D lucky bags.

Constraints

  • 2 ≤ D ≤ N ≤ 15 2 \leq D\leq N\leq 15 2DN15
  • 1 ≤ W i ≤ 1 0 8 1 \leq W_i\leq 10^8 1Wi108
  • All input values are integers.

Input

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

N N N D D D
W 1 W_1 W1 W 2 W_2 W2 … \ldots W N W_N WN

Output

Print the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
Your output will be considered correct if the absolute or relative error from the true value is at most 1 0 − 6 10^{-6} 106.

Sample Input 1

5 3
3 5 3 6 3

Sample Output 1

0.888888888888889

If you put the first and third items in the first lucky bag, the second and fifth items in the second lucky bag, and the fourth item in the third lucky bag, the total weight of the items in the bags are 6 6 6, 8 8 8, and 6 6 6, respectively.

Then, the average weight is 1 3 ( 6 + 8 + 6 ) = 20 3 \frac{1}{3}(6+8+6)=\frac{20}{3} 31(6+8+6)=320,
and the variance is 1 3 { ( 6 − 20 3 ) 2 + ( 8 − 20 3 ) 2 + ( 6 − 20 3 ) 2 } = 8 9 = 0.888888 … \frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots 31{(6320)2+(8320)2+(6320)2}=98=0.888888, which is the minimum.

Note that multiple items may have the same weight, and that each item must be in one of the lucky bags.

Solution

具体见文后视频。


Code

#include <iostream>
#include <cstring>
#define int long longusing namespace std;typedef pair<int, int> PII;const int SIZE = 17;int N, D;
int W[SIZE], Sum[1ll << SIZE];
double F[SIZE][1ll << SIZE];signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N >> D;for (int i = 1; i <= N; i ++)cin >> W[i];for (int i = 0; i < 1 << N; i ++)for (int j = 0; j < N; j ++)if (i >> j & 1)Sum[i] += W[j + 1];double Ave = Sum[(1 << N) - 1] * 1.0 / D;for (int i = 0; i <= D; i ++)for (int j = 0; j < 1 << N; j ++)F[i][j] = 1e18;F[0][0] = 0;for (int i = 1; i <= D; i ++)for (int j = 0; j < 1 << N; j ++)for (int k = j; k; k = k - 1 & j){int Left = j ^ k;if (F[i - 1][Left] == 1e18) continue;F[i][j] = min(F[i][j], F[i - 1][Left] + 1.0 * (Sum[k] * 1.0 - Ave) * (Sum[k] * 1.0 - Ave));}printf("%.8lf\n", F[D][(1 << N) - 1] * 1.0 / D);return 0;
}
}

F - Random Update Query

Problem Statement

You are given an integer 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.

We will perform the following operation on A A A for i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,,M in this order.

  • First, choose an integer between L i L_i Li and R i R_i Ri, inclusive, uniformly at random and denote it as p p p.
  • Then, change the value of A p A_p Ap to the integer X i X_i Xi.

For the final sequence A A A after the above procedure, print the expected value, modulo 998244353 998244353 998244353, of A i A_i Ai for each i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,,N.

How to print expected values modulo 998244353 998244353 998244353

It can be proved that the expected values sought in this problem are always rational. Furthermore, the constraints of this problem guarantee that if each of those expected values is expressed as an irreducible fraction y x \frac{y}{x} xy, then x x x is not divisible by 998244353 998244353 998244353.

Now, there is a unique integer z z z between 0 0 0 and 998244352 998244352 998244352, inclusive, such that x z ≡ y ( m o d 998244353 ) xz \equiv y \pmod{998244353} xzy(mod998244353). Report this z z z.

Constraints

  • All input values are integers.
  • 1 ≤ N , M ≤ 2 × 1 0 5 1 \leq N, M \leq 2 \times 10^5 1N,M2×105
  • 0 ≤ A i ≤ 1 0 9 0 \leq A_i \leq 10^9 0Ai109
  • 1 ≤ L i ≤ R i ≤ N 1 \leq L_i \leq R_i \leq N 1LiRiN
  • 0 ≤ X i ≤ 1 0 9 0 \leq X_i \leq 10^9 0Xi109

Input

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

N N N M M M
A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN
L 1 L_1 L1 R 1 R_1 R1 X 1 X_1 X1
L 2 L_2 L2 R 2 R_2 R2 X 2 X_2 X2
⋮ \vdots
L M L_M LM R M R_M RM X M X_M XM

Output

Print the expected values E i E_i Ei of the final A i A_i Ai for i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,,N in the format below, separated by spaces.

E 1 E_1 E1 E 2 E_2 E2 … \ldots E N E_N EN

Sample Input 1

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

Sample Output 1

499122179 1 665496238 665496236 5

We start from the initial state A = ( 3 , 1 , 4 , 1 , 5 ) A = (3, 1, 4, 1, 5) A=(3,1,4,1,5) and perform the following two operations.

  • The first operation chooses A 1 A_1 A1 or A 2 A_2 A2 uniformly at random, and changes its value to 2 2 2.
  • Then, the second operation chooses one of A 2 , A 3 , A 4 A_2, A_3, A_4 A2,A3,A4 uniformly at random, and changes its value to 0 0 0.

As a result, the expected values of the elements in the final A A A are ( E 1 , E 2 , E 3 , E 4 , E 5 ) = ( 5 2 , 1 , 8 3 , 2 3 , 5 ) (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5) (E1,E2,E3,E4,E5)=(25,1,38,32,5).

Sample Input 2

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

Sample Output 2

5 6

Sample Input 3

20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942

Sample Output 3

821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158

Solution

具体见文后视频。


Code

#include <iostream>
#define int long longusing namespace std;typedef pair<int, int> PII;const int SIZE = 2e5 + 10, MOD = 998244353;int N, M;
int A[SIZE];
struct Node
{int l, r;int sum, add, mul;
}Tree[SIZE * 4];void Pushup(int u)
{Tree[u].sum = (Tree[u << 1].sum + Tree[u << 1 | 1].sum) % MOD;
}void Eval(Node &tree, int add, int mul)
{tree.sum = (tree.sum * mul + (tree.r - tree.l + 1) * add) % MOD;tree.mul = (tree.mul * mul) % MOD;tree.add = (tree.add * mul + add) % MOD;
}void Pushdown(int u)
{Eval(Tree[u << 1], Tree[u].add, Tree[u].mul);Eval(Tree[u << 1 | 1], Tree[u].add, Tree[u].mul);Tree[u].add = 0, Tree[u].mul = 1;
}void Build(int u, int l, int r)
{if (l == r) Tree[u] = {r, r, A[r], 0, 1};else{Tree[u] = {l, r, 0, 0, 1};int mid = l + r >> 1;Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);Pushup(u);}
}void Modify(int u, int l, int r, int add, int mul)
{if (Tree[u].l >= l && Tree[u].r <= r) Eval(Tree[u], add, mul);else{Pushdown(u);int mid = Tree[u].l + Tree[u].r >> 1;if (l <= mid) Modify(u << 1, l, r, add, mul);if (r > mid) Modify(u << 1 | 1, l, r, add, mul);Pushup(u);}
}int Query(int u, int l, int r)
{if (Tree[u].l >= l && Tree[u].r <= r) return Tree[u].sum;Pushdown(u);int mid = Tree[u].l + Tree[u].r >> 1, sum = 0;if (l <= mid) sum = Query(u << 1, l, r);if (r > mid) sum = (sum + Query(u << 1 | 1, l, r)) % MOD;return sum;
}
int qmi(int a, int b, int p)
{int Result = 1;while (b){if (b & 1) Result = Result * a % p;a = a * a % p;b >>= 1;}return Result;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N >> M;for (int i = 1; i <= N; i ++)cin >> A[i];Build(1, 1, N);while (M --){int l, r, x;cin >> l >> r >> x;Modify(1, l, r, 0, (r - l) * qmi(r - l + 1, MOD - 2, MOD) % MOD);Modify(1, l, r, x * qmi(r - l + 1, MOD - 2, MOD) % MOD, 1);}for (int i = 1; i <= N; i ++)cout << Query(1, i, i) % MOD << " ";return 0;
}

视频题解

AtCoder Beginner Contest 332 讲解


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

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

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

相关文章

Crocoddyl: 多接触最优控制的高效多功能框架

系列文章目录 前言 我们介绍了 Crocoddyl&#xff08;Contact RObot COntrol by Differential DYnamic Library&#xff09;&#xff0c;这是一个专为高效多触点优化控制&#xff08;multi-contact optimal control&#xff09;而定制的开源框架。Crocoddyl 可高效计算给定预定…

太阳能供电+4G摄像头搭建EasyCVR鱼塘养殖远程视频监控方案

一、背景需求 随着我国农业的快速发展&#xff0c;以及对新兴技术的应用&#xff0c;养殖业、农牧业、种植业等也面临着全新的挑战与机遇。对鱼塘养殖行业来说&#xff0c;养殖区域面积大、管理难&#xff0c;经常会遇到偷钓者、盗窃鱼苗、非法入侵等监管难题。在国家大力扶持…

2023年OceanBase开发者大会-核心PPT资料下载

一、峰会简介 2023年OceanBase开发者大会主要涵盖了OceanBase的最新技术进展、产品更新以及开发者工具的发布。大会发布了OceanBase 4.1版本&#xff0c;公布了两大友好工具&#xff0c;升级了文档的易用性&#xff0c;并统一了企业版和社区版的代码分支。这些举措全面呈现了O…

基于局域网和广域网训练推理加速策略

一、基于局域网的多GPU上并行训练 TensorFlow 的 MirroredStrategy 和 PyTorch 的 DistributedDataParallel 它们可以帮助您在多个 GPU 或多个计算节点上训练大型模型。这些策略的核心目标是提高训练速度,同时保持模型的准确性和稳定性。 加速的底层原理 TensorFlow 的 Mirro…

一文教会pandas

今天的笔试题令我感触很深&#xff0c;回顾一下之前写的都是低代码想想都。。。 anareport[[reportid,anndt,stockid]].drop_duplicates().rolling(window10,min_periods1).sum().groupby([anndt,stockid])[reportid].count() dfanareport[[reportid,anndt,stockid]].drop_dup…

构建高效持久层:深度解析 MyBatis-Plus(02)

目录 引言1. 逻辑删除1.1 概述1.2 逻辑删除的优势1.3.为什么使用逻辑删除1.4 综合案例 2. 乐观锁和悲观锁2.1.什么是乐观锁和悲观锁2.2.乐观锁和悲观锁的区别2.3.综合案例 3. 分页插件总结 引言 在现代软件开发中&#xff0c;数据库操作是不可或缺的一环。为了提高系统的性能、…

awk统计日志

部分日志文本格式内容 [2023-12-18 22:40:52.301][INFO ][221][com.excellent.common.core.security.filter.UrlRedirectFilter.doFilter:131] ----------------->>> 请求进入拦截器&#xff0c;IP&#xff1a;14.111.199.218, CLIENT&#xff1a;ANDROID&#xff0c…

避坑指南:uni-forms表单在uni-app中的实践经验

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;uni-app篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来JavaScript篇专栏内容:uni-app中forms表单的避坑指南篇 该篇章已被前端圈子收录,点此处进入即可查看更多优质内…

计算机网络考研辨析(后续整理入笔记)

文章目录 体系结构物理层速率辨析交换方式辨析编码调制辨析 链路层链路层功能介质访问控制&#xff08;MAC&#xff09;信道划分控制之——CDMA随机访问控制轮询访问控制 扩展以太网交换机 网络层网络层功能IPv4协议IP地址IP数据报分析ICMP 网络拓扑与转发分析&#xff08;重点…

阶段十-springsecurity总结

jwt认证流程 SpringSecurity 认证过程 第一步&#xff1a; 创建一个类实现UserDetailsService接口&#xff0c;重写其中的方法 通过重写 public UserDetails loadUserByUsername(String username) 方法 从数据库校验用户输入的用户名 配置SecurityConfig Bean注入 Passwor…

类中成员函数及普通函数地址获取方式

文章目录 代码准备测试演示注意事项成员函数类型申明测试演示总的打印输出 代码准备 头文件:Calc.h #pragma onceclass Calc { public:int add(int,int);//函数声明static int sub(int,int);//函数声明 public:int b;//定义了一个成员变量static int a;//声明了一个静态成员变…

SG3524控制的恒流源电路图

SG3524简介 SG3524是开关电源脉宽调制型控制器。应用于开关稳压器&#xff0c;变压器耦合的直流变换器&#xff0c;电压倍增器&#xff0c;极性转换器等。采用固定频率&#xff0c;脉冲宽度调制&#xff08;脉宽调制&#xff09;技术。输出允许单端或推挽输出。芯片电路包括电…

lvs负载均衡

lvs负载均衡群集 群集 特点&#xff1a;由多台主机构成&#xff0c;都干同一件事&#xff0c;对外显示一个整体 企业集群分类 负载均衡群集高可用群集高性能运算群集 负载均衡群集&#xff08;LB&#xff09; 负载均衡的作用 提高应用系统的响应能力可以处理更多的访问请…

Bifrost 中间件 X-Requested-With 系统身份认证绕过漏洞复现

0x01 产品简介 Bifrost是一款面向生产环境的 MySQL,MariaDB,kafka 同步到Redis,MongoDB,ClickHouse等服务的异构中间件 0x02 漏洞概述 Bifrost 中间件 X-Requested-With 存在身份认证绕过漏洞,未经身份认证的攻击者可未授权创建管理员权限账号,可通过删除请求头实现身…

动态加载库

no_mangle 不要改标识符 首先是认识这个标注&#xff1a;mangle&#xff0c;英文的含义“撕裂、碾压”。我第一次把这个单次误以为是manage&#xff0c;说实话两个单词还挺像的。 RUS中函数或静态变量使用#[no_mangle]这个标注属性后&#xff0c;编译器就不会修改它们的名字了…

Home Assistant 如何开启SSH服务

环境&#xff1a; Home Assistant 11.2 SSH & Web Terminal 17.0 问题描述&#xff1a; Home Assistant 如何开启SSH服务 解决方案&#xff1a; 通过添加一个名为Terminal & SSH的插件来在 Home Assistant 中启用 SSH 服务 下面是启用 SSH 服务的大致步骤&#x…

C++第一讲之初入C++

注&#xff1a;本文是对于学完C语言再学C同学的讲解&#xff0c;主要补充C与C语言不同之处&#xff0c;如果你没学过C语言&#xff0c;不建议观看本文。 一.C简介 我们都知道C语言是过程性语言&#xff08;强调的是实现过程&#xff09;&#xff0c;即对计算机语言要处理的两…

计算机组成原理(存储器的校验)

存储器的校验 说到存储器的校验就是海明码&#xff08;汉明码&#xff09;&#xff0c;这种题型有两种&#xff1a; 1.编码问题&#xff1a;根据要传送的二进制代码来确定其对应的海明码&#xff08;汉明码&#xff09; 2.检错问题&#xff1a;已知收到的汉明码&#xff0c;…

【Linux】Linux运维基础

Linux简介&#xff1a; Linux是一个开源的操作系统内核&#xff0c;最初由Linus Torvalds创建。它通常与GNU工具一起使用&#xff0c;以创建一个完整的操作系统。Linux操作系统有许多基于内核的发行版&#xff0c;如Ubuntu、CentOS、Debian等&#xff0c;每个发行版都有其独特的…

深度学习中常见的激活函数

前文介绍 我们在前面了解到了线性回归模型&#xff0c;其实我们可以把线性回归看成一个单个的神经元&#xff0c;它实际上就完成了两个步骤 1.对输入的特征的加权求和 2.将结果通过传递函数&#xff08;或者激活函数&#xff09;输出 这里我们提到了传递函数&#xff08;或者…