权值线段树用于维护全局值域信息,每个节点记录的是该值域值出现的总次数.
支持查询全局k小值,全局rank,前驱,后继.
例题:
1.Ultra-QuickSort OpenJ_Bailian - 2299
链接
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 – the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
题意:一个长度为n的数组,对它进行冒泡排序,输出每个数需要交换的次数.即逆序对数.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=500005;
struct Tree{
int l,r;
ll sum;
}tree[maxn*4];
int n;
struct node{
ll val,pos;
}pos[maxn];
int x[maxn];
int cmp(node a,node b){
return a.val<b.val;
}
void build(int l,int r,int cnt){
tree[cnt].l=l;tree[cnt].r=r;
if(tree[cnt].l==tree[cnt].r){
tree[cnt].sum=0;
return;
}
int mid=(tree[cnt].l+tree[cnt].r)/2;
build(l,mid,cnt*2);
build(mid+1,r,cnt*2+1);
tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
}
void change(int cnt,int pos){
if(tree[cnt].l==pos&&tree[cnt].r==pos){
tree[cnt].sum=1;
return;
}
int m=(tree[cnt].l+tree[cnt].r)/2;
if(pos<=m)change(2*cnt,pos);
else change(2*cnt+1,pos);
tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
}
ll query(int cnt,int l,int r){
if(l>r)return 0;
if(tree[cnt].l==l&&tree[cnt].r==r){
return tree[cnt].sum;
}
int m=(tree[cnt].l+tree[cnt].r)/2;
if(l>m){
return query(2*cnt+1,l,r);
}
else if(r<=m){
return query(2*cnt,l,r);
}
else{
return query(2*cnt,l,m)+query(2*cnt+1,m+1,r);
}
}
int main(){
while(~scanf("%d",&n)){
if(n==0)break;
build(1,n,1);
for(int i=1;i<=n;i++){
scanf("%d",&pos[i].val);
pos[i].pos=i;
}
sort(pos+1,pos+1+n,cmp);
for(int i=1;i<=n;i++){
x[pos[i].pos]=i;
}
ll res=0;
for(int i=1;i<=n;i++){
res=res+x[i]-query(1,1,x[i]-1)-1;
change(1,x[i]);
}
cout<<res<<endl;
}
return 0;
}
2.Data Structure? HDU - 4217
链接
题意:在一个含有1-n的序列中,每次找到第Ki小的数,并把它删除。
第一个数T,表示测试数据的组数。
每组测试数据,第一行2个整数n和m,m表示操作的轮数。
接下来m行,每行一个整数k,表示要找出第k小的数,并把它删除。
每组数据,输出一个整数,表示删除元素的总和。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=300005;
struct node{
int l,r,sum;
}tree[maxn*4];
void build(int cnt,int l,int r){
tree[cnt].l=l;tree[cnt].r=r;
if(tree[cnt].l==tree[cnt].r){
tree[cnt].sum=1;
return;
}
int mid=(l+r)>>1;
build(cnt<<1,l,mid);
build(cnt<<1|1,mid+1,r);
tree[cnt].sum=tree[cnt<<1].sum+tree[cnt<<1|1].sum;
}
int query(int cnt,int k){
if(tree[cnt].l==tree[cnt].r)return tree[cnt].l;
int mid=(tree[cnt].l+tree[cnt].r)>>1;
if(tree[cnt<<1].sum>=k)return query(cnt<<1,k);
else return query(cnt<<1|1,k-tree[cnt<<1].sum);
}
void change(int x,int val,int cnt){
if(tree[cnt].l==tree[cnt].r){
tree[cnt].sum=val;
return ;
}
if(x<=tree[cnt*2].r)change(x,val,cnt*2);
else change(x,val,cnt*2+1);
tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
return ;
}
int main(){
int t,n,m;
scanf("%d",&t);
int cas=1;
while(t--){
scanf("%d%d",&n,&m);
build(1,1,n);
int k;
long long ans=0;
while(m--){
cin>>k;
int c=query(1,k);
ans+=c;
change(c,0,1);
}
printf("Case %d: ",cas++);
printf("%lld\n",ans);
}
return 0;
}