:Dynamic Array Range Sum Queries with Adjacent Element Swaps
Dynamic Array Range Sum Queries with Adjacent Element Swaps
Processing dynamic array operations that intermix point modifications with range aggregations requires careful algorithm selection. Consider a problem with two distinct operations: exchanging neighboring elements and computing the sum of a contiguous segment.
A prefix sum approach fails here because each adjacent swap necessitates rebuilding the entire prefix array, resulting in O(n) per operation. With frequent updates, this quickly exceeds time limits.
Segment trees offer an optimal O(log n) per operation solution. The critical observation is that swapping two adjacent elements impacts only their corresponding leaf nodes and the path to the root. The implementation can avoid intricate conditional logic by directly refreshing both modified positions and letting the tree's merge operation handle ancestor updates.
The structure maintains interval sums and supports lazy propagation for future extensions. When swapping elements at position p, we first update the backing array, then recompute the tree values for positions p and p+1 independently. Range queries traverse the tree, combining partial results from relevant segments.
The following implementation encapsulates the logic in a modular class:
#include <bits/stdc++.h>
using namespace std;
struct SegmentTree {
struct Node {
long long sum;
long long lazy;
};
int size;
vector<long long> values;
vector<Node> tree;
SegmentTree(const vector<long long>& input) {
values = input;
size = values.size() - 1;
tree.resize(4 * size);
construct(1, 1, size);
}
void construct(int idx, int left, int right) {
tree[idx].lazy = 0;
if (left == right) {
tree[idx].sum = values[left];
return;
}
int mid = (left + right) / 2;
construct(idx * 2, left, mid);
construct(idx * 2 + 1, mid + 1, right);
merge(idx);
}
void merge(int idx) {
tree[idx].sum = tree[idx * 2].sum + tree[idx * 2 + 1].sum;
}
void apply(int idx, int left, int right, long long delta) {
tree[idx].lazy += delta;
tree[idx].sum += delta * (right - left + 1);
}
void propagate(int idx, int left, int right) {
if (tree[idx].lazy != 0) {
int mid = (left + right) / 2;
apply(idx * 2, left, mid, tree[idx].lazy);
apply(idx * 2 + 1, mid + 1, right, tree[idx].lazy);
tree[idx].lazy = 0;
}
}
long long rangeSum(int L, int R, int idx, int left, int right) {
if (L <= left && right <= R) {
return tree[idx].sum;
}
propagate(idx, left, right);
int mid = (left + right) / 2;
long long total = 0;
if (L <= mid) total += rangeSum(L, R, idx * 2, left, mid);
if (R > mid) total += rangeSum(L, R, idx * 2 + 1, mid + 1, right);
return total;
}
void refresh(int pos, int idx, int left, int right) {
if (left == right) {
tree[idx].sum = values[left];
return;
}
propagate(idx, left, right);
int mid = (left + right) / 2;
if (pos <= mid) refresh(pos, idx * 2, left, mid);
else refresh(pos, idx * 2 + 1, mid + 1, right);
merge(idx);
}
void swapAdjacent(int position) {
swap(values[position], values[position + 1]);
refresh(position, 1, 1, size);
refresh(position + 1, 1, 1, size);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int elementCount, queryCount;
cin >> elementCount >> queryCount;
vector<long long> data(elementCount + 1);
for (int i = 1; i <= elementCount; i++) {
cin >> data[i];
}
SegmentTree segTree(data);
for (int i = 0; i < queryCount; i++) {
int operationType;
cin >> operationType;
if (operationType == 1) {
int swapPos;
cin >> swapPos;
segTree.swapAdjacent(swapPos);
} else {
int leftBound, rightBound;
cin >> leftBound >> rightBound;
cout << segTree.rangeSum(leftBound, rightBound, 1, 1, elementCount) << '\n';
}
}
return 0;
}
This design efficiently separates concerns, proivding logarithmic time complexity for both swap and query operations while keeping the implementation maintainable.