树状数组进阶

· · 个人记录

目录

1 树状数组求逆序对

2 树状数组求第k大

3 树状数组求前缀/后缀最大值(整体)

4 树状数组求区间最大值

by laihaochen

1 树状数组求逆序对

我们先了解一下逆序对的定义:

同时满足i < j与a[i] > a[j]的a[i]和a[j]就是一组逆序对

通过树状数组——从区间谈起我们可以知道,树状数组可以很快的求出前缀。

所以我们令c[i]的前缀和表示a[1] ~ a[i]比他小的个数, 所以我们可以先把原数组逆序,再把依次进行update(a[i], 1)的操作, 这时每个query(a[i])就是逆序对个数了。

如果数据太大或有负数,存不下,就要使用离散化, 把原数组的值改成它在数组中第几大, 这样我们的树状数组只用开到N就可以了。

例题:P1908 逆序对

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 5e5 + 5;
int n, c[N];
long long ans;
inline int lowbit (int x) {
    return x & -x;
} 
void update (int x, int v) {
    for (; x <= n; x += lowbit(x)) {
        c[x] += v;
    }
} 
int query (int x) {
    int sum = 0;
    for (; x; x -= lowbit(x)) {
        sum += c[x];
    }
    return sum;
}
struct node {
    int v, id;
}a[N];
bool cmp (node x, node y) {
    if (x.v == y.v) {
        return x.id > y.id;
    }
    return x.v > y.v;
}
int main() {
    scanf ("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &a[i].v);
        a[i].id = i;
    }
    sort (a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
        update (a[i].id, 1);
        ans += query (a[i].id - 1);
    }
    printf ("%lld\n", ans);
    return 0;
} 

2 树状数组求第k大

这里使用这张图方便理解树状数组原理的另一种解释方法。

树状数组可以看作是一个二叉树,只不过边没有连, 但可以通过lowbit来访问,x的左节点为x - lowbit(x) / 2, 右节点为x + lowbit(x) / 2。

所以我们在求树状数组第k大时, 可以把k分解为若干个可以合成1 ~ x的不相交区间, 此时x即为第k大。

int kth (int k) {
    int res = 0;
    for (int i = 20; i >= 0; i--) {
        int tmp = res + (1 << i);
        if (tmp <= cnt && c[tmp] < k) {
            res += 1 << i;
            k -= c[tmp];
        }
    }
    return q[res + 1];
}

通过代码, 我们可以发现我们每次找c[2^i]做判断, 为什么呢?

因为我们相当于把第k大的下标进行二进制分解,每次都看c[tmp]的管辖区域有没有超过当前的k,如果没, 就往右节点移动,如果超了, 继续枚举第一个左结点, 一直下去。

这样我们就能从某一个左节点开始,一直往右延伸, 所以这些节点最终会形成互不相交的区间,并拼成一个大的区间,且这个区间里共有k个数。因为树状数组的下标就是原来数组中的数值,所以这个大区间的结尾就是第k大。

例题:P3369 【模板】普通平衡树

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int n, cnt, a[N], q[N], opt[N], c[N];
int geth (int x) {//离散化,找到a[i]在a中是第几大
    return lower_bound(q + 1, q + cnt + 1, x) - q;
}
inline int lowbit (int x) {
    return x & -x;
}
void update (int x, int v) {
    for (; x <= cnt; x += lowbit(x)) {
        c[x] += v;
    }
} 
int query (int x) {
    int res = 0;
    for (; x; x -= lowbit(x)) {
        res += c[x];
    }
    return res;
}
int kth (int k) {
    int res = 0;
    for (int i = 20; i >= 0; i--) {
        int tmp = res + (1 << i);
        if (tmp <= cnt && c[tmp] < k) {
            res += 1 << i;
            k -= c[tmp];
        }
    }
    return q[res + 1];//排名, 要在加一个一
}
int main() {
    scanf ("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf ("%d%d", &opt[i], &a[i]);
        if (opt[i] != 4) {
            q[++cnt] = a[i];
        }
      //除了第4种都要先进行离散化
    }
    sort (q + 1, q + cnt + 1);
    cnt = unique (q + 1, q + cnt + 1) - (q + 1);
   //unique去重,cnt为q数组去重后的长度
    for (int i = 1; i <= n; i++) {
        int ans;
        if (opt[i] == 1) {//增加一个数
            update (geth(a[i]), 1);
            continue;
        } else if (opt[i] == 2) {//删除一个数
            update (geth(a[i]), -1);
            continue;
        } else if (opt[i] == 3) {//求排名
            ans = query (geth(a[i]) - 1) + 1;
        } else if (opt[i] == 4) {//求第k大
            ans = kth (a[i]);
        } else if (opt[i] == 5) {//求前驱
            ans = kth (query (geth(a[i]) - 1));
        } else {//求后继
            ans = kth (query (geth(a[i])) + 1);
        }
        printf ("%d\n", ans);
    }
    return 0;
}

