树状数组的区间修改与区间查询

【题意】

【分析】

学了带懒标记的线段树后,这就并不难了。

#include
#include
#include
#define ll long long
using namespace std;
const int mn = 100005;
struct seg{
    int l, r;
    ll s, h;
}t[mn << 3];
ll a[mn];
void make_tree(int i, int l, int r)
{
    t[i] = (seg) {l, r, 0, 0};
    if(l == r)
    {
        t[i].s = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    make_tree(i << 1, l, mid);
    make_tree((i << 1) | 1, mid + 1, r);
    t[i].s = t[i << 1].s + t[(i << 1) | 1].s;
}
void edit_tree(int i, int l, int r, ll d)
{
    t[i].s += (t[i].r - t[i].l + 1) * t[i].h;
    t[i << 1].h += t[i].h, t[(i << 1) | 1].h += t[i].h, t[i].h = 0;
    if(t[i].l > r || t[i].r < l)
        return;
    if(t[i].l == l && t[i].r == r)
    {
        t[i].s += (t[i].r - t[i].l + 1) * d;
        t[i << 1].h += d, t[(i << 1) | 1].h += d;
        return;
    }
    int mid = (t[i].l + t[i].r) >> 1;
    if(r <= mid || l > mid)
    {
        edit_tree(i << 1, l, r, d);
        edit_tree((i << 1) | 1, l, r, d);
    }
    else
    {
        edit_tree(i << 1, l, mid, d);
        edit_tree((i << 1) | 1, mid + 1, r, d);
    }
    t[i].s = t[i << 1].s + t[(i << 1) | 1].s;
}
ll get_sum(int i, int l, int r)
{
    t[i].s += (t[i].r - t[i].l + 1) * t[i].h;
    t[i << 1].h += t[i].h, t[(i << 1) | 1].h += t[i].h, t[i].h = 0;
    if(t[i].l > r || t[i].r < l)
        return 0;
    if(t[i].l == l && t[i].r == r)
        return t[i].s;
    int mid = (t[i].l + t[i].r) >> 1;
    ll ret = 0;
    if(r <= mid || l > mid)
        ret = get_sum(i << 1, l, r) + get_sum((i << 1) | 1, l, r);
    else
        ret = get_sum(i << 1, l, mid) + get_sum((i << 1) | 1, mid + 1, r);
    t[i].s = t[i << 1].s + t[(i << 1) | 1].s;
    return ret;
}
int main()
{
    int n, m, i;
    int T, x, y;
    ll z;
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    make_tree(1, 1, n);
    while(m--)
    {
        scanf("%d%d%d", &T, &x, &y);
        if(T == 2)
            printf("%lld\n", get_sum(1, x, y));
        else
        {
            scanf("%lld", &z);
            edit_tree(1, x, y, z);
        }
    }
}

然而个人觉得这个代码跑得飞慢

正好这段时间在玩整体二分,所以就学习了一下用树状数组进行区间修改与区间查询。
我们以支持区间修改、单点查询的树状数组为基础进行升级改造。在这个树状数组中,我们维护了一个差分数组 d [ i ] d[i] d[i],则:
a [ x ] = ∑ i = 1 x d [ i ] a[x]=\sum_{i=1}^xd[i] a[x]=i=1xd[i]
如今我们想求 ∑ i = 1 x a [ i ] \sum_{i=1}^{x}a[i] i=1xa[i],所以:
∑ i = 1 x a [ i ] = ∑ i = 1 x ∑ j = 1 i d [ i ] \sum_{i=1}^xa[i]=\sum_{i=1}^x\sum_{j=1}^id[i] i=1xa[i]=i=1xj=1id[i]
在这个和式中, d [ 1 ] d[1] d[1]计算了 x x x次, d [ 2 ] d[2] d[2]计算了 x − 1 x-1 x1次,…, d [ i ] d[i] d[i]计算了 x − i + 1 x-i+1 xi+1次。所以:
∑ i = 1 x a [ i ] = ∑ i = 1 x ( x − i + 1 ) d [ i ] = ( x + 1 ) ∑ i = 1 x d [ i ] − ∑ i = 1 x d [ i ] i \sum_{i=1}^xa[i]=\sum_{i=1}^x(x-i+1)d[i]=(x+1)\sum_{i=1}^xd[i]-\sum_{i=1}^xd[i]i i=1xa[i]=i=1x(xi+1)d[i]=(x+1)i=1xd[i]i=1xd[i]i
这样一来,我们可以维护两个树状数组:
c 1 [ x ] = ∑ i = 1 x d [ i ]                           c 2 [ x ] = ∑ i = 1 x d [ i ] i c1[x]=\sum_{i=1}^xd[i]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ c2[x]=\sum_{i=1}^xd[i]i c1[x]=i=1xd[i]                         c2[x]=i=1xd[i]i
具体维护与普通树状数组类似。

【代码】

#include
#define ll long long
using namespace std;
const int mn = 100005;
ll c1[mn], c2[mn]; int n;
inline int lowbit(int x) {return x & -x;}
inline void update(int p, ll v)
{
    for(int i = p; i <= n; i += lowbit(i))
        c1[i] += v, c2[i] += v * p;
}
inline ll getsum(int p)
{
    ll res = 0;
    for(int i = p; i; i -= i & -i)
        res += (p + 1) * c1[i] - c2[i];
    return res;
}
int main()
{
    int m, t, l, r;
    ll x;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%lld", &x), update(i, x), update(i + 1, -x);
    while(m--)
    {
        scanf("%d%d%d", &t, &l, &r);
        if(t == 1)
            scanf("%lld", &x), update(l, x), update(r + 1, -x);
        else
            printf("%lld\n", getsum(r) - getsum(l - 1));
    }
}

你可能感兴趣的:(数据结构)