????(1):???(???)

线段树是一棵二叉树,树中的每一个结点表示了一个区间[a,b]。

a,b通常是整数。每一个叶子节点表示了一个单位区间(长度为1)。

对于每一个非叶结点所表示的结点[a,b],其左儿子表示的区间为[a,(a+b)/2],右儿子表示的区间为[(a+b)/2+1,b](除法去尾取整)。

特点:

1.每个区间的长度是区间内整数的个数
2.叶子节点长度为1,不能再往下分
3.若一个节点对应的区间是[a,b],则其子节点对应的区间分别是[a,(a+b)/2]和[ (a+b)/2+1,b] (除法去尾取整)
4.线段树的平分构造,实际上是用了二分的方法。若根节点对应的区间是[a,b],那么它的深度为log2(b-a+1) +1 (向上取整)。
5.叶子节点的数目和根节点表示区间的长度相同.
6.线段树节点要么0度,要么2度, 因此若叶子节点数目为N,则线段树总结点数目为2N-1
7.初始区间的大小为N, 那么线段树的最终节点数为(4*N-1)

建树要求:

1.如果有某个节点代表的区间,完全属于待分解区间,则该节点为“终止”节点,不再继续往下分解
2.所有“终止”节点所代表的区间都不重叠,且加在一起就恰好等于整个待分解区间
3.区间分解的时候,每层最多2个“终止节点”,所以 终止节点总数也是log(n)量级的

使用特征

1.线段树的深度不超过log2(n)+1(向上取整,n是根节点对应区间的长度)。
2.线段树上,任意一个区间被分解后得到的“终止节点”数目都是log(n)量级。
3.线段树上更新叶子节点和进行区间分解时间复杂度都是O(log(n))的
4.线段树能在O(log(n))的时间内完成插入数据,更新数据、查找、统计等工作

使用注意

1.必须是对区间所对应的一些数据进行修改,过程和查询类似操作
2.用线段树解题,关键是要想清楚每个节点要存哪些信息(当然区间起终点,以及左右子节点指针是必须的)
3.这些信息如何高效更新,维护,查询。不要一更新就更新到叶子节点,那样更新效率最坏就可能变成O(n)的了。
4.先建树,然后插入数据,然后更新,查询。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Language:Default
A Simple Problem with Integers

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 94305Accepted: 29382
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
”C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
”Q a b” means querying the sum of Aa, Aa+1, … , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5 
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4 
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source


·
·
·

无耻上代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100010

using namespace std;

struct node {
    int l, r;
    long long sum, add;
}tree[maxn << 2];

void pushup(int root)
{
    tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
}

void pushdown(int root, int m)
{
    if (tree[root].add)
    {
        tree[root << 1].add += tree[root].add;
        tree[root << 1 | 1].add += tree[root].add;
        tree[root << 1].sum += tree[root].add * (m - (m >> 1));
        tree[root << 1 | 1].sum += tree[root].add * (m >> 1);
        tree[root].add = 0;
    }
}

void build(int l, int r, int root)
{
    tree[root].l = l;
    tree[root].r = r;
    tree[root].add = 0;
    if (l == r)
    {
        scanf("%lld", &tree[root].sum);
        return;
    }
    int mid = (tree[root].l + tree[root].r) / 2;
    build(l, mid, root << 1);
    build(mid + 1, r, root << 1 | 1);
    pushup(root);
}

void update(int c, int l, int r, int root)
{
    if (tree[root].l == l && tree[root].r == r)
    {
        tree[root].add += c;
        tree[root].sum += (long long)c * (r - l + 1);
        return;
    }
    if (tree[root].l == tree[root].r)
        return;

    pushdown(root, tree[root].r - tree[root].l + 1);

    int mid = (tree[root].l + tree[root].r) >> 1;
    if (r <= mid)
        update(c, l, r, root << 1);
    else if (l > mid)
        update(c, l, r, root << 1 | 1);
    else
    {
        update(c, l, mid, root << 1);
        update(c, mid + 1, r, root << 1 | 1);
    }

    pushup(root);
}

long long query(int l, int r, int root)
{
    if (l == tree[root].l && r == tree[root].r)
        return tree[root].sum;

    pushdown(root, tree[root].r - tree[root].l + 1);

    int mid = (tree[root].r + tree[root].l) >> 1;
    long long res = 0;
    if (r <= mid)
        res += query(l, r, root << 1);
    else if (l > mid)
        res += query(l, r, root << 1 | 1);
    else
    {
        res += query(l, mid, root << 1);
        res += query(mid + 1, r, root << 1 | 1);
    }

    return res;
}

int main(void)
{
    int n, m;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        build(1, n, 1);
        while (m--)
        {
            char ch[2];
            scanf("%s", ch);
            int a, b, c;
            if (ch[0] == 'Q')
            {
                scanf("%d %d", &a, &b);
                printf("%lld\n", query(a, b, 1));
            }
            else
            {
                scanf("%d %d %d", &a, &b, &c);
                update(c, a, b, 1);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值