zoukankan      html  css  js  c++  java
  • codeforces 540E 离散化技巧+线段树/树状数组求逆序对

    传送门:https://codeforces.com/contest/540/problem/E

    题意:

    有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆序对个数为多少

    题解:

    因为值域范围为1e9,而n的范围只有1e5,所以我们肯定是不能直接交换的,对n次操作离散化,离散化后的数组最大为2e5,这里需要用到一些离散化的技巧。

    将每次交换的u,v两个点放到map里面,键为pos,值为0

    然后对于map映射,值就是离散化后的下标

    离散化后我们应该做什么呢?

    首先,我们将求逆序对数分为两个部分

    PART1:位置为u的数与位置为v 的数交换产生的逆序对数 cnt1

    PART2: 位置为u的数是被交换过的,他和没有被交换过的数产生的逆序对数 cnt2

    f[i]表示交换序列中,离散化后,处于第i个位置上面的值(也是离散化之前的位置)

    p[i]表示现在在i位置上的数初始是在p[i]位置(位置是离散化后的从小到大的顺序,因此是对于交换序列的(因为只有交换序列才离散化了))

    对于处于交换序列中的某个数,其初始位置和最终位置之间有多少个数,答案为:abs(f[p[i]]-f[i])-1

    但是这两个位置之间,处于交换序列中的数在之前的第一部分已经算过了(也就是两个数都是被交换过的数)

    所以我们需要减去 两个位置之间,已经处于交换序列的个数(不包含两个位置)

    结果为abs(i-p[i])-1 (交换序列中 i和p[i]位置之间,有abs(i-p[i])-1个数)

    [cnt_1= i-get\_sum(p[i]);\ cnt_2= abs(f[p[i]]-f[i]) – abs(i-p[i]) ]

    代码:

    //线段树版本
    /**
     *        ┏┓    ┏┓
     *        ┏┛┗━━━━━━━┛┗━━━┓
     *        ┃       ┃  
     *        ┃   ━    ┃
     *        ┃ >   < ┃
     *        ┃       ┃
     *        ┃... ⌒ ...  ┃
     *        ┃       ┃
     *        ┗━┓   ┏━┛
     *          ┃   ┃ 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 <deque>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <ctime>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    typedef pair<LL, LL> pLL;
    typedef pair<LL, int> pLi;
    typedef pair<int, LL> pil;;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    #define ls rt<<1
    #define rs rt<<1|1
    #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 double eps = 1e-8;
    const int mod = 1e9 + 7;
    const int maxn = 3e5 + 5;
    const int INF = 0x3f3f3f3f;
    const LL INFLL = 0x3f3f3f3f3f3f3f3f;
    
    int n;
    int u[maxn];
    int v[maxn];
    int f[maxn], p[maxn];
    map<int, int>mp;
    LL sum[maxn << 2];
    void PushUp(int rt) {
        sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
    }
    void update( int p, LL sc, int l, int r, int rt) {
        if (l == r) {
            sum[rt] += sc;
            return ;
        }
        int mid = (l + r) >> 1;
        if (p <= mid) update(p, sc, lson);
        else update(p, sc, rson);
        PushUp(rt);
    }
    LL query(int L, int R, int l, int r, int rt) {
        if (L <= l && r <= R) return sum[rt];
        int mid = (l + r) >> 1;
        LL ret = 0LL;
        if (L <= mid) ret += query(L, R, lson);
        if (R > mid) ret += query(L, R, rson);
        return ret;
    }
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        scanf("%d", &n);
        mp.clear();
        memset(p, 0, sizeof(p));
        for ( int i = 1 ; i <= n ; i++) {
            scanf("%d%d", &u[i], &v[i]);
            mp[u[i]] = 0 ;
            mp[v[i]] = 0;
        }
        int cnt = 0 ;
        for ( auto it = mp.begin() ; it != mp.end() ; it++) {
            it->second = ++cnt;
            f[cnt] = it->first;
            p[cnt] = cnt;
        }
        for ( int i = 1 ; i <= n ; i++) u[i] = mp[u[i]], v[i] = mp[v[i]];
        for ( int i = 1 ; i <= n ; i++) swap(p[u[i]], p[v[i]]);
        LL ans = 0LL;
        for ( LL i = 1; i <= cnt ; i++) {
            update(p[i], 1, 1, cnt, 1);
            ans += i - query(1, p[i], 1, cnt, 1);
            ans += abs(f[i] - f[p[i]]) - abs(i - p[i]);
        }
        printf("%lld
    ", ans);
        return 0;
    }
    
    
    //树状数组版本
    
    /**
     *        ┏┓    ┏┓
     *        ┏┛┗━━━━━━━┛┗━━━┓
     *        ┃       ┃  
     *        ┃   ━    ┃
     *        ┃ >   < ┃
     *        ┃       ┃
     *        ┃... ⌒ ...  ┃
     *        ┃       ┃
     *        ┗━┓   ┏━┛
     *          ┃   ┃ 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 <deque>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <ctime>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    typedef pair<LL, LL> pLL;
    typedef pair<LL, int> pLi;
    typedef pair<int, LL> pil;;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    #define ls rt<<1
    #define rs rt<<1|1
    #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 double eps = 1e-8;
    const int mod = 1e9 + 7;
    const int maxn = 3e5 + 5;
    const int INF = 0x3f3f3f3f;
    const LL INFLL = 0x3f3f3f3f3f3f3f3f;
    
    int n;
    int u[maxn];
    int v[maxn];
    int f[maxn], p[maxn];
    map<int, int>mp;
    int cnt;
    int bit[maxn];
    
    int lowbit(int x) {
        return x & -x;
    }
    void add(int pos, int x) {
        while(pos <= cnt) {
            bit[pos] += x;
            pos += lowbit(pos);
        }
    }
    LL sum(int pos) {
        LL ans = 0;
        while(pos) {
            ans += bit[pos];
            pos -= lowbit(pos);
        }
        return ans;
    }
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        scanf("%d", &n);
        mp.clear();
        memset(p, 0, sizeof(p));
        for ( int i = 1 ; i <= n ; i++) {
            scanf("%d%d", &u[i], &v[i]);
            mp[u[i]] = 0 ;
            mp[v[i]] = 0;
        }
        cnt = 0;
        for ( auto it = mp.begin() ; it != mp.end() ; it++)     {
            it->second = ++cnt;
            f[cnt] = it->first;
            p[cnt] = cnt;
        }
        for ( int i = 1 ; i <= n ; i++) u[i] = mp[u[i]], v[i] = mp[v[i]];
        for ( int i = 1 ; i <= n ; i++) swap(p[u[i]], p[v[i]]);
        LL ans = 0LL;
        for ( LL i = 1; i <= cnt ; i++) {
            add(p[i], 1);
            ans += i - sum(p[i]);
            ans += abs(f[i] - f[p[i]]) - abs(i - p[i]);
        }
        printf("%lld
    ", ans);
        return 0;
    }
    
    
    
    
    
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    scnner02 (nextLine)
    Scanner01
    Spring 框架 (初学)
    查询自己写了多少行代码
    jdbc事务
    jdbc(预编译插入数据)
    jdbc(java连接数据库)
    监听器扩展
    listener(监听器)
    Filter过滤器
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/10853847.html
Copyright © 2011-2022 走看看