题意:有一个比较长的区间可能是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. 使用 trait 时报PHP Parse error: syntax error, unexpected 'use' (T_USE) 这个错误

    找一大圈原因, 最后终于找到了, 不是PHP版本的原因[], 是自己把use 写到了类里的方法里了. 这个东东,  不能脱离类单独使用, 否则的话, 会被认为是命名空间了. 测试例子如下 // Tra ...

  2. python-增删改查

    ###增删改查 names = ["zhangding","wangxu","wudong","cheng"] #增 n ...

  3. C#解leetcode 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. 使用MiniProfiler调试Asp.net Mvc性能

    使用nuget添加MiniProfiler.EF组件 在Global文件中配置MiniProfiler protected void Application_Start() { AreaRegistr ...

  5. ManagementException:WMI异常处理介绍

    .NET调用WMI后无论是同步调用还是异步调用,都会产生返回一个int类型的执行结果.如果成功,则返回0.如果不是0,则有对应错误码表示发生了什么错误. 根据咱们这个系列的博文,我总结了关于进程,服务 ...

  6. linux书籍推荐(转)

    ref: http://www.cnblogs.com/jiangjh/archive/2011/06/27/2091164.html#commentform 入门篇 <LINUX权威指南> ...

  7. iOS9 中的一些适配问题

    1.URL scheme白名单:在info文件中加入LSApplicationQueriesSchemes(Array),添加需要的scheme,如微信:weixin.wechat 支付宝:alipa ...

  8. Delphi 动态创建组件,单个创建、单个销毁

    效果图如下: 实现部分代码如下: var rec: Integer = 0; //记录增行按钮点击次数 implementation {$R *.dfm} //动态释放单个组件内存,即销毁组件 pro ...

  9. 【USACO 1.3.1】混合牛奶

    [题目描述] 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助梅丽乳业找到最优的牛奶采购方案. 梅丽乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的. ...

  10. window.showModalDialog以及window.open用法简介

    .可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象.例如:------------------------------parent.htm<script& ...