POI2011 Tree Rotations

给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树。要求前序遍历叶子的逆序对最少。

由于对于当前结点x,交换左右子树,对于范围之外的逆序对个数并没有影响,所以可以进行线段树合并,合并时统计l在左边还是在右边更优。

#include <cstdio>
#include <cctype>
using namespace std;
typedef long long LL; inline void read(int &x){
char ch; x=0;
for (; ch=getchar(), !isdigit(ch););
for (x=ch-48; ch=getchar(), isdigit(ch);)
x=(x<<3)+(x<<1)+ch-48;
}
inline void read(LL &x){
char ch; x=0;
for (; ch=getchar(), !isdigit(ch););
for (x=ch-48; ch=getchar(), isdigit(ch);)
x=(x<<3)+(x<<1)+ch-48;
}
inline LL min(LL a,LL b) { return a > b?b : a; } const int maxn = 2e5+5;
struct node {
int seg,ls,rs;
} a[maxn*30];
LL ANS = 0,ans1 = 0,ans2 = 0;
int n; int cnt = 0;
void modify(int &x,int l,int r, int pos) {
if(!x) x = ++cnt;
a[x].seg++;
int mid=l+r>>1;
if(l == r) return;
if(pos <= mid) modify(a[x].ls,l,mid,pos);
else modify(a[x].rs,mid+1,r,pos);
}
void merge(int &l,int r) {
if(!l || !r) { l+=r; return; }
a[l].seg += a[r].seg;
ans1 += (LL)a[a[l].rs].seg*a[a[r].ls].seg;
ans2 += (LL)a[a[l].ls].seg*a[a[r].rs].seg;
merge(a[l].ls,a[r].ls);
merge(a[l].rs,a[r].rs);
} void solve(int &x) {
int t,ls,rs;
x = 0; read(t);
if(!t) {
solve(ls),solve(rs);
ans1 = ans2 = 0;
x = ls;
merge(x,rs);
ANS += min(ans1,ans2);
} else modify(x,1,n,t);
} int main() {
read(n);
int t = 0;
solve(t);
printf("%lld\n",ANS);
return 0;
}

POI2011 Tree Rotations的更多相关文章

  1. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

  2. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  3. 2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...

  4. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  5. bzoj 2212: [Poi2011]Tree Rotations

    Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...

  6. 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...

  7. bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并

    Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...

  8. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  9. BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...

随机推荐

  1. cpu上下文切换(下)

    --怎么查看系统的上下文切换情况 过多的上下文切换,会把cpu时间消耗在寄存器.内核栈以及虚拟内存等数据的保存和恢复上,缩短进程真正运行的时间,成了系统性能大幅下降的一个元凶. 查看,使用vmstat ...

  2. 虚幻引擎4设置Visual Studio

    转自:http://www.unrealchina.net/portal.php?mod=view&aid=149 设置Visual Studio和虚幻引擎4协同工作有利于提高开发人员使用UE ...

  3. Java8 日期和时间实用技巧

    新的日期API ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则 Instant: 用来表示时间线上的一个点 LocalDate: 表示没有时区的日期, Lo ...

  4. SqlServer——事务一编程进阶(SqlServer技术内幕 T-SQL程序设计 第九章

    事务格式如下: 1.开启事务: begin tran 2.提交事务:commit tran 3.回滚事务:rollback tran 判断事务是提交还是应该回滚有两种方式,一是全局变量 @@error ...

  5. 通过XmlSerializer接口来生成xml文件

    xml文件我们可以用来保存一些数据.例如用来备份短信.这个例子中,我们就用XmlSerializer接口来实现一个备份短信的程序.当然了,为了程序简单化,这个程序我们并不是真的去备份短信.我们通过一个 ...

  6. springJunit测试

    ; m.setModelType(s); m.setModelUrlType(s); modelService.create(m); } }   来源: http://enki-ding-yeah-n ...

  7. BitmapCutter.Core 引用步骤

    1 将BitmapCutter.Core项目或者dll引用到需要的项目底下. 2 前台页面 <img id="imgPreview1" title="点击上传图片& ...

  8. 每天一道算法题(24)——自定义幂函数pow

    double myPower(double base, int exponent){ if(exponent==0) return 1; if(exponent==1) return base; if ...

  9. [hadoop入门]mapper与reducer(word_count计数demo)

    1.mapper #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words = line.sp ...

  10. Node.js的__dirname,__filename,process.cwd(),./的含义

    简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...