BZOJ 3295 动态逆序对

树状数组套平衡树

最直接的解法应该是O(nlogn)的带修主席树,但是还是可以树状数组套平衡树乱搞的。。多了一个log。。

因为leaf tree常数比较小,所以就随意任性了~

删除一个数的时候,减去左边比这个数大数和右边比这个数小的数即可

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
#define newNode(a, b, c, d) (&(*pool[cnt++] = Node(a, b, c, d)))
#define merge(a, b) newNode(a->size + b->size, b->val, a, b)
#define push_up(rt) if(rt->lf->size) rt->size = rt->lf->size + rt->rf->size, rt->val = rt->rf->val
#define ratio 4
const int N = 5000000;
int n, m, cnt, pos[100005], tree[100005], val[100005];
struct Node{
    int size, val;
    Node *lf, *rf;
    Node(){}
    Node(int size, int val, Node *lf, Node *rf): size(size), val(val), lf(lf), rf(rf){}
}*null, *root[100005], *pool[N], t[N];

inline void maintain(Node *cur){
    if(cur->lf->size > cur->rf->size * ratio)
        cur->rf = merge(cur->lf->rf, cur->rf), pool[--cnt] = cur->lf, cur->lf = cur->lf->lf;
    if(cur->rf->size > cur->lf->size * ratio)
        cur->lf = merge(cur->lf, cur->rf->lf), pool[--cnt] = cur->rf, cur->rf = cur->rf->rf;
}

void insert(Node *cur, int val){
    if(cur->size == 1){
        cur->lf = newNode(1, min(val, cur->val), null, null);
        cur->rf = newNode(1, max(val, cur->val), null, null);
    }
    else insert(val > cur->lf->val ? cur->rf : cur->lf, val);
    push_up(cur), maintain(cur);
}

void erase(Node *cur, int val){
    if(cur->lf->size == 1 && cur->lf->val == val)
        pool[--cnt] = cur->lf, pool[--cnt] = cur->rf, *cur = *cur->rf;
    else if(cur->rf->size == 1 && cur->rf->val == val)
        pool[--cnt] = cur->rf, pool[--cnt] = cur->lf, *cur = *cur->lf;
    else erase(val > cur->lf->val ? cur->rf : cur->lf, val);
    push_up(cur), maintain(cur);
}

int find(Node *cur, int val){
    if(cur->size == 1) return 1;
    return val > cur->lf->val ? cur->lf->size + find(cur->rf, val) : find(cur->lf, val);
}

inline void add(int t, int val){
    for(; t <= n; t += lowbit(t))
        insert(root[t], val);
}

inline void sub(int t, int val){
    for(; t <= n; t += lowbit(t))
        erase(root[t], val);
}

inline int queryL(int t, int val){
    int ret = 0;
    for(; t; t -= lowbit(t))
        ret += root[t]->size - 1 - (find(root[t], val) - 1);
    return ret;
}

inline int queryR(int t, int val){
    int ret = 0;
    for(; t; t -= lowbit(t))
        ret += find(root[t], val) - 1;
    return ret;
}

inline void add(int t){
    for(; t <= n; t += lowbit(t))
        tree[t] += 1;
}

inline int record(int t){
    int ret = 0;
    for(; t; t -= lowbit(t))
        ret += tree[t];
    return ret;
}

int main(){

    for(int i = 0; i < N; i ++) pool[i] = &t[i];
    null = new Node(0, 0, nullptr, nullptr);
    n = read(), m = read();
    for(int i = 1; i <= n; i ++) root[i] = new Node(1, INF, null, null);
    for(int i = 1; i <= n; i ++){
        val[i] = read();
        add(i, val[i]), pos[val[i]] = i;
    }
    ll res = 0;
    for(int i = 1; i <= n; i ++){
        add(val[i]);
        res += i - record(val[i]);
    }
    while(m --){
        printf("%lld\n", res);
        int x = read(); ll ans = 0;
        ll tmp = queryL(pos[x] - 1, x);
        ans += tmp;
        ll a = queryR(n, x), b = queryR(pos[x], x);
        ans += a - b;
        res -= ans;
        sub(pos[x], x);
    }
    return 0;
}
posted @ 2019-06-14 14:49  清楚少女ひなこ  阅读(100)  评论(0编辑  收藏  举报