#P8518. [IOI 2021] 分糖果

[IOI 2021] 分糖果

Description

Aunt Khong is preparing nn boxes of candies for students at a nearby school. The boxes are numbered from 00 to n1n - 1, and initially all boxes are empty. Box ii (0in1)(0 \leq i \leq n - 1) can hold at most c[i]c[i] candies (its capacity is c[i]c[i]).

Aunt Khong spends qq days preparing the candy boxes. On day jj (0jq1)(0 \leq j \leq q - 1), she performs an operation based on three integers l[j]l[j], r[j]r[j], and v[j]v[j], where 0l[j]r[j]n10 \leq l[j] \leq r[j] \leq n - 1 and v[j]0v[j] \neq 0. For every box kk such that l[j]kr[j]l[j] \leq k \leq r[j]:

  • If v[j]>0v[j] > 0, Aunt Khong puts candies into box kk one by one until she has put exactly v[j]v[j] candies or the box becomes full. That is, if the box contains pp candies before this operation, then after this operation it will contain min(c[k],p+v[j])\min(c[k], p + v[j]) candies.

  • If v[j]<0v[j] < 0, Aunt Khong takes candies out of box kk one by one until she has taken out exactly v[j]-v[j] candies or the box becomes empty. That is, if the box contains pp candies before this operation, then after this operation it will contain max(0,p+v[j])\max(0, p + v[j]) candies.

Your task is to compute the number of candies in each box after qq days.

Input Format

Implementation details

You need to implement the following function:

std::vector<int> distribute_candies(
  	std::vector<int> c, std::vector<int> l, 
  	std::vector<int> r, std::vector<int> v)
  • cc: an array of length nn. For 0in10 \leq i \leq n - 1, c[i]c[i] is the capacity of box ii.

  • ll, rr, and vv: three arrays of length qq. On day jj, for 0jq10 \leq j \leq q - 1, Aunt Khong performs the operation determined by integers l[j]l[j], r[j]r[j], and v[j]v[j], as described in the statement.

Output Format

  • The function should return an array of length nn. Let this array be ss. For 0in10 \leq i \leq n - 1, s[i]s[i] should be the number of candies in box ii after qq days.
3
10 15 13
2
0 2 20
0 1 -11

0 4 13

Hint

Example 1

Consider the following call: distribute_candies([10, 15, 13], [0, 0], [2, 1], [20, -11])

This means box 00 has capacity 1010, box 11 has capacity 1515, and box 22 has capacity 1313.

At the end of day 00, box 00 has min(c[0],0+v[0])=10\min(c[0], 0 + v[0]) = 10 candies, box 11 has min(c[1],0+v[0])=15\min(c[1], 0 + v[0]) = 15 candies, and box 22 has min(c[2],0+v[0])=13\min(c[2], 0 + v[0]) = 13 candies.

At the end of day 11, box 00 has max(0,10+v[1])=0\max(0, 10 + v[1]) = 0 candies, and box 11 has max(0,15+v[1])=4\max(0, 15 + v[1]) = 4 candies. Since 2>r[1]2 > r[1], the number of candies in box 22 does not change. The summary at the end of each day is as follows:

Day Box 00 Box 11 Box 22
00 1010 1515 1313
11 00 44

In this case, the function should return [0,4,13][0, 4, 13].

Constraints

  • 1n2000001 \le n \le 200 000

  • 1q2000001 \le q \le 200 000

  • 1c[i]1091 \le c[i] \le 10 ^ 9 (for all 0in10 \le i \le n - 1

  • 0l[j]r[j]n10 \le l[j] \le r[j] \le n - 1 (for all 0jq10 \le j \le q - 1)

  • 109v[j]109−10 ^ 9 \le v[j] \le 10 ^ 9, v[j]0v[j] \neq 0 (for all 0jq10 \le j \le q - 1)

Subtasks

  1. (33 points) n,q2000n, q \leq 2000.
  2. (88 points) v[j]>0v[j] > 0 (for all 0jq10 \leq j \leq q - 1).
  3. (2727 points) c[0]=c[1]==c[n1]c[0] = c[1] = \cdots = c[n - 1].
  4. (2929 points) l[j]=0l[j] = 0 and r[j]=n1r[j] = n - 1 (for all 0jq10 \leq j \leq q - 1).
  5. (3333 points) No additional constraints.

Sample grader

The sample grader reads the input in the following format:

  • Line 11: nn.
  • Line 22: c[0] c[1]  c[n1]c[0] ~ c[1] ~ \cdots ~ c[n - 1].
  • Line 33: qq.
  • Line 4+j4 + j (0jq10 \leq j \leq q - 1): l[j] r[j] v[j]l[j] ~ r[j] ~ v[j].

The sample grader prints your answer in the following format:

  • Line 11: s[0] s[1]  s[n1]s[0] ~ s[1] ~ \cdots ~ s[n - 1].

Translated by ChatGPT 5