公式

E - 403 Forbidden 解説 by en_translator


If there are only queries \(1\) and \(3\)

For each user \(X\), let \(S_X\) be the set of contest pages that user \(X\) can view. For query \(1\), add \(Y\) to \(S_X\). For query \(3\), check if \(S_X\) contains \(Y\).

Since \(S_X\) can have up to \(M\) elements, if we manage \(S_X\) as an array, checking if \(S_X\) contains \(Y\) costs \(O(M)\) time per query. Thus, the time complexity is \(O(QM)\), which is infeasible within the execution time limit.

Instead, we need to use a data structure that can efficiently manage a set. Most programming languages provide with such a data structure as a standard feature; in C++ and Python, we may use set. If you use a set, one can process queries \(1\) and \(3\) in \(O(\log M)\) time per query. The overall time complexity is \(O(Q \log M)\), which is fast enough.

If there are also query \(2\).

In query \(2\), if you add every values from \(1\) through \(M\) to \(S_X\), processing such a query costs \(O(M \log M)\) time, which is inefficient. Instead, we maintain a flag \(f_X\) signifying whether user \(X\) can view all the pages. Then, processing query \(2\) can be done merely by setting \(f_X\) to \(\mathrm{true}\), which costs \(O(1)\) time per query.

In this case, query \(3\) has to be processed as follows:

  • If \(f_X=\mathrm{true}\), then user \(X\) can view all the contest pages, so print Yes.
  • Otherwise, print Yes if \(S_X\) contains \(Y\) and No otherwise.

Sample code (C++)

Sample code (Python)

投稿日時:
最終更新: