公式

D - Roadway 解説 by evima


Assign a potential \(v_j\) to each vertex \(j\) and set the road strength as \(w_j = v_{j+1}-v_j\).

First, consider the case where every person satisfies \(S_i < T_i\).

Here, person \(i\) requires \(v_{S_i}=v_{T_i}\) and \(v_j > v_{S_i}\) for all \(j=S_i+1,\dots,T_i-1\).

Let interval \(i\) be \((S_i,T_i)\). There exists a potential assignment that satisfies all requirements if and only if one of the following conditions is satisfied for every \((i,j)\,(1\le i < j \le M)\):

  • The intervals \(i\) and \(j\) are disjoint. That is, \(T_i\le S_j\) or \(T_j\le S_i\).
  • One of the intervals \(i\) and \(j\) is strictly contained in the other, and neither their left nor right endpoints coincide. That is, \(S_i<S_j<T_j<T_i\) or \(S_j<S_i<T_i<T_j\).

Let us call a set of intervals satisfying this laminar.

Now, consider the general case.

If \(S_i > T_i\), person \(i\) requires \(v_{T_i}=v_{S_i}\) and \(v_j < v_{T_i}\) for all \(j=T_i+1,\dots,S_i-1\).

Let us derive necessary conditions for there to exist a potential assignment that satisfies all requirements.

The first necessary condition is:

  • \(S_i\neq T_j\) and \(S_j\neq T_i\) for any \((i,j)\) with \(S_i<T_i\) and \(T_j<S_j\).

Otherwise, the signs of edge strengths at the ends would conflict.

Next, the second necessary condition is:

  • The set of intervals \([S_i,T_i]\) for persons \(i\) with \(S_i<T_i\) is laminar.

The third necessary condition is:

  • The set of intervals \([T_i,S_i]\) for persons \(i\) with \(T_i<S_i\) is laminar.

When these three conditions hold, a suitable potential assignment always exists (proof sketch below).

Thus, to process the queries, you can use the two‑pointer method to find, for \(L_k=1,2,\dots,M\) in this order, the maximal \(R_k\) that satisfies the conditions.

The first condition is easy to check. For the second and third conditions, we need to perform insertions and removals of intervals to a set and test whether the current set is laminar; this can be done with Zobrist hashing. To compute the XOR of an interval, we may use a BIT. The total complexity is \(O((N+M)\log N+Q)\).

Below is a sketch of the proof.

Consider a directed graph with \(N\) vertices and \(M\) edges, where edge \(i\) is directed from \(S_i\) to \(T_i\).

Under the three conditions, every connected component of this graph (viewed as undirected) is a path whose vertices appear in ascending order of their indices.

Let us order these paths. For a path with leftmost vertex \(l\) and rightmost vertex \(r\), call \([l,r]\) its coverage.

Paths with disjoint coverages impose no constraints on the magnitude relationship between their potentials.

For two paths \(i\) and \(j\) with overlapping coverages, we may assume \(l_i<l_j\le r_i\).

If path \(i\) contains an edge \(S\rightarrow T\) with \(S<l_j<T\), the magnitude relationship between the potentials of paths \(i\) and \(j\) must be \(i<j\).

Here, from the second and third conditions, for vertex \(x_j\) of path \(j\), there is no edge \(T\rightarrow S\) in path \(i\) with \(T<x_j<S\). Similarly, for vertex \(x_i\) of path \(i\), there is no edge \(S\rightarrow T\) in path \(j\) with \(S<x_i<T\). Thus, the magnitude relationship between the potentials of paths \(i\) and \(j\) is uniquely determined.

On the other hand, if path \(i\) contains an edge \(T\rightarrow S\) with \(T<l_j<S\), the magnitude relationship between the potentials of paths \(i\) and \(j\) must be \(i>j\). Again in this case, a similar argument shows that the magnitude relationship between the potentials of paths \(i\) and \(j\) is uniquely determined.

The graph resulting from these magnitude relationships is a DAG.

投稿日時:
最終更新: