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;
#define int long long
#define tii tuple<int,int,int>
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
const int maxn = 1e5+5, INF=0x3f3f3f3f, MOD=1e9+7;
int bit[maxn];
map<pii,int> mp;
vector<int> cnt[maxn];
void upd(int x){
for(; x<maxn; x+=x&-x) bit[x]++;
}
int query(int x){
int rt=0;
for(; x>0; x-=x&-x) rt+=bit[x];
return rt;
}
int bbin(int id, int x){
int l=0, r=cnt[id].size()-1;
while(l<=r){
int m = (l+r)/2;
if(cnt[id][m]<x) l=m+1;
else r=m-1;
}
return l;
}
int calc(int a, int b){
int rt=0;
if(cnt[a].size()<=cnt[b].size()){
for(int x : cnt[a]){
rt += bbin(b,x);
}
}
else{
for(int x : cnt[b]){
rt += cnt[a].size() - bbin(a,x);
}
}
return rt;
}
int32_t main(){
int n,k,q; cin >> n >> k >> q;
vector<int> v(n);
for(int i=0; i<n; i++){
cin >> v[i];
cnt[v[i]].push_back(i);
}
int ans=0;
for(int i=0; i<n; i++){
ans += query(k)-query(v[i]);
upd(v[i]);
}
vector<int> target(k+1);
for(int i=1; i<=k; i++) target[i]=i;
while(q--){
int id; cin >> id;
int a=target[id], b=target[id+1];
swap(target[id],target[id+1]);
pii pr = {min(a,b),max(a,b)};
if(mp.find(pr)==mp.end()) mp[pr] = calc(a,b);
int& inv = mp[pr];
int aux = cnt[a].size()*cnt[b].size()-inv;
ans += aux-inv;
inv = aux;
cout << ans << '\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... |