题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值。

分析:很明显我们不可能对区间的每个点都进行更新,不过我们可以使用一个op的开关,如果op等于0说明这个不需要更新,如果等于1就说明需要进行更新,这样只需要和插入的时候进行一下更新即可
***********************************************************************
注意:在向下更新的时候如果是更新子树有可能一个区间会更新多次,而子树区间更新的时候不能乘本区间的更新值,只能乘根节点的更新值,根节点的跟新值用完后归0;
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; #define maxn 100005
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R struct Tree//op等于0的时候子树不需要更新,op等于1需要更新
{
    int L, R, op;//需要向下更新的值为e
    long long sum, e;//因为区间和比较大,所以使用long long 保存
    int Mid(){return (L+R)/2;}
    int Len(){return (R-L+1);}
}tree[maxn*4];
long long val[maxn]; void Down(int root)//向下更新
{
    if(tree[root].op && tree[root].L != tree[root].R)
    {
        tree[root].op = false;
        tree[root<<1].op = tree[root<<1|1].op = true;
        tree[root<<1].e += tree[root].e;
        tree[root<<1|1].e += tree[root].e;         tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
        tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e;         tree[root].e = 0;
    }
}
void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    tree[root].op = false;     if(L == R)
    {
        tree[root].sum = val[L];
        return ;
    }     Build(Lson);
    Build(Rson);     tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
void Insert(int root, int L, int R, long long e)
{
    Down(root);     tree[root].sum += (R-L+1) * e;     if(tree[root].L == L && tree[root].R == R)
    {
        tree[root].e = e, tree[root].op = true;
        return ;
    }     if(R <= tree[root].Mid())
        Insert(root<<1, L, R, e);
    else if(L > tree[root].Mid())
        Insert(root<<1|1, L, R, e);
    else
    {
        Insert(Lson, e);
        Insert(Rson, e);
    }
}
long long Query(int root, int L, int R)
{
    Down(root);     if(tree[root].L == L && tree[root].R == R)
        return tree[root].sum;
    if(R <= tree[root].Mid())
        return Query(root<<1, L, R);
    else if(L > tree[root].Mid())
        return Query(root<<1|1, L, R);
    else
        return Query(Lson) + Query(Rson);
} int main()
{
    int i, N, Q;     while(scanf("%d%d", &N, &Q) != EOF)
    {
        for(i=1; i<=N; i++)
            scanf("%I64d", &val[i]);
        Build(1, 1, N);         int a, b; char s[10];
        long long c;         while(Q--)
        {
            scanf("%s%d%d", s, &a, &b);             if(s[0] == 'C')
            {
                scanf("%I64d", &c);
                Insert(1, a, b, c);
            }
            else
            {
                long long ans = Query(1, a, b);
                printf("%I64d\n", ans);
            }
        }
    }     return 0;
} /*
5 2
1 2 3 4 5
C 1 5 5
Q 1 1
*/

C - A Simple Problem with Integers - poj 3468(区间更新)的更多相关文章

  1. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 69589   ...

  2. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  3. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  6. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  8. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  9. A Simple Problem with Integers~POJ - 3468

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  10. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

随机推荐

  1. 模板-->常系数线性齐次递推(矩阵快速幂)

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 Matrix模板 poj_2118_Firepersons,my_ac_code 简单的测试 None 代码模板 /* * ...

  2. HDU 5037 Frog(贪心)

    题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径.问怎么添加石头能让青蛙最多的次数.输出青蛙跳的最多的次数. 考 ...

  3. .net之页面生面周期

    # 事件或方法 功能 描述 1 Init 事件 页面初始化 页面生存周期中的第一个阶段是初始化.当 Init 事件发生时,在.aspx 源文件中静态声明的所有控件都已实例化并取其默认值.应该注意的是, ...

  4. 刚入门的easyui

    这两天看了下easyui的教学先说说自己的一些小小理解吧! ----在使用easyui中也遇到了一个问题 : Uncaught TypeError:cannot call method ‘offset ...

  5. css.day.05.eg

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. CSS 让标点符号不出现在行首

    word-break:break-all; 这条语句的作用是让语句到达边界的时候自动换行,但是正是这个样式让标点符号跑到了行首. 语法: word-break : normal | break-all ...

  7. Linq101-CustomSequence

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class CustomS ...

  8. HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

    1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...

  9. OC中的单例设计模式及单例的宏抽取

    // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式 // 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 #pragma Person.h文件 #i ...

  10. [逼死强迫症 - C&C++设计风格选择.1] : 命名规范

    1.命名规范 本系列的第一篇,命名风格本就是有关艺术审美,没有美与丑的绝对标准,本文难免带有主观选择倾向,但是会尽量保持客观的态度归纳几种主流的命名风格,仅供参考.制定规范是为了方便团队沟通和利于代码 ...