题目分析:

写的无旋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. Node.js中读取文件后用Json.parse方法报错解决方案

    今天,在调试一个node项目时,发现了一个很大的坑,在此分享给大家! 大家都知道,Json.parse()方法对格式要求是很严格的,格式不对极其容易报错,但是有时候格式看似是正确的也会报错. 比如这一 ...

  2. NanoFabric-ServiceFabric 操作手册

    service-fabric-52abp-ocelot A Service Fabric sample with a Frontend, one API Gateway and 52abp Micro ...

  3. [Linux]Debian 9重启DNS重置问题

    先编辑/etc/resolv.conf, 添加一个DNS, 比如114.114.114.114 然后sudo apt-get install resolvconf 然后编辑/etc/resolvcon ...

  4. Unique Snowflakes UVA - 11572 (离散化+尺取法)

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...

  5. Tea Party CodeForces - 808C (构造+贪心)

    Polycarp invited all his friends to the tea party to celebrate the holiday. He has ncups, one for ea ...

  6. oc之证书

    https://www.cnblogs.com/MrJalen/p/6813309.html iOS推送证书生成pem文件(详细步骤)   1.pem文件概述 pem文件是服务器向苹果服务器做推送时候 ...

  7. linux中根据名称kill进程

    shell函数如下: # kill processes by name kbn() { line=`ps -a | grep $1` arr=($line) for((i=0;i<${#arr[ ...

  8. CI框架在模型中切换读写库和读写库

    如果你想在控制器中切换在application/config/database.php中配置好的数据库group,那么你可以参考这篇博客:CI框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...

  9. 【学习总结】Git学习-参考廖雪峰老师教程三-创建版本库

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  10. tomcat7 server.xml max thread

    java - Tomcat - maxThreads vs maxConnections - Stack Overflowhttps://stackoverflow.com/questions/246 ...