公式

E - Set Add Query 解説 by en_translator


Adding naively for each query does not finish within the time limit. In this problem, only the resulting \(A\) is asked, so let us perform addition to the \(i\)-th element in bulk.

For each \(i\), maintain when \(i\) was inserted to \(S\), and perform bulk addition to \(i\) when \(i\) is removed from \(S\).

Suppose that \(i\) was inserted by the \(L_i\)-th query and removed by the \(j\)-th. Then, we want to add the sum of \(|S|\) over the \(L_i\)-th through the \((j-1)\)-th queries. This can be done by managing \(|S|\) after the queries as cumulative sums.

Be careful not to forget to perform additions for the elements in \(S\) after processing the last query.

投稿日時:
最終更新: