EGOI 2026 Editorial - Watering Plants


Task Author: Harry Zhang

Subtask 1

In this subtask, there is only one day and one query for some resident $i$. We have to output how often resident $i$ comes home before $i-1$. Since there is only one day, this boils down to checking whether $t_i < t_{i-1}$. If yes, the answer is 1, else 0.

Subtask 2 - No ! type queries

Since there are no ! type queries, the relative order of arrival times never changes: if resident $i$ comes home before resident $i-1$ at the beginning, the same holds when we answer any ? query.

So for a given day $j$, the answer will be

Subtask 3 - N = 2

In this subtask, only resident 1 ever waters the plants for resident 0. When iterating through the days, we keep track of a number $w$ (initially 0), the number of times resident 1 watered for resident 0 so far, and we keep track of the current $t_0$ and $t_1$. Each day, we first check whether $t_1 < t_0$ (does resident 1 water for resident 0 today?) and if yes, increment $w$ by one. After that, in case we read an update event, we update $t_0$ or $t_1$. In case we read an update event, we output $w$.

Subtask 4 - $O(ND)$ Solution

For each day, we directly track and update for everyone how many times they have watered the plants for resident below them.

Maintain $w[i]$ for $1 \le i \le N-1$, the number of days resident $i$ has watered resident $i-1$'s plants. We initialize $w[i] = 0$ for all $i$. For each day $j$ from $0$ to $D-1$:

This runs in $\mathcal{O}(N \cdot D)$ time.

Subtask 5

The key observation for this subtask and the full solution is that the watering relationship for pair $(i-1, i)$ only changes when one of those two residents updates their arrival time. Between consecutive such updates, resident $i$ either waters every day or never, so we can process whole time segments between any changes without updating $w[i]$ after every step.

In this subtask, every resident changes their return time at most once. For each resident $i$, save their previous arrival time $prev[i]$ and current arrival time $cur[i]$ (initially both $t_i$) and the time when it changed $change[i]$ (initially $0$). When some resident updates their arrival time, we update $prev[i]$, $cur[i]$ and $change[i]$. To answer a query $i$, we can compute the answer directly.

Let $t' = \min(change[i - 1], change[i])$ denote the time of the earlier change, and let $t'' = \max(change[i - 1], change[i])$ denote the time for the later change. There are three time segments we need to consider:

Summing all numbers gives us the solution.

This runs in $\mathcal{O}(N + D)$ time.

Full Solution

Using a similar approach from the previous subtask, we could accumulate the correct numbers for all time segments between consecutive changes for every ? query. However, this is too slow since return times can now change often. Instead, we now update $w[i]$ continuously whenever one of the following events happen:

  1. There is a query for $i$,
  2. the schedule of resident $i$ changes or
  3. the schedule of resident $i-1$ changes.

For each resident $i$ we keep:

When we need to update some $w[i]$ on some day $d$, this is done in the following way: We check whether currently, resident $i$ comes home before $i-1$ ($t[i] < t[i-1]$). If yes, we add $d - lu[i]$ to $w[i]$ and set $lu[i] = d$.

The solution then works as follows:

When we process an update event ! i t at the end of day $j$, we update both $w[i]$ and $w[i+1]$ as described above, and then adapt the arrival times $t[i]$ accordingly.

When we process a query event ? i at the end of day $j$, we update $w[i]$ and then output the value of $w[i]$ as the result.

This solution runs in $\mathcal{O}(N + D)$ time.