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. java代码包装类----------Integer

    总结:我的笔试挂了..基础的继承不懂不会.我不知道到底是哪里的问题. 要好好反思 package com.da.ima2; public class jrfk { // int整型不能直接转化为Int ...

  2. Oracle 高可用作业测试

    1.RAC 是一种: A 分布式数据库B 内存共享多节点数据库C 能够保护数据安全的架构 2.RAC 和CRS 的区别是: A 前者表示一种架构,后者是一套软件B 前者表示一套软件,后者是一种架构 C ...

  3. c++ 端口扫描程序

    第一.原理 端口扫描的原理很简单,就是建立socket通信,切换不通端口,通过connect函数,如果成功则代表端口开发者,否则端口关闭. 所有需要多socket程序熟悉,本内容是在window环境下 ...

  4. Android中Activity之间的数据传递

    在开发中,我们经常涌用到Activity,那么既然用到了Activity,就一定免不了在两个或者多个Activity之间传递数据.这里我们先说一说原理,然后在看看代码和例子. 情况A:我们需要从Act ...

  5. Java通过JDBC 进行Dao层的封装

    前言 前面有一章节,我专门讲解了Java通过JDBC 进行MySQL数据库操作,这主要讲解了MySQL数据库的连接和简单的操作,但是在真正的Java项目中,我们要不断的和数据库打交道,为了提高数据库操 ...

  6. 理解和正确使用Java中的断言(assert)

    一.语法形式:    Java2在1.4中新增了一个关键字:assert.在程序开发过程中使用它创建一个断言(assertion),它的语法形式有如下所示的两种形式:1.assert conditio ...

  7. 配gzip的过滤器进行压缩解决表单加载慢问题

    一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...

  8. js语法和数据类型

    基础知识(Basics) JavaScript 的很多语法借鉴自 Java,但也受 Awk,Perl 和 Python 影响. JavaScript 是大小写敏感的,使用 Unicode 字符集. 在 ...

  9. 关于service和线程的区别

    主要有两方面,访问控制和功能区别 首先,service是运行在主线程上的,并不是一个新的线程 其次,service在运行的时候可以被多个activity访问和控制,而线程是不可以的 最后,servic ...

  10. java格式化数字、货币、金钱

    网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...