zoukankan      html  css  js  c++  java
  • P3521 [POI2011]ROT-Tree Rotations (线段树合并)

    P3521 [POI2011]ROT-Tree Rotations

    题意:

    给你一颗树,只有叶子节点有权值,你可以交换一个点的左右子树,问你最小的逆序对数

    题解:

    线段树维护权值个个数即可

    然后左右子树合并时计算交换和不交换的贡献取一个min即可

    代码:

    /**
     *        ┏┓    ┏┓
     *        ┏┛┗━━━━━━━┛┗━━━┓
     *        ┃       ┃  
     *        ┃   ━    ┃
     *        ┃ >   < ┃
     *        ┃       ┃
     *        ┃... ⌒ ...  ┃
     *        ┃       ┃
     *        ┗━┓   ┏━┛
     *          ┃   ┃ Code is far away from bug with the animal protecting          
     *          ┃   ┃   神兽保佑,代码无bug
     *          ┃   ┃           
     *          ┃   ┃        
     *          ┃   ┃
     *          ┃   ┃           
     *          ┃   ┗━━━┓
     *          ┃       ┣┓
     *          ┃       ┏┛
     *          ┗┓┓┏━┳┓┏┛
     *           ┃┫┫ ┃┫┫
     *           ┗┻┛ ┗┻┛
     */
    // warm heart, wagging tail,and a smile just for you!
    //
    //                            _ooOoo_
    //                           o8888888o
    //                           88" . "88
    //                           (| -_- |)
    //                           O  =  /O
    //                        ____/`---'\____
    //                      .'  |     |//  `.
    //                     /  |||  :  |||//  
    //                    /  _||||| -:- |||||-  
    //                    |   |   -  /// |   |
    //                    | \_|  ''---/''  |   |
    //                      .-\__  `-`  ___/-. /
    //                  ___`. .'  /--.--  `. . __
    //               ."" '<  `.___\_<|>_/___.'  >'"".
    //              | | :  `- \`.;` _ /`;.`/ - ` : | |
    //                 `-.   \_ __ /__ _/   .-` /  /
    //         ======`-.____`-.___\_____/___.-`____.-'======
    //                            `=---='
    //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                     佛祖保佑      永无BUG
    #include <set>
    #include <map>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <bitset>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define bug printf("*********
    ")
    #define FIN freopen("input.txt","r",stdin);
    #define FON freopen("output.txt","w+",stdout);
    #define IO ios::sync_with_stdio(false),cin.tie(0)
    #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
    "
    #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    const int maxn = 3e5 + 5;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    const double Pi = acos(-1);
    LL gcd(LL a, LL b) {
        return b ? gcd(b, a % b) : a;
    }
    LL lcm(LL a, LL b) {
        return a / gcd(a, b) * b;
    }
    double dpow(double a, LL b) {
        double ans = 1.0;
        while(b) {
            if(b % 2)ans = ans * a;
            a = a * a;
            b /= 2;
        } return ans;
    }
    LL quick_pow(LL x, LL y) {
        LL ans = 1;
        while(y) {
            if(y & 1) {
                ans = ans * x % mod;
            } x = x * x % mod;
            y >>= 1;
        } return ans;
    }
    struct node {
        int l, r, sum;
    } tree[maxn * 40];
    int tree_cnt;
    int root[maxn];
    void update(int &x, int l, int r, int val) {
        if(!x) x = ++tree_cnt;
        tree[x].sum++;
        if(l == r) return;
        int mid = (l + r) >> 1;
        if(val <= mid) update(tree[x].l, l, mid, val);
        else update(tree[x].r, mid + 1, r, val);
    }
    int n;
    LL num1, num2, ans = 0;
    void merge(int &x, int y) {
        if(!x || !y) {
            x = x + y;
            return;
        }
    
        tree[x].sum += tree[y].sum;
        num1 += 1LL * tree[tree[x].l].sum * tree[tree[y].r].sum;
        num2 += 1LL * tree[tree[x].r].sum * tree[tree[y].l].sum;
    
        merge(tree[x].l, tree[y].l);
        merge(tree[x].r, tree[y].r);
    }
    void dfs(int &x) {
        int val;
        scanf("%d", &val);
        int ls = 0, rs = 0;
        if(!val) {
            dfs(ls);
            dfs(rs);
            num1 = num2 = 0;
            x = ls;
            merge(x, rs);
            ans += min(num1, num2);
        } else {
            update(x, 1, n, val);
        }
    }
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
    
        scanf("%d", &n);
        int x = 0;
        dfs(x);
        printf("%lld
    ", ans);
        return 0;
    }
    

  • 相关阅读:
    AjaxEvent、AjaxMethod和Listeners的区别
    chm 文档突然打不开了
    ComboBox实现联动时所遇问题,待解!
    Silverlight 3 中使用WCF上传文件 (简单进度条展示)
    Eclipse 使用ApacheAnt 小记
    ApacheAnt 基础知识
    EClipse + Jdk + ApacheAnt + jetty + GWT + MySQL(Navicat)
    限制文本框输入N个字符的使用
    JAVA编程经验汇总 (载)
    Visual Studio 2010: Addin 的目录设置及其项目模板位置
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11656713.html
Copyright © 2011-2022 走看看