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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
typedef pair pii;
typedef unsigned long long uLL;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#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)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<>= 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\n", ans);
    return 0;
}

你可能感兴趣的:(P3521 [POI2011]ROT-Tree Rotations (线段树合并))