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\) 内 ...
随机推荐
- Ubuntu10.04 python2.6下安装matplotlib环境
一.准备工作1.sudo apt-get install python-numpy2.sudo apt-get install python2.6-dev3.sudo apt-get install ...
- php实现多进程
转:http://www.jb51.net/article/71238.htm cd php-version/ext/pcntl phpize ./configure && make ...
- ASCII 码
http://baike.baidu.com/link?url=Y4crTsVq678Z8fr92DAGIrqVHoexVXsMc-WKBMVUKGDq4KbEOuhWbUQXuQEtnlom4yln ...
- Python -- Pandas介绍及简单实用【转】
转http://www.datadependence.com/2016/05/scientific-python-pandas/ 一. Pandas简介 1.Python Data Analysis ...
- 运用kNN算法识别潜在续费商家
背景与目标 Youzan 是一家SAAS公司,服务于数百万商家,帮助互联网时代的生意人私有化顾客资产.拓展互联网客群.提高经营效率.现在,该公司希望能够从商家的交易数据中,挖掘出有强烈续费倾向的商家, ...
- SQL知识点、SQL语句学习
一. 数据库简介和创建1. 系统数据库在安装好SQL SERVER后,系统会自动安装5个用于维护系统正常运行的系统数据库: (1)master:记录了SQL SERVER实例的所有系统级消息,包括实例 ...
- html5的理解
1.良好的移动性,以移动设备为主 2.响应式设计,以适应自动变化的屏幕尺寸 3.支持离线缓存技术,webStorage本地缓存 4.新增canvas.video.audio等新标签元素,新增特殊内容元 ...
- django框架基本介绍
一.mvc和mtv 1.mvc介绍 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器( ...
- strcpy和memcpy,memmove函数的区别
strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...
- Eloquent JavaScript #10# Modules
索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...