借鉴()了一下题解……

线段树合并的裸题吧…

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 4000050
typedef long long LL;
int n,cnt,tree[N],son[N][2],root,Root[N],all,tr[N],s[N][2];
LL ans,ans1,ans2;
void build(int &x){
x=++cnt;
scanf("%d",&tree[x]);
if(tree[x])return;
build(son[x][0]),build(son[x][1]);
}
void dfs(int x){
if(!x)return;
printf("x=%d tree[x]=%d\n",x,tree[x]);
dfs(son[x][0]),dfs(son[x][1]);
}
void push_up(int x){tr[x]=tr[s[x][0]]+tr[s[x][1]];}
void insert(int &x,int l,int r,int wei){
if(!x)x=++all;
if(l==r){tr[x]=1;return;}
int mid=(l+r)>>1;
if(wei<=mid)insert(s[x][0],l,mid,wei);
else insert(s[x][1],mid+1,r,wei);
push_up(x);
}
int merge(int x,int y){
if(!x)return y;if(!y)return x;
ans1+=1LL*tr[s[x][1]]*tr[s[y][0]];
ans2+=1LL*tr[s[x][0]]*tr[s[y][1]];
s[x][0]=merge(s[x][0],s[y][0]);
s[x][1]=merge(s[x][1],s[y][1]);
push_up(x);return x;
}
void solve(int x){
if(tree[x])return;
solve(son[x][0]),solve(son[x][1]);
ans1=ans2=0;
Root[x]=merge(Root[son[x][0]],Root[son[x][1]]);
ans+=min(ans1,ans2);
}
int main(){
scanf("%d",&n);
build(root);
for(int i=1;i<=cnt;i++)if(tree[i])insert(Root[i],1,n,tree[i]);
solve(root);
printf("%lld\n",ans);
}

BZOJ 2212线段树的合并的更多相关文章

  1. BZOJ 2733 线段树的合并 并查集

    思路: 1.线段树合并(nlogn的) 2.splay+启发式合并 线段树合并比较好写 我手懒 //By SiriusRen #include <cstdio> #include < ...

  2. 树链剖分——线段树区间合并bzoj染色

    线段树区间合并就挺麻烦了,再套个树链就更加鬼畜,不过除了代码量大就没什么其他的了.. 一些细节:线段树每个结点用结构体保存,pushup等合并函数改成返回一个结构体,这样好写一些 struct Seg ...

  3. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  4. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  5. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  6. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  7. SPOJ COT3 Combat on a tree(Trie树、线段树的合并)

    题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...

  8. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  9. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. 常用图片、文字轮播插件SuperSlide

    SuperSlide 是致力于实现网站统一特效调用的函数,能解决大部分标签切换.焦点图切换等效果,还能多个slide组合创造更多的效果.(兼容ie内核(包括无敌的ie6).webkit内核.ff.op ...

  2. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...

  3. Rx (Reactive Extensions)

    The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...

  4. Python数据结构1-----基本数据结构和collections系列

    1.基本数据结构 整型.浮点型.字符串.元祖.列表.字典.集合 2.高级数据结构 (collections模块) (1)计数器(counter):对字典的补充,用于追踪值的出现次数. [具备字典所有的 ...

  5. C++基础 (7) 第七天 多态的原理 纯虚函数和抽象类 依赖倒置原则

    1 昨日回顾 2 多态的原理 1 要有继承 2 要有子类重写父类的虚函数 3 父类指针(或者引用)指向子类对象 (动态联编 虚函数表 3 证明vptr指针的存在 4 vptr指针在构造父类的时候是分步 ...

  6. HDU 1021 Fibonacci Again( 同余水 )

    链接:传送门 题意:现在给出 Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).问第 n 项能不能整除 ...

  7. [luogu2052 NOI2011] 道路修建 (树形dp)

    传送门 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...

  8. Mysql 忘记数据库密码

    windows下忘记MySQL密码的修改方法 1.关闭正在运行的Mysql服务: A.命令下运行   net stop mysql B.找到mysql服务停止mysql的服务 C.在任务管理器中结束M ...

  9. adb简单使用

    一.基本命令 1.查看目前连接的设备/虚拟器的状态 adb devices

  10. 在使用SSH+JPA开发中,ajax使用ObjectMapper类从后台向前台传值

    使用ObjectMapper对象的writeValue方法 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValu ...