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

来源:互联网 发布:c语言表示10的n次方 编辑:程序博客网 时间:2024/06/06 13:14

线段树是一棵二叉树,树中的每一个结点表示了一个区间[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-17.初始区间的大小为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: 131072KTotal Submissions: 94305Accepted: 29382Case 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

POJ Monthly–2007.11.25, Yang Yi

·
·
·

无耻上代码

#include<cstdio>#include<cstring>#include<algorithm>#define maxn 100010using 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