题目链接: 传送门

A Simple Problem with Integers

Time Limit: 5000MS     Memory Limit: 131072K

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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l ,m , rt << 1
#define rson m + 1 , r ,rt << 1 | 1
typedef __int64 LL;
const int maxn = 100005;
LL sum[maxn<<2],lazy[maxn<<2];

void PushUp(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void PushDown(int rt,int m)
{
    if (lazy[rt])
    {
        lazy[rt<<1] += lazy[rt];
        lazy[rt<<1|1] += lazy[rt];
        sum[rt<<1] += (m - (m>>1)) * lazy[rt];
        sum[rt<<1|1] += (m>>1) * lazy[rt];
        lazy[rt] = 0;
    }
}

void build(int l,int r,int rt)
{
    if (l == r)
    {
        scanf("%I64d",&sum[rt]);
        lazy[rt] = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
} 

void upd(int L,int R,int c,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        sum[rt] += (LL)(r - l + 1) * c;
        lazy[rt] += c;
        return;
    }
    PushDown(rt,r - l + 1);
    int m = (l + r) >> 1;
    if (L <= m) upd(L,R,c,lson);
    if (R > m)  upd(L,R,c,rson);
    PushUp(rt);
}

LL qry(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    PushDown(rt,r - l + 1);
    int m = (l + r) >> 1;
    LL ret = 0;
    if (L <= m) ret += qry(L,R,lson);
    if (R > m)  ret += qry(L,R,rson);
    return ret;
}

int main()
{
    int N,Q,a,b,c;
    char opt;
    while (~scanf("%d%d",&N,&Q))
    {
        build(1,N,1);
        while (Q--)
        {
            getchar();
            scanf("%c",&opt);
            if (opt == 'C')
            {
                scanf("%d%d%d",&a,&b,&c);
                upd(a,b,c,1,N,1);
            }
            else
            {
                scanf("%d%d",&a,&b);
                printf("%I64d\n",qry(a,b,1,N,1));
            }
        }
    }
    return 0;
}

POJ 3468 A Simple Problem with Integers(线段树/区间更新)的更多相关文章

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

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

  2. (简单) 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 ...

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

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

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

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

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

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

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

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

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

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  8. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  10. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. lecture1-NN的简介

    这是DL的发明人Hinton在多伦多大学的2013年冬季教授de课程,并将视频分享到coursera网站上.其中不但有视频,也有课件,但是Hinton主页上还有他上课的课后问题,Hinton告诉学生这 ...

  2. HTTP真的很简单

    原文:HTTP Made Really Easy因为我本身网络基础就很差,所以看到这篇文章一方面是学习网络知识,另一方面为了锻炼我蹩脚的英语水平,文中如有错误,欢迎浏览指正! 前言 在看这篇文章的时候 ...

  3. JVM内存管理------GC算法精解(五分钟让你彻底明白标记/清除算法)

    相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底理解标记/清除算法,不过倘若各位猿友不能在五分钟内 ...

  4. B树和B+树

    当数据量大时,我们如果用二叉树来存储的会导致树的高度太高,从而造成磁盘IO过于频繁,进而导致查询效率下降.因此采用B树来解决大数据存储的问题,很多数据库中都是采用B树或者B+树来进行存储的.其目的就是 ...

  5. PS技能大全

    1.设置固定的图片大小 参考:http://jingyan.baidu.com/album/642c9d3418bec4644a46f72a.html?picindex=1

  6. HIbernate的脏数据检测和延缓加载

    脏数据监测: 在一个事务中,加载的数据,除了返回给用户之外,会复制一份在session中,在事务提交时,会用session中的备份和用户的数据进行比对,如果用户的数据状态改变, 则用户的数据即为:脏数 ...

  7. 【BZOJ 1038】【ZJOI 2008】瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小 ...

  8. ES6新特性:Javascript中的Reflect对象

    Reflect介绍: Reflect这个对象在我的node(v4.4.3)中还没有实现, babel(6.7.7)也没有实现 ,新版本的chrome是支持的, ff比较早就支持Proxy和Reflec ...

  9. poj2774 后缀数组2个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 26601   Accepted: 10 ...

  10. SQL server 那些数据类型不能作为索引

    下数据类型不能包括在列存储索引中: ·binary和varbinary ·ntext.text和 image ·varchar(max)和nvarchar(max) ·uniqueidentifier ...