#P16543. [EGOI 2026] 给花浇水 / Watering Plants

[EGOI 2026] 给花浇水 / Watering Plants

Problem Description

In Cesenatico there is a tall building with NN floors, and one resident lives on each floor. The floors are numbered from bottom to top as 00 to N−1N - 1, and resident rr lives on floor rr.

Each floor has a balcony where residents can enjoy the sun and grow some flowers. They can also enjoy the flowers on the balconies below. Since all flowers must be watered every day, everyone decides to help each other. Each resident can water the flowers for the resident living one floor below.

Every morning, all residents leave the building at time 0. Initially, resident rr comes back home at time trt_r. If resident rr comes back strictly earlier than the one living below, that is, tr<tr−1t_r < t_{r - 1}, then resident rr will water the flowers for resident r−1r - 1. (Otherwise, resident r−1r - 1 must water their own flowers.) At the end of each day, one of the following two events happens:

  • Type !: Some resident rr updates their return time, effective starting from the next day.
  • Type ?: Some resident rr asks how many times they have watered the flowers for resident r−1r - 1.

Note that resident 00 will not water for anyone, and the flowers of resident N−1N - 1 will never be watered by anyone else.

Your task is to help the residents answer all queries of type ?.

Input Format

The first line contains two integers NN and DD, representing the number of residents and the number of days to track.

The next line contains NN integers t0,t1,⋯ ,tN−1t_0, t_1, \cdots, t_{N-1}, the initial return times of the residents.

Then there are DD lines. The ii-th line describes the event that happens at the end of day ii.

Each event has one of the following formats:

  • ! r x. Resident rr (0≤r≤N−10 \leq r \leq N-1) will return home at time xx starting from the next day, i.e. the value of trt_r becomes xx. Note that xx may be the same as the current trt_r.
  • ? r. Ask, for resident rr (1≤r≤N−11 \leq r \leq N-1), how many times in total they have watered the flowers for resident r−1r - 1 since day 00.

It is guaranteed that there is at least one ? event.

Output Format

For each ? event, output one line with one integer: the number of times resident rr has watered the flowers for resident r−1r - 1 since day 00.

Note: In this problem, do not count the times a resident waters their own flowers.

3 4
7 7 5
? 2
? 1
? 2
? 2
1
0
3
4
2 5
5 7
! 1 4
? 1
! 0 4
! 1 6
? 1
1
2
4 6
13 9 15 2
! 1 18
? 3
! 0 12
! 2 1
? 1
? 2
2
1
5
3 6
5 2 4
? 1
! 1 8
! 0 10
! 1 3
? 1
? 2
1
4
2

Hint

Sample Explanation

:::align{center}

Sample 1. A watering-can icon means that this resident will water the flowers for the neighbor below. :::

The first sample applies to subtasks 2, 4, 5, and 6. Since the schedule is never updated, resident 22 comes home earlier than resident 11 every day and waters their flowers. After day 00, resident 22 has watered the neighbor once. Since residents 00 and 11 return home at the same time, resident 11 will not water for resident 00. After day 11, resident 11 still has never watered the neighbor. After day 22, resident 22 has watered the neighbor three times. After day 33, resident 22 has watered the neighbor four times.

:::align{center}

Sample 2. :::

The second sample applies to subtasks 3, 4, and 6. On day 00, resident 11 does not water the neighbor. After day 00, resident 11's schedule is updated. Since on day 11 they return home earlier than the neighbor, they water the flowers. After day 11, resident 11 has watered the neighbor once. On day 22, resident 11 waters the neighbor again. After day 44, resident 11 has watered the neighbor a total of two times.

The third sample applies to subtasks 4, 5, and 6. Note that this sample has no illustration.

:::align{center}

Sample 4. :::

The fourth sample applies to subtasks 4 and 6. After day 00, resident 11 watered the neighbor once. After day 44, resident 11 watered the neighbor four times (on days 00, 11, 33, and 44). Resident 22 watered the neighbor a total of two times (on days 22 and 33).

Constraints

  • 2≤N≤200 0002 \leq N \leq 200\ 000.
  • 1≤D≤200 0001 \leq D \leq 200\ 000.
  • 1≤tr≤1091 \leq t_r \leq 10^9 (initially and after each change).

Scoring

Your program will be tested on testdata split into several subtasks. To get the score for a subtask, you must solve all testdata in that subtask correctly.

  • Subtask 00 [00 points]: Samples.
  • Subtask 11 [99 points]: D=1D = 1, i.e. there is only one ? event.
  • Subtask 22 [1212 points]: All events are of type ?.
  • Subtask 33 [1313 points]: N=2N = 2.
  • Subtask 44 [1818 points]: N≤2000N \le 2000 and D≤2000D \le 2000.
  • Subtask 55 [2121 points]: Each resident changes their return time at most once.
  • Subtask 66 [2727 points]: No additional constraints.

Translated by ChatGPT 5