POI2011 Tree Rotations

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

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

  1. #include <cstdio>
  2. #include <cctype>
  3. using namespace std;
  4. typedef long long LL;
  5. inline void read(int &x){
  6. char ch; x=0;
  7. for (; ch=getchar(), !isdigit(ch););
  8. for (x=ch-48; ch=getchar(), isdigit(ch);)
  9. x=(x<<3)+(x<<1)+ch-48;
  10. }
  11. inline void read(LL &x){
  12. char ch; x=0;
  13. for (; ch=getchar(), !isdigit(ch););
  14. for (x=ch-48; ch=getchar(), isdigit(ch);)
  15. x=(x<<3)+(x<<1)+ch-48;
  16. }
  17. inline LL min(LL a,LL b) { return a > b?b : a; }
  18. const int maxn = 2e5+5;
  19. struct node {
  20. int seg,ls,rs;
  21. } a[maxn*30];
  22. LL ANS = 0,ans1 = 0,ans2 = 0;
  23. int n;
  24. int cnt = 0;
  25. void modify(int &x,int l,int r, int pos) {
  26. if(!x) x = ++cnt;
  27. a[x].seg++;
  28. int mid=l+r>>1;
  29. if(l == r) return;
  30. if(pos <= mid) modify(a[x].ls,l,mid,pos);
  31. else modify(a[x].rs,mid+1,r,pos);
  32. }
  33. void merge(int &l,int r) {
  34. if(!l || !r) { l+=r; return; }
  35. a[l].seg += a[r].seg;
  36. ans1 += (LL)a[a[l].rs].seg*a[a[r].ls].seg;
  37. ans2 += (LL)a[a[l].ls].seg*a[a[r].rs].seg;
  38. merge(a[l].ls,a[r].ls);
  39. merge(a[l].rs,a[r].rs);
  40. }
  41. void solve(int &x) {
  42. int t,ls,rs;
  43. x = 0; read(t);
  44. if(!t) {
  45. solve(ls),solve(rs);
  46. ans1 = ans2 = 0;
  47. x = ls;
  48. merge(x,rs);
  49. ANS += min(ans1,ans2);
  50. } else modify(x,1,n,t);
  51. }
  52. int main() {
  53. read(n);
  54. int t = 0;
  55. solve(t);
  56. printf("%lld\n",ANS);
  57. return 0;
  58. }

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代码输出质因数

    package com.badu; import java.util.Scanner; //分解质因数问题: //从键盘输一个数, //首先最小质因数为2 //n不能被2整除时, //n能被2整除时, ...

  2. df 命令-显示目前磁盘剩余的磁盘空间

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  3. postgresql 主从复制并切换

    1 环境 192.168.19.145 Kylin 3.3 mysqlhq  9.5.2  psql_master192.168.19.227 Kylin 3.3 mysql3    9.5.2  p ...

  4. linux uid/euid/suid

    Each UNIX process has 3 UIDs associated to it. Superuser/root is UID=0. UID Read UID. It is of the u ...

  5. Flash 零日漏洞复现(CVE-2018-4878)

    项目地址:https://github.com/Sch01ar/CVE-2018-4878.git 影响版本为:Adobe Flash Player <= 28.0.0.137 攻击机器IP:1 ...

  6. 如何通过outline为SQL语句指定执行计划

    创建测试表 以用户jyu连接,创建测试表 SQL> conn jyu/jyu; Connected. SQL> create table t (id number, name varcha ...

  7. DAY13-前端之jQuery

    jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...

  8. Javascript面向对象(二):构造函数的继承

    这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...

  9. Mac系统下MySql下载MySQL5.7及详细安装流程

    一.在浏览器当中输入以下地址 https://dev.mysql.com/downloads/mysql/    二.进入以下界面:直接点击下面位置 ,选择跳过登录 点过这后直接下载. 三.下载完成后 ...

  10. 使用Aspectj 的配置文件方式进行aop操作