E. K Integers 题解(线段树+思维)
题目链接
题目思路
假设\(1-k\)都挨在一起,那么答案不就是逆序对嘛
但是要先把他们挨在一起
怎么挨在一起呢,显然是要放在中位数最合适
权值线段树上二分就可以确定中位数了
然后再乱求求就行了
代码
#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<long long,long long >
//typedef pair<long long,long long > pii
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n;
int a[maxn],pos[maxn];
ll ni=0;
ll tree[maxn<<2][2];
void push(int node){
tree[node][0]=tree[node<<1][0]+tree[node<<1|1][0];
tree[node][1]=tree[node<<1][1]+tree[node<<1|1][1];
}
void update(int node,int pos,int l,int r){
if(l==r){
tree[node][0]++;
tree[node][1]=l;
return ;
}
int mid=(l+r)/2;
if(mid>=pos) update(node<<1,pos,l,mid);
else update(node<<1|1,pos,mid+1,r);
push(node);
}
int serch(int node,int l,int r,int need){
if(l==r){
return l;
}
int mid=(l+r)/2;
if(tree[node<<1][0]>=need) return serch(node<<1,l,mid,need);
else return serch(node<<1|1,mid+1,r,need-tree[node<<1][0]);
}
ll query(int node,int L,int R,int l,int r,int op){
if(L>R) return 0;
if(L<=l&&r<=R){
return tree[node][op];
}
int mid=(l+r)/2;
ll sum=0;
if(mid>=L) sum+=query(node<<1,L,R,l,mid,op);
if(mid<R) sum+=query(node<<1|1,L,R,mid+1,r,op);
return sum;
}
signed main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=1;i<=n;i++){
ni+=query(1,pos[i]+1,n,1,n,0);
update(1,pos[i],1,n);
ll mid=serch(1,1,n,(i+1)/2);
ll num1=query(1,1,mid-1,1,n,0);
ll num2=query(1,mid+1,n,1,n,0);
ll sum1=query(1,1,mid-1,1,n,1);
ll sum2=query(1,mid+1,n,1,n,1);
ll tmp1=(mid-1-num1+1 + mid-1)*num1/2-sum1;
ll tmp2=sum2-(mid+1 + mid+1+num2-1)*num2/2;
printf("%lld ",ni+tmp1+tmp2);
}
return 0;
}
不摆烂了,写题