题目分析:

写的无旋treap应该跑不过,但bzoj判断的总时限。把相关实现改成线段树合并就可以了。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; int n;
int ch[maxn][],num,val[maxn]; int son[maxn>>][],sz[maxn],rot[maxn],data[maxn],key[maxn]; long long ans = ; int merge(int r1,int r2){
if(r1 == ) return r2; if(r2 == ) return r1;
if(key[r1] < key[r2]){
son[r1][] = merge(son[r1][],r2);
sz[r1] = sz[son[r1][]]+sz[son[r1][]]+;
return r1;
}else{
son[r2][] = merge(r1,son[r2][]);
sz[r2] = sz[son[r2][]]+sz[son[r2][]]+;
return r2;
}
}
pair<int,int> split(int rt,int k){
if(k == ) return make_pair(,rt);
if(k >= sz[rt]) return make_pair(rt,);
if(sz[son[rt][]] >= k){
pair<int,int> res = split(son[rt][],k);
son[rt][] = res.second; res.second = rt;
sz[rt] = sz[son[rt][]] + sz[son[rt][]] + ;
return res;
}else{
pair<int,int> res = split(son[rt][],k-sz[son[rt][]]-);
son[rt][] = res.first; res.first = rt;
sz[rt] = sz[son[rt][]] + sz[son[rt][]] + ;
return res;
}
}
int found(int now,int x){
if(now == ) return ;
if(data[now] == x) return sz[son[now][]];
if(data[now] < x) return sz[son[now][]]++found(son[now][],x);
else return found(son[now][],x);
} void dfs(int now){
int d; scanf("%d",&d);
if(d){val[now] = d; return;}
ch[now][] = ++num; dfs(num); ch[now][] = ++num; dfs(num);
} long long alpha,beta;
void walk(int A,int B){
int z = found(B,data[A]); alpha += z; beta += (sz[B]-z);
if(son[A][]) walk(son[A][],B);
if(son[A][]) walk(son[A][],B);
} void dfs2(int A,int &B){
if(son[A][]) dfs2(son[A][],B),son[A][] = ;
if(son[A][]) dfs2(son[A][],B),son[A][] = ;
sz[A] = ; int z = found(B,data[A]);
pair<int,int> pr = split(B,z);
B = merge(merge(pr.first,A),pr.second);
} void dfs1(int now){
if(val[now]){
num++;
key[num]=rand();
sz[num]=;
data[num]=val[now];
rot[now]=num;
return;
}
dfs1(ch[now][]); dfs1(ch[now][]);
alpha = ,beta = ;
if(sz[rot[ch[now][]]] < sz[rot[ch[now][]]]){
walk(rot[ch[now][]],rot[ch[now][]]);
dfs2(rot[ch[now][]],rot[ch[now][]]);
rot[now] = rot[ch[now][]];
}else {
walk(rot[ch[now][]],rot[ch[now][]]);
dfs2(rot[ch[now][]],rot[ch[now][]]);
rot[now] = rot[ch[now][]];
}
ans += min(alpha,beta);
} void read(){
scanf("%d",&n);
num = ;
dfs();
} void work(){
num = ;
dfs1();
printf("%lld",ans);
} int main(){
read();
work();
return ;
}

BZOJ2212 [POI2011] Tree Rotations 【treap】的更多相关文章

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

    题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...

  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 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  4. BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对

    原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...

  5. bzoj2212[Poi2011]Tree Rotations [线段树合并]

    题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...

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

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

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

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

  8. 2212: [Poi2011]Tree Rotations

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

  9. POI2011 Tree Rotations

    POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...

随机推荐

  1. c++入门之结构体初步

    结构体实际上是一种数据结构的雏形,对结构体的灵活使用很多时候可以带来很多便利.下面给出一个关于结构体的程序: #include "iostream" # include " ...

  2. 未能加载文件或程序集&quot;Newtonsoft.Json, Version=4.5.0.0

    这问题遇到好几次了,重新更改了引用都不好使,有的时候版本改成一致就好了,但是有的地方你不知道在哪里用了就不好排查,所性在config里面加个配置让程序运行的时候去处理得了~ 很实用,放在configu ...

  3. IDEA通过Git同步代码到Coding

     准备工作: (1)在本地创建好项目 (2)在coding创建好项目,并设置公开      1.创建Git仓库 2.选择对应的本地项目文件夹 以上两步相当于在项目文件夹中git bash here 并 ...

  4. zabbix使用jmx监控tomcat

    zabbix监控Tomcat/JVM实例性能(115) – 运维生存时间http://www.ttlsa.com/zabbix/zabbix-use-jmx-monitor-tomcat/ zabbi ...

  5. h5-canvas(其他api)

    ###1.使用图片(需要image对象) drawImage(image,x,y,width,height) 其中image是image或者canvas对象,x和y 是其在目标canvas的起始坐标 ...

  6. h5 文件下载

    一.a 标签 移动端不支持 onDownFile= (url, filename) => { const downUrl = `http://10.1.109.123:19092/down/to ...

  7. IdentityServer4【Topic】之StartUp中的配置

    Startup 身份服务器是中间件和服务的组合.所有的配置都是在启动类中完成的. Configuring services 通过调用如下代码在DI(dependency inject,依赖注入)中添加 ...

  8. PHP中对象是按值传递还是按引用传递?

    1.首先,什么是按值传递和按引用传递? 按值传递就是仅仅把值传递过去,相当于传递的是值的拷贝,而按引用传递传递的是内存的地址. 在 PHP5 中,如果按引用传递,就是将 zval 的地址赋给另一个变量 ...

  9. [转帖]NUMA

    作者:ibless 来源:CSDN 原文:https://blog.csdn.net/ibless/article/details/80114009 其实 很早之前对这一块有了解 比较多的的是 CCN ...

  10. 在linux上安装MySQL数据库,并简单设置用户密码,登录MySQL

    在新装的Centos系统上安装MySQL数据库. <p><a href="http://www.cnblogs.com/tijun/">提君博客原创< ...