[CQOI 2011] 动态逆序对

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=3295

[算法]

        记Lx表示第x个数的出现位置

        显然 , 每次删去一个数 , 逆序对数减少([1 , Lx - 1]中 > x的数的个数 + [Lx + 1 , n]中 < x的数的个数)个

        树状数组套主席树动态维护逆序对即可

        时间复杂度 : O(NlogN ^ 2)

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define MAXP 10000010
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

int n , m , L , R;
int a[MAXN] , fen[MAXN] , loc[MAXN] , root[MAXN] , ql[MAXN] , qr[MAXN];

struct Presitent_Segment_Tree
{
        int sz;
        int ls[MAXP] , rs[MAXP] , cnt[MAXP];
        Presitent_Segment_Tree()
        {
                sz = 0;
                memset(ls , 0 , sizeof(ls));
                memset(rs , 0 , sizeof(rs));
                memset(cnt , 0 , sizeof(cnt));
        }
        inline void modify(int &k , int old , int l , int r , int pos , int value)
        {
                if (!k) k = ++sz;
                ls[k] = ls[old] , rs[k] = rs[old];
                cnt[k] = cnt[old] + value;
                if (l == r) return;
                int mid = (l + r) >> 1;
                if (mid >= pos) modify(ls[k] , ls[k] , l , mid , pos , value);
                else modify(rs[k] , rs[k] , mid + 1 , r , pos , value);
        }
        inline int queryA(int l , int r , int k)
        {
                if (r <= k) return 0;
                if (l > k)
                {
                        int ret = 0;
                        for (int i = 1; i <= L; i++) ret -= cnt[ql[i]];
                        for (int i = 1; i <= R; i++) ret += cnt[qr[i]];
                        return ret;
                }
                int mid = (l + r) >> 1;
                if (mid >= k)
                {
                        int ret = 0;
                        for (int i = 1; i <= L; i++) ret -= cnt[rs[ql[i]]];
                        for (int i = 1; i <= R; i++) ret += cnt[rs[qr[i]]];
                        for (int i = 1; i <= L; i++) ql[i] = ls[ql[i]];
                        for (int i = 1; i <= R; i++) qr[i] = ls[qr[i]];
                        ret += queryA(l , mid , k);
                        return ret;
                } else 
                {
                        for (int i = 1; i <= L; i++) ql[i] = rs[ql[i]];
                        for (int i = 1; i <= R; i++) qr[i] = rs[qr[i]];
                        return queryA(mid + 1 , r , k);
                }
        }
        inline int queryB(int l , int r , int k)
        {
                if (l >= k) return 0;
                if (r < k)
                {
                        int ret = 0;
                        for (int i = 1; i <= L; i++) ret -= cnt[ql[i]];
                        for (int i = 1; i <= R; i++) ret += cnt[qr[i]];
                        return ret;
                }
                int mid = (l + r) >> 1;
                if (mid < k)
                {
                        int ret = 0;
                        for (int i = 1; i <= L; i++) ret -= cnt[ls[ql[i]]];
                        for (int i = 1; i <= R; i++) ret += cnt[ls[qr[i]]];
                        for (int i = 1; i <= L; i++) ql[i] = rs[ql[i]];
                        for (int i = 1; i <= R; i++) qr[i] = rs[qr[i]];
                        ret += queryB(mid + 1 , r , k);
                        return ret;
                } else
                {
                        for (int i = 1; i <= L; i++) ql[i] = ls[ql[i]];
                        for (int i = 1; i <= R; i++) qr[i] = ls[qr[i]];
                        return queryB(l , mid , k);
                }
        }
} PST;
struct Binary_Indexed_Tree
{
        inline int lowbit(int x)
        {
                return x & (-x);
        }
        inline void modify(int pos , int x , int value)
        {
                for (int i = pos; i <= n; i += lowbit(i))
                        PST.modify(root[i] , root[i] , 1 , n , x , value);        
        }        
        inline int queryA(int l , int r , int k)
        {
                if (l > r) return 0;
                L = 0 , R = 0;
                for (int i = l - 1; i >= 1; i -= lowbit(i)) ql[++L] = root[i];
                for (int i = r; i >= 1; i -= lowbit(i)) qr[++R] = root[i];
                return PST.queryA(1 , n , k);
        }
        inline int queryB(int l , int r , int k)
        {
                if (l > r) return 0;
                L = 0 , R = 0;
                for (int i = l - 1; i >= 1; i -= lowbit(i)) ql[++L] = root[i];
                for (int i = r; i >= 1; i -= lowbit(i)) qr[++R] = root[i];
                return PST.queryB(1 , n , k);
        }
} BIT;

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
inline int lowbit(int x)
{
        return x & (-x);
}
inline void change(int x , int w)
{
        while (x <= n)
        {
                fen[x] += w;
                x += lowbit(x);
        }
}
inline int ask(int x)
{
        int ret = 0;
        while (x >= 1)
        {
                ret += fen[x];
                x -= lowbit(x);
        }
        return ret;
}
inline int ask(int l , int r)
{
        return ask(r) - ask(l - 1);
}

int main()
{
        
        read(n); read(m);
        for (int i = 1; i <= n; i++)
        {
                read(a[i]);
                loc[a[i]] = i;
        }
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
                ans += ask(a[i] + 1 , n);
                change(a[i] , 1);    
        }
        for (int i = 1; i <= n; i++) BIT.modify(i , a[i] , 1);
        for (int i = 1; i <= m; i++)
        {
                int x;
                read(x);
                printf("%lld\n" , ans);
                ans -= BIT.queryA(1 , loc[x] - 1 , x);
                ans -= BIT.queryB(loc[x] + 1 , n , x);
                BIT.modify(loc[x] , x , -1);
        }
        
        return 0;
    
}

 

posted @ 2018-12-29 22:11  evenbao  阅读(159)  评论(0编辑  收藏  举报