bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations
- 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内的逆序对数,跨越 \(ls,rs\) 的逆序对数三者之和.
- 交换 \(ls,rs\) 显然对前两种的答案没有影响,只需最大化最后一种答案.
- 对每个叶子节点开一棵权值线段树向上合并,选取权值中点 \(mid\) 划分开,那么两种情况在当前层产生的贡献即为 \(ls\) 的左子树大小 \(\times\) \(rs\) 的右子树大小,\(ls\) 的右子树大小 \(\times\) \(rs\) 的左子树大小中的最大值.
- 继续递归合并,就可以计算到所有贡献.
- 时间复杂度为 \(O(nlogn)\) .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=2e5+10;
ll ans=0,ans1,ans2;
struct node{
int siz,ls,rs;
}Tree[MAXN*30];
#define root Tree[o]
#define lson Tree[root.ls]
#define rson Tree[root.rs]
#define t(x) Tree[x]
int n,cnt=0;
void update(int &o,int l,int r,int pos)
{
if(!o)
o=++cnt;
++root.siz;
if(l==r)
return;
int mid=(l+r)>>1;
if(pos<=mid)
update(root.ls,l,mid,pos);
else
update(root.rs,mid+1,r,pos);
}
int merge(int ls,int rs)
{
if(!ls || !rs)
return ls+rs;
t(ls).siz+=t(rs).siz;
ans1+=1LL*(Tree[t(ls).ls].siz)*(Tree[t(rs).rs].siz);
ans2+=1LL*(Tree[t(ls).rs].siz)*(Tree[t(rs).ls].siz);
t(ls).ls=merge(t(ls).ls,t(rs).ls);
t(ls).rs=merge(t(ls).rs,t(rs).rs);
return ls;
}
int solve()
{
int p=read(),x=0;
if(!p)
{
int ls=solve();
int rs=solve();
ans1=ans2=0;
x=merge(ls,rs);
ans+=min(ans1,ans2);
return x;
}
else
update(x,1,n,p);
return x;
}
int main()
{
n=read();
solve();
cout<<ans<<endl;
return 0;
}
bzoj 2212 Tree Rotations的更多相关文章
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- bzoj 2212: [Poi2011]Tree Rotations
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
随机推荐
- MonkeyScript使用教程
原文地址https://www.cnblogs.com/yizhou-xu/p/8072813.html 原文地址https://www.cnblogs.com/YatHo/p/7205162.htm ...
- 什么是API测试
什么是API API是Application Programming Interface的简写. 实现了两个或多个独立系统或模块间的通信和数据交换能力. 什么是API测试 图片.png API测试是不 ...
- jQuery获取属性
jQuery在获取jQuery对象的属性时,出现attr()获取不到的情况,此时,请使用prop()获取 如下为经常用到的: var oHtml=$(this).prop("outerHTM ...
- 图解Kerberos认证工作原理
本文是我在看了这篇英文说明之后的总结 https://technet.microsoft.com/zh-cn/library/cc961976.aspx 是总结,不是翻译,所以是我看后按自己的理解写的 ...
- Python 安装pytz
1. https://pypi.org/project/pytz/#files 2. 下载上图标黄的文件, 3. pip install 4. from pytz import ...
- MySQL connector c++使用笔记
MySQL的connector官方地址: http://dev.mysql.com/downloads/connector/ 针对c++来说, 可以选择c或者c++的库. c++的实现是参考了java ...
- python webdriver grid多节点运行webdriver程序
grid整理: 机制 Hub机器和节点机器上要装jdk和jar包 A机器:hub 中控:用来监控所有节点机的状态 启动命令: java -jar selenium-server-standalone ...
- windows下利用批处理脚本监控程序
1.要监控的程序为使用cygwin环境编译的exe可执行文件hello.exe,源码如下: #include <stdio.h> #include <unistd.h> voi ...
- HDU 3374 String Problem(最大最小表示+KMP)题解
题意:给你一个字符串,这个字符串可以这样操作:把第一个字符放到最后一个形成一个新的字符串,记原式Rank为1,每操作一步Rank+1,问你这样操作得出的最小字典序的字符串的Rank和这样的字符串有几个 ...
- spring Boot加载bean
1.SpringBoot中加载bean,可以使用注解@compenent直接加载到applicationContext容器中 2.在直接类@Configuration中,手动注册bean,如: