이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template <typename T>
int sz(const T &a){return int(a.size());}
const int MN=1e5+1,MB=100;
int arr[MN];
vector<int> locs[MN];
vector<ll> pc[MN];
int ind[MN];
int ord[MN];
ll bit[MN];
int n,k,q;
void update(int loc, ll val){
for(;loc<=k;loc+=loc&-loc)bit[loc]+=val;
}
ll query(int loc){
ll res=0;
for(;loc>0;loc-=loc&-loc)res+=bit[loc];
return res;
}
int main(int argc, char* argv[]){//bubble sort, count inversions. maintain number of adjacent inversions? sqrt is possible..
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin>>n>>k>>q;
ll tot=0;
for(int i=1;i<=n;i++){
cin>>arr[i];
tot+=query(k)-query(arr[i]);
update(arr[i],1);
locs[arr[i]].push_back(i);
}
int am=0;
for(int i=1;i<=k;i++){
ord[i]=i;
if(sz(locs[i])>=MB){
ind[i]=am++;
for(int j=1;j<=k;j++)pc[j].push_back(0);
int cnt=0;
for(int j=1;j<=n;j++){
if(arr[j]==i)cnt++;
else{
pc[arr[j]].back()+=cnt;
}
}
}
}
auto calc=[&](int i){
int cur=ord[i],pre=ord[i-1];
ll res=0;
if(sz(locs[cur])>=MB){
res+=pc[pre][ind[cur]];
}
else if(sz(locs[pre])>=MB){
res+=ll(sz(locs[pre]))*ll(sz(locs[cur]))-pc[cur][ind[pre]];
}
else{
int ptr=0;
for(auto x:locs[cur]){
while(ptr!=sz(locs[pre])&&locs[pre][ptr]<=x)ptr++;
res+=sz(locs[pre])-ptr;
}
}
return res;
};
while(q--){
int a;
cin>>a;
tot-=calc(a+1);
swap(ord[a+1],ord[a]);
tot+=calc(a+1);
printf("%lli\n",tot);
}
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... |