bzoj 2212: [Poi2011]Tree Rotations (线段树)

2212: [Poi2011]Tree Rotations

Time Limit: 20 Sec   Memory Limit: 259 MB
Submit: 817   Solved: 320
[ Submit][ Status][ Discuss]

Description

Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features: The tree consists of straight branches, bifurcations and leaves. The trunk stemming from the ground is also a branch. Each branch ends with either a bifurcation or a leaf on its top end. Exactly two branches fork out from a bifurcation at the end of a branch - the left branch and the right branch. Each leaf of the tree is labelled with an integer from the range . The labels of leaves are unique. With some gardening work, a so called rotation can be performed on any bifurcation, swapping the left and right branches that fork out of it. The corona of the tree is the sequence of integers obtained by reading the leaves' labels from left to right. Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs(I,j), (1< = I < j < = N ) such that(Ai>Aj) in the corona(A1,A2,A3…An).  The original tree (on the left) with corona(3,1,2) has two inversions. A single rotation gives a tree (on the right) with corona(1,3,2), which has only one inversion. Each of these two trees has 5 branches. Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.

现在有一棵二叉树,所有非叶子节点都有两个孩子。在每个叶子节点上有一个权值(有n个叶子节点,满足这些权值为1..n的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。

Input

In the first line of the standard input there is a single integer (2< = N < = 200000) that denotes the number of leaves in Byteasar's tree. Next, the description of the tree follows. The tree is defined recursively: if there is a leaf labelled with ()(1<=P<=N) at the end of the trunk (i.e., the branch from which the tree stems), then the tree's description consists of a single line containing a single integer , if there is a bifurcation at the end of the trunk, then the tree's description consists of three parts: the first line holds a single number , then the description of the left subtree follows (as if the left branch forking out of the bifurcation was its trunk), and finally the description of the right subtree follows (as if the right branch forking out of the bifurcation was its trunk).

第一行n
下面每行,一个数x
如果x==0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,
如果x!=0,表示这个节点是叶子节点,权值为x

1<=n<=200000

Output

In the first and only line of the standard output a single integer is to be printed: the minimum number of inversions in the corona of the input tree that can be obtained by a sequence of rotations.

一行,最少逆序对个数

Sample Input

3
0
0
3
1
2

Sample Output

1

HINT

Source

题解:线段树

刚开始写了一个O(nlog^2n)的做法,结果TLE了。

这个其实好写,因为没到达应该区间我们需要根据跨越左右子树的逆序对数,来判断是否需要交换。

一个子树的答案=他左子树的答案+右子树的答案+跨左右子树的答案。

统计跨左右子树的答案,直接将左子树的加入权值树状数组,然后右子树的查询权值比他大的数量即可。

因为树高为log,那么每个数都最多会被加入权值树状数组logn次。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 400003
#define LL long long
using namespace std;
int n,m,val[N];
int l[N],r[N],maxn,num[N],cnt,in[N],out[N],q[N],sz,root;
LL tr[N];
int dfs()
{
	int now=++cnt;
	scanf("%d",&val[now]);
	if (val[now]) {
		q[++sz]=val[now];
		in[now]=out[now]=sz;
		return now;
	}
	in[now]=sz+1;
	l[now]=dfs(); r[now]=dfs();
	out[now]=sz;
	return now;
}
int lowbit(int x)
{
	return x&(-x);
}
void change(int x,LL v)
{
	for (int i=x;i<=n;i+=lowbit(i))
	 tr[i]+=v;
}
LL sum(int x)
{
	LL ans=0;
	for (int i=x;i>=1;i-=lowbit(i))
	  ans+=tr[i];
	return ans;
}
LL solve(int x)
{
	LL ans=0;
	if (val[x]) return 0;
	ans=solve(l[x])+solve(r[x]);
	int ll=in[l[x]]; int rr=out[l[x]]; int len1=rr-ll+1;
	for (int i=ll;i<=rr;i++) change(q[i],1);
    LL t=0;
    ll=in[r[x]]; rr=out[r[x]]; int len2=rr-ll+1;
    for (int i=ll;i<=rr;i++) t+=sum(q[i]);
    for (int i=in[l[x]];i<=out[l[x]];i++) change(q[i],-1);
    ans+=min(t,(LL)len1*(LL)len2-t);
    return ans;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d",&n);
	root=dfs();
    printf("%I64d\n",solve(root));
}


把这个不科学的思路扔掉,我们考虑O(nlogn)的做法。

这个题的关键就是统计跨子树的逆序对.因为左右子树中的顺序对于跨子树的统计是没有关系的,所以每个位置统计跨子树的答案都是一个独立的状态。

统计逆序对不用树状数组,不用归并排序还能怎么搞?我们建立权值线段树,对于一个权值区间[l,r],能产生多少逆序对,我们以不交换左右子树为例答案就是左子树[mid+1,r]的个数*右子树[l,mid]的个数,然后加上处理[l,mid],[mid+1,r]的答案,可以递归计算。

刚开始我们可以对于每一个叶子节点建立权值线段树(动态开点),因为我们是从下往上不断记录答案的,所有我们再处理完左右子树中,就要将左右子树权值线段树的信息合并给他们的父亲,然后一直合并上去。

因为每个点只会被合并一次,所有时间复杂度是O(nlogn)

过程中顺便计算答案即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 400003
#define LL long long
using namespace std;
int n,m,val[N],rt,ls[N*20],rs[N*20],tot;
int l[N],r[N],maxn,num[N],cnt,in[N],out[N],q[N],sz,root[N];
LL sl,sr,sum[N*20];
int dfs()
{
	int now=++cnt;
	scanf("%d",&val[now]);
	if (val[now]) {
		q[++sz]=val[now];
		in[now]=out[now]=sz;
		return now;
	}
	in[now]=sz+1;
	l[now]=dfs(); r[now]=dfs();
	out[now]=sz;
	return now;
}
void update(int x)
{
	sum[x]=sum[ls[x]]+sum[rs[x]];
}
void insert(int &i,int l,int r,int x)
{
	i=++tot;
	if (l==r) {
		sum[i]=1; 
		return;
	}
	int mid=(l+r)/2;
	if (x<=mid) insert(ls[i],l,mid,x);
	else insert(rs[i],mid+1,r,x);
	update(i);
}
int merge(int x,int y)
{
	if (!x) return y;
	if (!y) return x;
	sl+=sum[rs[x]]*sum[ls[y]];
	sr+=sum[ls[x]]*sum[rs[y]];
	ls[x]=merge(ls[x],ls[y]);
	rs[x]=merge(rs[x],rs[y]);
	update(x);
	return x;
}
LL solve(int x)
{
	if (val[x]) return 0;
	LL ans=solve(l[x])+solve(r[x]);
	sl=0; sr=0;
	root[x]=merge(root[l[x]],root[r[x]]);
	return ans+min(sl,sr);
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d",&n);
	rt=dfs();
	for (int i=1;i<=cnt;i++)
	 if (val[i]) insert(root[i],1,n,val[i]);
    printf("%I64d\n",solve(rt));
}




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值