思路

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

我脑补的线段树合并貌似不太优秀的样子。。。每次都要新建节点,学习了直接把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. django migrate无效的解决方法

    遇到一个很奇怪的问题 python manage.py makemigrations 的时候显示要创建两张表,但是执行 python manage.py migrate 的时候不能识别,也就是说失效了 ...

  2. OC 反射-->动态创建类

    系统方法 NSLog(@"%s", __func__); //打印出类的方法名称,如: //打印结果:2018-02-22 10:52:15.394575+0800 DemoRun ...

  3. js函数集

    js函数集·字符串(String) 1.声明 var myString = new String("Every good boy does fine."); var myStrin ...

  4. python 将字节字符串转换成十六进制字符串

    想将一个十六进制字符串解码成一个字节字符串或者将一个字节字符串编码成一个十六进制字符串 1)只是简单的解码或编码一个十六进制的原始字符串,可以使用 binascii模块 >>> s ...

  5. python 读csv格式的文件

    对于大多数的CSV 格式的数据读写问题,都可以使用csv 库 1. 直接读csv 以下是要操作的csv文件内容 import csv with open(r'C:\Temp\f.csv') as f: ...

  6. 使用Oozie中workflow的定时任务重跑hive数仓表的历史分期调度

    在数仓和BI系统的开发和使用过程中会经常出现需要重跑数仓中某些或一段时间内的分区数据,原因可能是:1.数据统计和计算逻辑/口径调整,2.发现之前的埋点数据收集出现错误或者埋点出现错误,3.业务数据库出 ...

  7. vue各种实例集合

    注意:以下所有示例基于vue 2.x.Vuex 2.x. vm.$mount()-挂载: <body> <div id="a"> </div> ...

  8. Different between MB SD Connect Compact 5 and MB SD C4 Star Diagnostic Tool

    MB SD C4 Star Diagnostic Tool is the professional MB Star Diagnostic Tools for benz cars and trucks. ...

  9. Django框架----ORM数据库操作注意事项

    1.多对多的正向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名&q ...

  10. 翻唱 - shape of you - 个个版本

    翻唱: http://7j1xky.com1.z0.glb.clouddn.com/1525514286196.mp4 乐队版-我的翻唱-混合 http://7j1xky.com1.z0.glb.cl ...