浅谈线段树合并: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的更多相关文章

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

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

  2. 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...

  3. bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并

    Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...

  4. 【BZOJ2212】[POI2011]Tree Rotations (线段树合并)

    题解: 傻逼题 启发式合并线段树里面查$nlog^2$ 线段树合并顺便维护一下$nlogn$ 注意是叶子为n 总结点2n 代码: #include <bits/stdc++.h> usin ...

  5. BZOJ2212: [Poi2011]Tree Rotations

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

  6. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

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

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

  8. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...

  9. 2212: [Poi2011]Tree Rotations

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

随机推荐

  1. Python菜鸟之路:Python基础-生成器和迭代器、递归

    一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,知道所有的元素被访问完结束.迭代器只能往前不会后退. 1. 迭代器优点 对于无法随机访问的数据结构(比如set)而言, ...

  2. Android笔记之GridView

    完整Demo链接:https://pan.baidu.com/s/1d_G9aCwBxpiYQcdQhwSDDw,提取码:5deh 效果图 activity_main.xml <?xml ver ...

  3. JVM性能优化, Part 1 ―― JVM简介

    JVM性能优化这些列文章共分为5章,是ImportNew上面翻译自Javaworld: 第1章:JVM技术概览 第2章:编译器 第3章:垃圾回收 第4章:并发垃圾回收 第5章:可伸缩性 众所周知,Ja ...

  4. sql获取数组指定元素

    需求:获取字符数组1,2,3的第2个元素 方法:通过自定义函数来实现 /* 获取字符串数组某个元素 */ from sysobjects where id = object_id('Get_StrAr ...

  5. persisted? vs new_record?

    https://joe11051105.gitbooks.io/you-need-to-know-about-ruby-on-rails/content/activerecord/persisted_ ...

  6. UVALive - 7427 the math 【二分匹配】

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. 每天一个Linux命令(35)wc命令

          Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出.       (1)用法:     用法:  wc [选项] [文件]. ...

  8. 2014年互联网IT待遇

    1. 13k*14~16k*145.美团 13k*15~16k*15,也有更高的.6.去哪儿 11k*16~15k*167.人人技术类(12K-14K)*14 (2014)8.58同城 20w+9.网 ...

  9. centos 中 增强web服务器安全

    一.修改ssh连接的默认端口: 1.1 用root 连接进入系统: 1.2 修改ssh的配置文件 #vi /etc/ssh/sshd_config 在13行找到#Port 22 (默认端口22) 1. ...

  10. in型子查询陷阱,exists子查询

    in 型子查询引出的陷阱 select goods_id from goods where cat_id in (1,2,3) 直接用id,不包含子查询,不会中陷阱 题: 在ecshop商城表中,查询 ...