BZOJ 2212线段树的合并

借鉴()了一下题解……
线段树合并的裸题吧…

//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);
}

这里写图片描述

转载于:https://www.cnblogs.com/SiriusRen/p/6532045.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值