P3521 [POI2011]ROT-Tree Rotations
思路
发现每个子树内的交换情况不会对子树外造成影响,所以可以利用贪心的思想,线段树合并找出当前子树的合并的最小值直接累加给答案即可
我脑补的线段树合并貌似不太优秀的样子。。。每次都要新建节点,学习了直接把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的更多相关文章
- P3521 [POI2011]ROT-Tree Rotations (线段树合并)
P3521 [POI2011]ROT-Tree Rotations 题意: 给你一颗树,只有叶子节点有权值,你可以交换一个点的左右子树,问你最小的逆序对数 题解: 线段树维护权值个个数即可 然后左右子 ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告
P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树
二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
随机推荐
- Python2.6 升级2.7
一. Centos6 默认为python2.6且不可卸载(因为Centos6深度依赖Python),要想升级为2.7 只能通过全新升级 操作如下: 1.下载 Python2.7 网址 https:// ...
- jQuery文档操作--empty()和remove()
empty() 概述 删除匹配的元素集合中所有的子节点 <!DOCTYPE html> <html> <head> <meta charset="U ...
- MySql获取两个日期间的时间差
[1]MySql 语言获取两个日期间的时间差 DATEDIFF 函数可以获得两个日期之间的时间差.但是,这个函数得出的结果是天数. 需要直接获取秒数可使用TIMESTAMPDIFF函数.应用示例如下: ...
- plsql 代码自动补全
1.新建一个文件,命名不限定,文件内容为自动补全内容,比如: i=INSERTu=UPDATEs=SELECTf=FROMw=WHEREo=ORDER BYd=DELETEdf=DELETE FROM ...
- Firefox 功能笔记
1.复制标签 说明:复制标签功能即新开一个与当前页一样的标签页,这个功能在Chrome中点击标签右键复制即可,但是在firefox中没有 Firefox中实现:Ctrl+拖动标签页
- python小练习:读入一个考试得分,判断这个分数是哪个等级,并输出,考虑异常场景
读入一个考试得分,判断这个分数是哪个等级,并输出. 等级:>=90 优 ,>=80且小于90 良,>=70 且小于80,中,>=60且<70及格 <60 不及格 ...
- pytest+request 接口自动化测试
1.安装python3brew update brew install pyenv 然后在 .bash_profile 文件中添加 eval “$(pyenv init -)” pyenv insta ...
- oj练习
1.toj 1138. Binomial Showdown $$ 二项式定理恒等式变换.数据类型溢出(乘法.加法).排列组合数计算优化(C(k,n) = C(n-k,n).排列组合数的计算. ...
- php 一个文件搞定支付宝支付,微信支付
博客:https://me.csdn.net/jason19905 支付宝支付:https://github.com/dedemao/alipay 微信支付:https://github.com/de ...
- 使用v-for指令渲染列表
v-for:对集合或对象进行遍历: 使用v-for对数组遍历时: 效果如下: 代码: <script> window.onload= () =>{new Vue({ el:'#two ...