This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m, q, a[maxn], p[maxn];
vector<int> V[maxn];
namespace BIT {
int c[maxn];
void add(int p, int v) {
for (; p <= m; p += p & -p) c[p] += v;
}
int query(int p) {
int s = 0;
for (; p; p -= p & -p) s += c[p];
return s;
}
} // namespace BIT
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i], V[a[i]].push_back(i);
}
iota(p + 1, p + m + 1, 1);
long long ans = 0;
for (int i = n; i; i--) {
ans += BIT::query(a[i] - 1), BIT::add(a[i], 1);
}
auto solve = [&](int x, int y) {
static map<pair<int, int>, long long> mp;
if (mp.count({x, y})) return mp[{x, y}];
long long s = 0;
if (V[x].size() < V[y].size()) {
for (int i : V[x]) {
s += V[y].end() - lower_bound(V[y].begin(), V[y].end(), i);
}
} else {
for (int i : V[y]) {
s += lower_bound(V[x].begin(), V[x].end(), i) - V[x].begin();
}
}
return mp[{x, y}] = s;
};
while (q--) {
int k;
cin >> k;
ans += 2 * solve(p[k], p[k + 1]) - 1LL * V[p[k]].size() * V[p[k + 1]].size();
cout << ans << "\n", swap(p[k], p[k + 1]);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |