BZOJ2212 [POI2011] Tree Rotations 【treap】
题目分析:
写的无旋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】的更多相关文章
- BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】
题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...
- 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 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
随机推荐
- Python实现将爱词霸每日一句定时推送至微信
前言 前几天在网上看到一篇文章<教你用微信每天给女票说晚安>,感觉很神奇的样子,随后研究了一下,构思的确是巧妙.好,那就开始动工吧!服务器有了,Python环境有了,IDE打开了...然而 ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Springboot通过cors解决跨域问题(解决spring security oath2的/oauth/token跨域问题)
@Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCo ...
- JavaScript中防止重复提交
有这么一种情况: 页面有一个按钮,点击之后会触发Ajax请求,但是用户在点击之后,不知道是否点成功了,于是又点了一下,如果不加处理的话,就会进行两次Ajax请求,并且请求的数据都是一样的,对后端的程序 ...
- NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus
NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plushttps://docs. ...
- [转帖]SAP一句话入门:Plant Maintenance
SAP一句话入门:Plant Maintenance http://blog.vsharing.com/MilesForce/A618273.html PM就是Plant Maintenance(本文 ...
- [转帖]linux sed命令
linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...
- 【Java基础】switch语句实现根据数字输出对应星期
代码: import java.util.Scanner; /* * switch语句格式: * switch(表达式) { * case 值1: * 语句体1; * break; * case 值2 ...
- Angular 自定义指令传参
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- Java8 Hash改进/内存改进
又开新坑o(*≧▽≦)ツ讲讲几个Java版本的特性,先开始Java8, HashMap的改进 HashMap采用哈希算法,先使用hashCode()判断哈希值是否相同,如果相同,再使用equals() ...