这道题还有一个坑点,就是求排名时, 我们 query(x)的定义是有几个数小于等于x, 因为可能有重复,所以我们要求出<= (比a[i]小的最大数)的数的个数, 再加一才是a[i]的排名。

同理,求前驱时,我们要求出前驱的排名query (geth(a[i]) - 1), 再求出排名是前驱排名的数即可。

求后继时,我们要求出后继的排名, 同理操作3只需把a[i] - 1换成a[i]即可。

3 树状数组求前缀/后缀最大值(整体)

例题:P1198 [JSOI2008]最大数

这道题就要用到后缀最大值。

处理后缀与前缀一样, 不过数组要逆序存储。

容易想到, 我们其实把c[i]的定义从和改成最大值即可。

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 2e6 + 5;
int n, d, c[N];
inline int lowbit (int x) {
    return x & -x;
}
void update (int x, int v) {
    for (; x <= n; x += lowbit(x)) {
        c[x] = max (c[x], v);
    } 
}
int query (int x) {
    int res = 0;
    for (; x; x -= lowbit(x)) {
        res = max (res, c[x]);
    }
    return res;
} 
int main() {
    scanf ("%d%d", &n, &d);
    char c;
    int x, t = 0, pos = n;
    for (int i = 1; i <= n; i++) {
        scanf ("\n%c%d", &c, &x);
        if (c == 'Q') {
            t = query (pos + x);
            printf ("%d\n", t);
        } else {
            update (pos--, (x + t) % d);
        }
    }
    return 0;
}

4 树状数组求区间最大值

树状数组居然也可以求区间最大值

int query (int l, int r) {
    int res = 0;
    while (l <= r) {
        for (; r > 0 && l <= r - lowbit(r) + 1; r -= lowbit(r)) {
            res = max (res, c[r]);
        } 
        if (l <= r) {
            res = max (res, a[r--]);
        }
    }
    return res;
}

这时问题出现了,怎么只用c存储所有区间的最大值?

所以我们需要把原始数组存下来。

每当现有区间足够c[r]就直接res = max (res, c[r])

如果不够,就res = max (res, a[r--])

注意这里一定要加上判断if (l <= r), 因为当r刚好减到l时,就不能再减了。

例题:P1531 I Hate It

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 200005;
int n, m, a[N], c[N];
inline int lowbit (int x) {
    return x & -x;
}
void update (int x, int v) {
    a[x] = v;
    for (; x <= n; x += lowbit(x)) {
        c[x] = max (c[x], v);
    }
}
int query (int l, int r) {
    int res = 0;
    while (l <= r) {
        for (;r > 0 && l <= r - lowbit(r) + 1; r -= lowbit(r)) {
            res = max (res, c[r]);
        } 
        if (l <= r) {
            res = max (res, a[r--]);
        }
    }
    return res;
}
int main() {
    scanf ("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &a[i]);
        update (i, a[i]);
    }
    char ch[2];
    int x, y;
    for (int i = 1; i <= m; i++) {
        scanf ("%s%d%d", ch, &x, &y);
        if (ch[0] == 'Q') {
            printf ("%d\n", query (x, y));
        } else {
            if (a[x] < y) {
                update (x, y);
            }
        }
    }
    return 0;
}