#P16543. [EGOI 2026] 给花浇水 / Watering Plants
[EGOI 2026] 给花浇水 / Watering Plants
Problem Description
In Cesenatico there is a tall building with floors, and one resident lives on each floor. The floors are numbered from bottom to top as to , and resident lives on floor .
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 comes back home at time . If resident comes back strictly earlier than the one living below, that is, , then resident will water the flowers for resident . (Otherwise, resident must water their own flowers.) At the end of each day, one of the following two events happens:
- Type
!: Some resident updates their return time, effective starting from the next day. - Type
?: Some resident asks how many times they have watered the flowers for resident .
Note that resident will not water for anyone, and the flowers of resident 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 and , representing the number of residents and the number of days to track.
The next line contains integers , the initial return times of the residents.
Then there are lines. The -th line describes the event that happens at the end of day .
Each event has one of the following formats:
! r x. Resident () will return home at time starting from the next day, i.e. the value of becomes . Note that may be the same as the current .? r. Ask, for resident (), how many times in total they have watered the flowers for resident since day .
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 has watered the flowers for resident since day .
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 comes home earlier than resident every day and waters their flowers. After day , resident has watered the neighbor once. Since residents and return home at the same time, resident will not water for resident . After day , resident still has never watered the neighbor. After day , resident has watered the neighbor three times. After day , resident has watered the neighbor four times.
:::align{center}

Sample 2. :::
The second sample applies to subtasks 3, 4, and 6. On day , resident does not water the neighbor. After day , resident 's schedule is updated. Since on day they return home earlier than the neighbor, they water the flowers. After day , resident has watered the neighbor once. On day , resident waters the neighbor again. After day , resident 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 , resident watered the neighbor once. After day , resident watered the neighbor four times (on days , , , and ). Resident watered the neighbor a total of two times (on days and ).
Constraints
- .
- .
- (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 [ points]: Samples.
- Subtask [ points]: , i.e. there is only one
?event. - Subtask [ points]: All events are of type
?. - Subtask [ points]: .
- Subtask [ points]: and .
- Subtask [ points]: Each resident changes their return time at most once.
- Subtask [ points]: No additional constraints.
Translated by ChatGPT 5