To solve this problem, we need to efficiently answer multiple range queries about the number of inversion pairs in a sequence. An inversion pair is defined as a pair of indices (i, j) where i < j and a[i] > a[j]. Given the constraints (n up to 10^5 and m up to 500,000), a brute-force approach is infeasible. Instead, we use Mo's algorithm combined with a Fenwick tree (Binary Indexed Tree) to handle the queries efficiently.
Approach
Mo's Algorithm: This algorithm processes range queries in a specific order to minimize the number of adjustments needed when moving from one query to the next. The queries are sorted based on blocks of size √n, which helps in reducing the total number of operations.
Fenwick Tree: This data structure is used to maintain the frequency of elements in the current window. It allows efficient updates and queries for the number of elements greater than a given value, which is crucial for updating the inversion count when adding or removing elements from the current window.
Steps
Reading Input: Read the sequence and the queries.
Sorting Queries: Sort the queries based on Mo's algorithm criteria (block of the left endpoint, and right endpoint within the same block).
Processing Queries: For each query, adjust the current window to match the query's range by expanding or contracting the window. During these adjustments:
Adding an Element: When adding an element to the window, calculate how many elements already in the window are greater than this element (using the Fenwick tree) and add this count to the inversion count.
Removing an Element: When removing an element from the window, calculate how many elements in the window are greater than this element and subtract this count from the inversion count.
Output Results: After processing all queries, output the results in the original order.
Solution Code
cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXN = 100010;
int n, m;
int a[MAXN];
ll ans[MAXN];
int tree[MAXN];
void update(int i, int delta) {
while (i <= n) {
tree[i] += delta;
i += i & -i;
}
}
int query(int i) {
int sum = 0;
while (i) {
sum += tree[i];
i -= i & -i;
}
return sum;
}
struct Query {
int l, r, idx;
};
bool cmp(Query a, Query b) {
int block_a = a.l / sqrt(n);
int block_b = b.l / sqrt(n);
if (block_a != block_b)
return block_a < block_b;
return a.r < b.r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> m;
vector<Query> queries(m);
for (int i = 0; i < m; i++) {
cin >> queries[i].l >> queries[i].r;
queries[i].idx = i;
}
sort(queries.begin(), queries.end(), cmp);
int cur_l = 1, cur_r = 0;
ll current_ans = 0;
for (int i = 0; i < m; i++) {
Query q = queries[i];
while (cur_r < q.r) {
cur_r++;
int x = a[cur_r];
current_ans += (cur_r - cur_l) - query(x);
update(x, 1);
}
while (cur_r > q.r) {
int x = a[cur_r];
update(x, -1);
current_ans -= (cur_r - cur_l) - query(x);
cur_r--;
}
while (cur_l < q.l) {
int x = a[cur_l];
update(x, -1);
current_ans -= query(x) - 1;
cur_l++;
}
while (cur_l > q.l) {
cur_l--;
int x = a[cur_l];
current_ans += query(x);
update(x, 1);
}
ans[q.idx] = current_ans;
}
for (int i = 0; i < m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Explanation
Reading Input: The input consists of the sequence length n, the sequence a, the number of queries m, and each query's range [l, r].
Sorting Queries: Queries are sorted based on Mo's algorithm criteria to minimize the number of adjustments when moving between queries. The left endpoint is divided into blocks of size √n, and within the same block, queries are sorted by the right endpoint.
Processing Queries:
Expanding Right: When moving the right pointer to include a new element, the number of existing elements greater than this new element is calculated using the Fenwick tree. This count is added to the current inversion count.
Contracting Right: When moving the right pointer to exclude an element, the count of elements greater than this element is subtracted from the inversion count.
Expanding Left: When moving the left pointer to include a new element on the left, the count of elements less than this new element (which form inversion pairs with it) is added to the inversion count.
Contracting Left: When moving the left pointer to exclude an element, the count of elements less than this element is subtracted from the inversion count.
Output Results: After processing all queries, the results are output in the original query order.
This approach efficiently handles the range queries by leveraging Mo's algorithm for query ordering and a Fenwick tree for dynamic frequency tracking, ensuring optimal performance even for large input sizes.