There are two keys to solving the problem swap and sum :
- Treap, which enables O(log N) time for swapping two segments and summing over one segment, because we could attach an addition field 'sum' to each node of Treap. However in this problem there is O(N) element swaps per request so the issue isn't resolved yet.
- Coordinate transform. This will deal with O(N) element swap. Transform the array into all even-indexed elements followed by all odd-indexed elements, i.e. a, a, a, ... , a, a, ...
With this, any O(N) swap becomes swapping two segments of same length in the transformed array, and every summation splits to two which is still managable.