BZOJ2212:[POI2011]Tree Rotation
浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2212
递归去做,统计每个子树内最少会产生多少逆序对,在合并线段树的时候统计就好了。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n,tot;
ll ans,cnt1,cnt2;
int rt[maxn<<1],ls[maxn<<1],rs[maxn<<1];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct segment_tree {
int tot;
int sum[maxn*20],ls[maxn*20],rs[maxn*20];
void update(int p) {
sum[p]=sum[ls[p]]+sum[rs[p]];
}
void change(int &p,int l,int r,int pos) {
p=++tot;
if(l==r) {sum[p]=1;return;}
int mid=(l+r)>>1;
if(pos<=mid)change(ls[p],l,mid,pos);
else change(rs[p],mid+1,r,pos);
update(p);
}
int merge(int a,int b) {
if(!a||!b)return a+b;
cnt1+=1ll*sum[rs[a]]*sum[ls[b]];//cnt1记录不交换左右子树会得到多少逆序对
cnt2+=1ll*sum[rs[b]]*sum[ls[a]];//cnt2记录交换左右子树会得到多少逆序对
ls[a]=merge(ls[a],ls[b]);
rs[a]=merge(rs[a],rs[b]);
update(a);return a;
}
}T;
void solve(int u) {
int x=read();
if(x)T.change(rt[u],1,n,x);
else {
ls[u]=++tot,solve(ls[u]);
rs[u]=++tot,solve(rs[u]);
cnt1=cnt2=0;
rt[u]=T.merge(rt[ls[u]],rt[rs[u]]);
ans+=min(cnt1,cnt2);
}
}
int main() {
n=read();
solve(1);
printf("%lld\n",ans);
return 0;
}
BZOJ2212:[POI2011]Tree Rotation的更多相关文章
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并
原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...
- bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- 【BZOJ2212】[POI2011]Tree Rotations (线段树合并)
题解: 傻逼题 启发式合并线段树里面查$nlog^2$ 线段树合并顺便维护一下$nlogn$ 注意是叶子为n 总结点2n 代码: #include <bits/stdc++.h> usin ...
- 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_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
随机推荐
- WCF基础之数据协定
数据协定最重要的当然就是DataContract和DataMember.这两个特性能应用到类.结构和枚举.这个两个特性跟服务契约的特点是一样的,只有被DataContract标记的类和类中被标记Dat ...
- HTML元素嵌套关系
- L2 范数 L1 范数 出租车范数
https://en.wikipedia.org/wiki/Norm_(mathematics) http://cs231n.github.io/classification/
- php扩展trie_filter: 利用词库, 过滤敏感词
1. 先安装libiconv# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz# tar -zxvf libiconv- ...
- 【Robot Framework】---- Robot Framework简介、特点、RIDE
Robot Framework简介.特点.RIDE 一.简介.特点. Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型 ...
- 【题解】P2161[SHOI2009]会场预约(set)
[题解][P2161 SHOI2009]会场预约 题目很像[[题解]APIO2009]会议中心 \(set\)大法好啊! 然后我们有个小\(trick\)(炒鸡帅),就是如何优雅地判断线段交? str ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
- [ACM] FZU 2087 统计数边 (有多少边至少存在一个最小生成树里面)
Problem Description 在图论中,树:随意两个顶点间有且仅仅有一条路径的图. 生成树:包括了图中全部顶点的一种树. 最小生成树:对于连通的带权图(连通网)G,其生成树也是带权的. 生成 ...
- 免费好用的Diff和Merge工具大总结
总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...
- CVPR 2018paper: DeepDefense: Training Deep Neural Networks with Improved Robustness第一讲
前言:好久不见了,最近一直瞎忙活,博客好久都没有更新了,表示道歉.希望大家在新的一年中工作顺利,学业进步,共勉! 今天我们介绍深度神经网络的缺点:无论模型有多深,无论是卷积还是RNN,都有的问题:以图 ...