思路

发现每个子树内的交换情况不会对子树外造成影响,所以可以利用贪心的思想,线段树合并找出当前子树的合并的最小值直接累加给答案即可

我脑补的线段树合并貌似不太优秀的样子。。。每次都要新建节点,学习了直接把y合并到x上的想法,但是因为#define int long long的坏习惯导致MLE,最后还是要写内存池。。。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define int long long
using namespace std;
struct Node{
int lson,rson,sz;
}Seg[3000000];
int Nodecnt,ans,min1,min2,n;
queue<int> q;
int getnew(void){
if(q.size()){
int t=q.front();
q.pop();
Seg[t].lson=Seg[t].rson=Seg[t].sz=0;
return t;
}
return ++Nodecnt;
}
void throwin(int x){
q.push(x);
}
void build(int l,int r,int &o,int val){
// printf("%d %d %d %d\n",l,r,o,val);
if(!o)
o=getnew();
// printf("%d %d %d %d\n",l,r,o,val);
Seg[o].sz++;
if(l==r){
return;
}
int mid=(l+r)>>1;
if(val<=mid)
build(l,mid,Seg[o].lson,val);
else
build(mid+1,r,Seg[o].rson,val);
}
void merge(int &x,int y,int l,int r){
if(x*y==0){
x=x+y;
return;
}
min1+=Seg[Seg[x].rson].sz*Seg[Seg[y].lson].sz;
min2+=Seg[Seg[x].lson].sz*Seg[Seg[y].rson].sz;
if(l==r){
Seg[x].sz=Seg[x].sz+Seg[y].sz;
return;
}
Seg[x].sz=Seg[x].sz+Seg[y].sz;
int mid=(l+r)>>1;
merge(Seg[x].lson,Seg[y].lson,1,mid);
merge(Seg[x].rson,Seg[y].rson,mid+1,r);
throwin(y);
return;
}
int solve(void){
int x;
scanf("%lld",&x);
if(!x){
int lson,rson;
lson=solve();
rson=solve();
min1=0;
min2=0;
int t=lson;
merge(t,rson,1,n);
// printf("%d %d\n",min1,min2);
ans+=min(min1,min2);
return t;
}
else{
int t=0;
// printf("ok\n");
build(1,n,t,x);
// printf("ok\n");
return t;
}
return 0;
}
signed main(){
scanf("%lld",&n);
solve();
printf("%lld\n",ans);
return 0;
}

P3521 [POI2011]ROT-Tree Rotations的更多相关文章

  1. P3521 [POI2011]ROT-Tree Rotations (线段树合并)

    P3521 [POI2011]ROT-Tree Rotations 题意: 给你一颗树,只有叶子节点有权值,你可以交换一个点的左右子树,问你最小的逆序对数 题解: 线段树维护权值个个数即可 然后左右子 ...

  2. BZOJ2212: [Poi2011]Tree Rotations

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

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

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

  4. loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)

    P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...

  5. 2212: [Poi2011]Tree Rotations

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

  6. 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告

    P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...

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

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

  8. POI2011 Tree Rotations

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

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

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

  10. bzoj 2212 Tree Rotations

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

随机推荐

  1. 仿豆瓣网(电脑版网页)HTML+CSS实现

    步骤一:将豆瓣电脑版网页以图片形式保存下来: 利用了chrome里面的插件: 步骤二:将图片放置到PS中,研究布局: 我将其分为header部分,banner部分,section部分,footer部分 ...

  2. spring 的核心类JdbcTemplate 方法

    2018-11-29  10:28:02

  3. 从js中提取数据

    <script language="JavaScript" type="text/javascript+gk-onload"> SKART = (S ...

  4. Lua语言总结

    [1]要退出交互模式和解释器,只需输入“os.exit()” [2]在交互模式执行程序块可以使用函数dofile,这个函数就可以立即执行一个文件.应用示例:dofile("f:/myLua/ ...

  5. Hive中实现group concat功能(不用udf)

    在 Hive 中实现将一个字段的多条记录拼接成一个记录: hive> desc t; OK id string str string Time taken: 0.249 seconds hive ...

  6. kibana添加ES索引403错误解决

    kibana添加ES索引时发现kibana添加索引不生效,没有创建成功只是一闪而过 查看控制台发现报错403 解决办法: curl -XPUT -H "Content-Type: appli ...

  7. The Little Prince-12/15

    The Little Prince-12/15 明天四六级考试了呢!!!喵喵喵,愿大家都能取得好成绩. 星星美丽,因为里面有一朵看不见的花. 沙漠美丽,因为沙漠的某处隐藏着一口井. ————生活美好, ...

  8. 2017第十三届湖南省省赛B - Simplified Blackjack CSU - 1998

    在一次聚会上,Bob打算和Alice一起玩Blackjack游戏,但Alice平时很少玩扑克类游戏,Bob觉得跟Alice解释清楚Blackjack的规则有点困难,于是Bob决定和Alice玩一次简化 ...

  9. lnmp 安装redis-最全

    一. 安装redis 1.下载,解压,编译 $ wget http://download.redis.io/releases/redis-3.2.8.tar.gz $ tar -xzf redis-3 ...

  10. Docker学习笔记之浅谈虚拟化和容器技术

    0x00 概述 相信所有对 Docker 有所耳闻的朋友都知道,它是一款以容器虚拟化技术为基础的软件,因此在了解有关 Docker 的概念知识和使用方法之前,虚拟化和容器技术是我们不可或缺的基础知识. ...