[BZOJ 2141] 排队

[题目链接]

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

[算法]

          首先求出原序列中的逆序对个数

          考虑交换两个数的位置 , 对答案产生的影响

          显然 , (x , y)对答案的贡献为 : -(x对答案的贡献) - (y对答案的贡献) + x的新贡献 + y的新贡献

          我们需要快速计算一个形如" 区间[l , r]中大于 / 小于某个数的数有多少个"的问题

          树状数组套主席树即可 , 时间复杂度 : O(NlogN ^ 2)

[代码]

        

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

int n , m , len , L , R;
int a[MAXN] , tmp[MAXN] , fen[MAXN] , root[MAXN] , ql[MAXLOG] , qr[MAXLOG];

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 , len , 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 , len , 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 , len , 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);
        for (int i = 1; i <= n; i++) 
        {
                read(a[i]);
                tmp[i] = a[i];
        }
        sort(tmp + 1 , tmp + n + 1);
        len = unique(tmp + 1 , tmp + n + 1) - tmp - 1;
        for (int i = 1; i <= n; i++) a[i] = lower_bound(tmp + 1 , tmp + len + 1 , a[i]) - tmp;
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
                ans += (ll)ask(a[i] + 1 , len);
                change(a[i] , 1); 
        }
        printf("%d\n" , ans);
        for (int i = 1; i <= n; i++) BIT.modify(i , a[i] , 1);
        read(m);
        for (int i = 1; i <= m; i++)
        {
                int x , y;
                read(x); read(y);
                if (x > y) swap(x , y);
                ans += BIT.queryA(x + 1 , y - 1 , a[x]);
                ans -= BIT.queryB(x + 1 , y - 1 , a[x]);
                ans += BIT.queryB(x + 1 , y - 1 , a[y]);
                ans -= BIT.queryA(x + 1 , y - 1 , a[y]);
                if (a[x] > a[y]) --ans;
                if (a[x] < a[y]) ++ans;
                printf("%lld\n" , ans);
                BIT.modify(x , a[x] , -1);
                BIT.modify(y , a[y] , -1);
                swap(a[x] , a[y]);
                BIT.modify(x , a[x] , 1);
                BIT.modify(y , a[y] , 1);
        }
        
        return 0;
    
}

 

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