题目分析:

写的无旋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. Quartz.NET 任务调度新教程

    https://www.cnblogs.com/yscit/p/10393867.html

  2. 朱晔和你聊Spring系列S1E2:SpringBoot并不神秘

    朱晔和你聊Spring系列S1E2:SpringBoot并不神秘 [编辑器丢失了所有代码的高亮,建议查看PDF格式文档] 文本我们会一步一步做一个例子来看看SpringBoot的自动配置是如何实现的, ...

  3. 2017百度软研(C++)

    问了很多,不过很多也都没有问到,感觉有点偏,很深入的东西,越问越虚.第一次实习面试从百度开始,也从百度结束吧.看得见的差距,不想将就,所以还是拿最后一次机会去尝试.win or go home, 所以 ...

  4. An error occurred while updating the entries. See the inner exception for details.

    EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...

  5. Day5 Pyhton基础之编码与解码(四)

    1.编码与解码 1.1现在常用的编码类型

  6. javaScript 删除本地cookie删不了

    一.js删除本地cookie无法删除 今天发现自己真的蠢爆了! 以下为cookie定义: 1.设置Cookie的key   2.设置Cookie的key-value值   3.过期时间-自定义(一般在 ...

  7. RabbitMQ 安装与使用

    RabbitMQ 安装与使用   前言 吃多了拉就是队列,吃饱了吐就是栈 使用场景 对操作的实时性要求不高,而需要执行的任务极为耗时:(发送短信,邮件提醒,更新文章阅读计数,记录用户操作日志) 存在异 ...

  8. PAT L3-020 至多删三个字符

    https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 给定一个全部由小写英文字母组成的字符串,允许 ...

  9. Eclipse支持文件UTF-8编码

    Eclipse修改编码格式_百度经验https://jingyan.baidu.com/article/2009576193ee38cb0721b416.html 这篇最棒 如何为eclipse中的文 ...

  10. jmeter 启动jmeter-server.bat远程调用报错: java.io.FileNotFoundException: rmi_keystore.jks (系统找不到指定的文件。)

    1.找到apache-jmeter-5.0\bin\jmeter.properties 2.修改server.rmi.ssl.disable=true (记得去除server.rmi.ssl.disa ...