线段树求逆序数 HDU 1394 POJ2299

线段树求逆序数模板

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
struct node{
	int l,r,value;
}tree[maxn<<2];
void build(int m,int l,int r)
{
	tree[m].l=l;
	tree[m].r=r;
	tree[m].value=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void updata(int m,int x)
{
	if(tree[m].l==tree[m].r)
	{
		tree[m].value++;
		return;
	}
	int mid=(tree[m].l+tree[m].r)>>1;
	if(x<=mid) updata(m<<1,x);
	else updata(m<<1|1,x);
	tree[m].value++;
}
int query(int m,int l,int r)
{
	if(l>r)return 0;
	if(tree[m].l==l&&tree[m].r==r)
		return tree[m].value;
	int mid=(tree[m].l+tree[m].r)>>1;
	int temp;
	if(r<=mid)temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main()
{
	int n,a[5005];
	while(cin>>n&&n)
	{
		int cnt=0;
		build(1,0,n+1);
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			cnt+=query(1,a[i]+1,n+1);
			updata(1,a[i]);
		}
		cout<<cnt<<endl;
	}
} 

HDU 1394
题意:给你一个有0–n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找出其中最小的一个输出!
思路:先找出原始序列的逆序数,然后对于改变后的序列,因为把序列第一个数拿到了最后,用a[i]代表拿的数,把a[i]放在了最后 则只要比a[i]大的数都与他形成了逆序对,即增加了 n-1+a[i]个逆序对,那么减少了多少呢?减少了比他小的数的个数,即a[i]个。
cnt=cnt-a[i]+(n-1-a[i]);

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
struct node{
	int l,r,value;
}tree[maxn<<2];
void build(int m,int l,int r)
{
	tree[m].l=l;
	tree[m].r=r;
	tree[m].value=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void updata(int m,int x)
{
	if(tree[m].l==tree[m].r)
	{
		tree[m].value++;
		return;
	}
	int mid=(tree[m].l+tree[m].r)>>1;
	if(x<=mid) updata(m<<1,x);
	else updata(m<<1|1,x);
	tree[m].value++;
}
int query(int m,int l,int r)
{
	if(l>r)return 0;
	if(tree[m].l==l&&tree[m].r==r)
		return tree[m].value;
	int mid=(tree[m].l+tree[m].r)>>1;
	int temp;
	if(r<=mid)temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main()
{
	int n,a[5005];
	while(cin>>n&&n)
	{
		int cnt=0;
		build(1,0,n+1);
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			cnt+=query(1,a[i]+1,n+1);
			updata(1,a[i]);
		}
		int minn=cnt;
		for(int i=1;i<=n;i++)
		{
			cnt=cnt-a[i]+(n-1-a[i]);
			minn=min(minn,cnt);
		}
		cout<<minn<<endl;
	}
} 

POJ 2299 离散化
STL

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=5e5+5;
struct node{
	int l,r,value;
}tree[maxn<<2];
int n,m,a[500005],b[500005];
vector<int> v;
int getid(int x) { return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void build(int m,int l,int r)
{
	tree[m].l=l;
	tree[m].r=r;
	tree[m].value=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void updata(int m,int x)
{
	if(tree[m].l==tree[m].r)
	{
		tree[m].value++;
		return;
	}
	int mid=(tree[m].l+tree[m].r)>>1;
	if(x<=mid) updata(m<<1,x);
	else updata(m<<1|1,x);
	tree[m].value++;
}
int query(int m,int l,int r)
{
	if(l>r)return 0;
	if(tree[m].l==l&&tree[m].r==r)
		return tree[m].value;
	int mid=(tree[m].l+tree[m].r)>>1;
	int temp;
	if(r<=mid)temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
		ll cnt=0;
		build(1,0,n+1);
		v.clear();
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			v.push_back(a[i]);
		}
		sort(v.begin(),v.end()),unique(v.begin(),v.end());
    	for(int i=1;i<=n;i++)
       		b[i]=getid(a[i]);
       	int size =v.size();
		for(int i=1;i<=n;i++)
		{
			cnt+=query(1,b[i]+1,size+1);
			updata(1,b[i]);
		}
		cout<<cnt<<endl;
	}
} 

STL有时候会TLE
建议手写

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=5e5+5;
struct node{
	int l,r,value;
}tree[maxn<<2];
int n,m,a[500005],b[500005],save[500005],hashh[500005];
int getid(int x) {return lower_bound(save+1,save+m+1,x)-save;}
void build(int m,int l,int r)
{
	tree[m].l=l;
	tree[m].r=r;
	tree[m].value=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void updata(int m,int x)
{
	if(tree[m].l==tree[m].r)
	{
		tree[m].value++;
		return;
	}
	int mid=(tree[m].l+tree[m].r)>>1;
	if(x<=mid) updata(m<<1,x);
	else updata(m<<1|1,x);
	tree[m].value++;
}
int query(int m,int l,int r)
{
	if(l>r)return 0;
	if(tree[m].l==l&&tree[m].r==r)
		return tree[m].value;
	int mid=(tree[m].l+tree[m].r)>>1;
	int temp;
	if(r<=mid)temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
		ll cnt=0;
		build(1,0,n+1);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			hashh[i]=a[i];
		}
		sort(hashh+1,hashh+n+1);
		m=1;
		save[1]=hashh[1];
        for(int i=2;i<=n;i++) if(hashh[i]!=hashh[i-1]) save[++m]=hashh[i];
        int size=m;
        for(int i=1;i<=n;i++) 
			b[i]=getid(a[i]);
		for(int i=1;i<=n;i++)
		{
			cnt+=query(1,b[i]+1,size+1);
			updata(1,b[i]);
		}
		cout<<cnt<<endl;
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值