zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",ans);
    }

    这里写图片描述

  • 相关阅读:
    CentOS 下搭建Jenkins
    SRVE0255E: 尚未定义要处理 ***的 Web 组/虚拟主机。
    WebSphere Application Server中manageprofiles的使用
    WAS 与IHS集成问题
    CentOS ln 链接
    VIM常见命令
    虚拟机VM下CentOS7部署WASND9+HTTP9
    CentOS7下安装GUI图形界面
    CentOS 系统时间与硬件时间
    hive 排序和聚集
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532045.html
Copyright © 2011-2022 走看看