P3521 [POI2011]ROT-Tree Rotations

题目大意:

给一棵\((1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少。

我们发现交换两个子树并不会影响某个子树内的逆序对个数,只会对两个子树之间的逆序对产生影响.

所以我们将换与不换的逆序对求出来,取个最小值

将两颗线段树合并就好了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 4e5 + 3;
struct node{
int sum;
int lc,rc;
}a[N * 30];
int root;
int n,t,cnt = 1;
int rt[N];
LL ans1,ans2,ans;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline void pushup(int u){
a[u].sum = a[a[u].lc].sum + a[a[u].rc].sum;
}
inline void ins(int &u,int l,int r,int x){
// printf("%d %d %d\n",l,r,x);
if(!u) u = ++t;
if(l == r){
a[u].sum++;
return ;
}
int mid = (l + r) >> 1;
if(x <= mid) ins(a[u].lc,l,mid,x);
else ins(a[u].rc,mid + 1,r,x);
pushup(u);
}
inline void merge(int &u1,int u2,int l,int r){
if(!u1){u1 = u2;return ;}
if(!u2) return ;
if(l == r){
a[u1].sum += a[u2].sum;
return ;
}
int mid = (l + r) >> 1;
ans1 += 1ll * a[a[u1].lc].sum * a[a[u2].rc].sum;
ans2 += 1ll * a[a[u2].lc].sum * a[a[u1].rc].sum;
merge(a[u1].lc,a[u2].lc,l,mid);
merge(a[u1].rc,a[u2].rc,mid + 1,r);
pushup(u1);
}
inline void solve(int pos){
int x = read();
if(!x){
int lc = ++cnt;solve(lc);
int rc = ++cnt;solve(rc);
merge(rt[pos],rt[lc],1,n);
ans1 = ans2 = 0;
merge(rt[pos],rt[rc],1,n);
ans += min(ans1,ans2);
}
else{
ins(rt[pos],1,n,x);
return ;
}
}
int main(){
n = read();
solve(1);
printf("%lld\n",ans);
return 0;
}

LuoguP3521 [POI2011]ROT-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. POI2011 Tree Rotations

    POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...

  6. [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树

    二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...

  7. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

  8. 线段树合并(【POI2011】ROT-Tree Rotations)

    线段树合并([POI2011]ROT-Tree Rotations) 题意 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有nn个叶子节点,满足这些权值为1-n1-n的一个 ...

  9. bzoj 2212: [Poi2011]Tree Rotations

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

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

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

随机推荐

  1. 微信小程序云数据库——where查询和doc查询区别

    用法 条件查询where 我们也可以一次性获取多条记录.通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项,用 ...

  2. phpcms信息模型使用

    PHPCMS V9 分类信息模型测试版下载 安装 1.确定您的phpcms版本为20110318版本以上 2.将所有文件覆盖到网站目录下,运行install_info. 3.将文件中所有的phpcms ...

  3. 安装完django验证是否安装成功&&运行项目

    1.验证是否成功安装django 上图证明安装成功 2.打开客户端的pycharm,在terminal中打开窗体,什么都没输入,运行下面的命令 python manage.py runserver 在 ...

  4. UVa 10220 【大整数】

    uva 10220 可采用uva 623这道题求N!,再最后稍微改一下就好. 参考代码: } #include<cstdio> #include<cstring> #inclu ...

  5. 2019-8-31-C#-将-Begin-和-End-异步方法转-task-异步

    title author date CreateTime categories C# 将 Begin 和 End 异步方法转 task 异步 lindexi 2019-08-31 16:55:58 + ...

  6. oracle函数 to_single_byte(c1)

    [功能]将字符串中的全角转化为半角 [参数]c1,字符型 [返回]字符串 [示例] SQL> select to_multi_byte('高A') text from dual; test -- ...

  7. ROS 设置串口USB软连接

    原创:未经同意,请勿转载 我们在windows 通过USB连接串口,在设备串口中可以观测到COM0或者COMx.当我们插入不同的USB口时会显示不同的COM. 在UBUNTU下,ROS下接收串口信息时 ...

  8. CENTOS7安装R语言环境

    CENTOS7安装R语言环境 yum install texinfo.x86_64 yum install texlive.x86_64 cd /opt wget https://mirrors.tu ...

  9. 从零学React Native之02状态机

    本篇文章首发于简书 欢迎关注 之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 本篇文章主要介绍下下面的知识: 1.简单界面的搭 ...

  10. jmeter日期处理beanshell(1)

    import java.time.LocalDate; //昨天: String sdate1 = LocalDate.now().minusDays(1).toString(); vars.put( ...