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 i1. Since there is only one day, this boils down to checking whether ti<ti1. 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 i1 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 t0 and t1. Each day, we first check whether t1<t0 (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 t0 or t1. 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 1iN1, the number of days resident i has watered resident i1's plants. We initialize w[i]=0 for all i. For each day j from 0 to D1:

This runs in O(ND) time.

Subtask 5

The key observation for this subtask and the full solution is that the watering relationship for pair (i1,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 ti) 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[i1],change[i]) denote the time of the earlier change, and let t=max(change[i1],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 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 i1 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 i1 (t[i]<t[i1]). If yes, we add dlu[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 O(N+D) time.