Official

C - One Sky, Many Stars Editorial by evima


For simplicity, we identify $x$ with $A$ and $d$ with $B$, and add persons $0, N + 1$ as sentinels. Their parameters are $x_0 = - \infty, x_{N + 1} = \infty, d_0 = d_{N + 1} = 0$.

Consider the case $Q = 1$. We may assume that stars exist only at integer coordinates. Person $i$'s testimony yields the following constraints.

  • At least one of the coordinates $x_i - d_i, x_i + d_i$ has a star.
  • None of the coordinates $x_i - d_i + 1, x_i - d_i + 2, \cdots, x_i + d_i - 1$ has a star.

From this, we can consider the following algorithm for the problem Stargazing.

  • Call a coordinate $x$ a good coordinate if it satisfies the following.
    • $x_j - d_j + 1, x_j - d_j + 2, \cdots, x_j + d_j - 1$ does not include $x$ for every $0 \leq j \leq N + 1$.
  • If, for some $i$ with $1 \leq i \leq N$, neither $x_i - d_i$ nor $x_i + d_i$ is a good coordinate, output No. Otherwise, output Yes.

This runs in $O(N^2)$, so this approach takes $O(N^2Q)$ and would likely result in TLE. In fact, the following algorithm, an improvement of the above, is also correct. (The proof is given later.)

  • Fix $i$ with $1 \leq i \leq N$. Call a coordinate $x$ a good coordinate if it satisfies the following.
    • $x_j - d_j + 1, x_j - d_j + 2, \cdots, x_j + d_j - 1$ does not include $x$ for $j = i - 1, i + 1$.
  • If, for some $i$ with $1 \leq i \leq N$, neither $x_i - d_i$ nor $x_i + d_i$ is a good coordinate, output No. Otherwise, output Yes.

Under this algorithm, if we maintain the set of $i$ with $1 \leq i \leq N$ such that neither $x_i - d_i$ nor $x_i + d_i$ is a good coordinate, then for a single-point change of $d$, we only need to recheck at most three people.

Thus, this problem can be solved in, for example, $O(N + Q)$.

Proof

We prove the correctness of the second algorithm. First, if the true answer is Yes, this algorithm clearly outputs Yes as well, so it suffices to prove that when the true answer is No, this algorithm also outputs No.
Suppose there exists some $i$ such that $x_i - d_i > x_{i + 1} - d_{i + 1}$. In this case, noting that $x_i < x_{i + 1}$, we have $x_i + d_i < x_{i + 1} + d_{i + 1}$. Thus, both coordinates where person $i$'s testimony requires a star cannot have a star due to the constraint by person $i + 1$. Thus, it outputs No.
From this, we may consider only the case where $x_i - d_i$ is in ascending order. By a similar argument, we only need to consider the case where $x_i + d_i$ is also in ascending order.

Consider determining whether $x_i - d_i$ is a good coordinate for person $i$. This amounts to determining whether there exists $j$ such that $x_j - d_j + 1 \leq x_i - d_i \leq x_j + d_j - 1$. If $i < j$, then $x_i - d_i \leq x_j - d_j$, so $x_j - d_j + 1 > x_i - d_i$, which is not applicable. Thus, we only need to consider $j < i$.
If $x_{i - 1} - d_{i - 1} < x_i - d_i$, then for $j < i$ the condition is simply $x_i - d_i \leq x_j + d_j - 1$. The maximum of $x_j + d_j$ over $j < i$ is attained at $j = i - 1$, so it suffices to check only person $i - 1$.
Suppose $x_i - d_i = x_{i - 1} - d_{i - 1} = \cdots = x_{i - k} - d_{i - k} \neq x_{i - (k + 1)} - d_{i - (k + 1)}$. In this case, since $x_{i - k} < x_{i - k + 1} < \cdots < x_i$, $x_j + d_j$ is not a good coordinate for $i - k \leq j \leq i - 1$. Therefore, it is necessary that $x_{i - k} - d_{i - k}$ is a good coordinate, which can be checked by focusing on persons $i - (k + 1)$ and $i - k$.

The same argument applies to $x_i + d_i$, which confirms the correctness.

Sample Implementation (C++)