Official

B - 山寺の札 / Tags at the Mountain Temple Editorial by admin Editorial for printing

swe-2-high

Explanation

Keep the tag arrangement as the string \(S\) itself, and for each query swap the two characters at the given positions. Positions are 1-indexed in the input, so subtract \(1\) to get array indices. When \(a_i = b_i\), swapping an element with itself changes nothing.

The program reads \(S\), performs swap(s[a-1], s[b-1]) for each of the \(Q\) queries, and prints \(S\) at the end. Since each query is exactly a swap of the specified positions, processing all of them yields the correct final arrangement.

Complexity

Each swap is \(O(1)\), giving \(O(N + Q)\) time and \(O(N)\) space to hold the string.

posted:
last update: