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;
using ll = long long;
int N, K, Q, A[101010], B[101010], T[101010];
vector<int> P[101010]; ll R;
void Add(int x, int v){ for(x+=3; x<101010; x+=x&-x) T[x] += v; }
int Get(int x){ int ret = 0; for(x+=3; x; x-=x&-x) ret += T[x]; return ret; }
map<pair<int,int>, ll> Cache;
ll Swap(int &u, int &v){
auto it = Cache.lower_bound(make_pair(u, v));
if(it != Cache.end() && it->first == make_pair(u, v)) return it->second;
ll res = 0;
if(P[u].size() < P[v].size()){
for(auto i : P[u]){
int cnt = P[v].end() - lower_bound(P[v].begin(), P[v].end(), i);
res += cnt - (P[v].size() - cnt);
}
}
else{
for(auto i : P[v]){
int cnt = lower_bound(P[u].begin(), P[u].end(), i) - P[u].begin();
res += cnt - (P[u].size() - cnt);
}
}
Cache.emplace_hint(it, make_pair(u, v), res);
swap(u, v);
return res;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> K >> Q; iota(B+1, B+K+1, 1);
for(int i=1; i<=N; i++) cin >> A[i], P[A[i]].push_back(i);
for(int i=N; i>=1; i--) R += Get(A[i]-1), Add(A[i], 1);
for(int q=1; q<=Q; q++){
int t; cin >> t;
R += Swap(B[t], B[t+1]);
cout << R << "\n";
}
}
# | 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... |