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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e5 + 10;
struct fen{
vector <int> f;
int N;
void ass(int n) {
N = n;
f.assign(n + 1, 0);
}
void upd(int i, int x) {
for (; i; i -= i & (-i))
f[i] += x;
}
int get(int i) {
int s = 0;
for (; i <= N; i += i & (-i))
s += f[i];
return s;
}
};
vector <int> pos[N];
int perm[N];
map <pair <int, int>, ll> mp;
ll cal(int x, int y) {
if (sz(pos[x]) > sz(pos[y]))
return sz(pos[x]) * sz(pos[y]) - cal(y, x);
if (mp.find({x, y}) != mp.end())
return mp[{x, y}];
ll ans = 0;
for (auto t : pos[x]) {
int p = lower_bound(all(pos[y]), t) - pos[y].begin();
ans += p;
}
return mp[{x, y}] = ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, q;
cin >> n >> k >> q;
fen f;
f.ass(k + 1);
ll num = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
pos[x].pb(i);
num += f.get(x + 1);
f.upd(x, 1);
}
for (int i = 1; i <= k; i++)
perm[i] = i;
for (int i = 0; i < q; i++) {
int x;
cin >> x;
ll tot = sz(pos[perm[x]]) * sz(pos[perm[x + 1]]);
ll t = cal(perm[x], perm[x + 1]);
swap(perm[x], perm[x + 1]);
num += (tot - t - t);
cout << num << '\n';
}
return 0;
}
/*
5 4 3
1 4 2 1 2
3
2
1
*/
# | 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... |