POJ   3468

Description

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

Input

第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

接下来Q行询问,格式如题目描述。

Output

对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

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 思路:线段树区间更新。 本题代码是很好的线段树区间更新模板:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
const int maxn=;
struct Node
{
LL sum,val;
} node[*maxn]; void pushup(int i)
{
node[i].sum=node[i<<].sum+node[i<<|].sum;
} void pushdown(int i,int m)
{
if(node[i].val)
{
node[i<<].val+=node[i].val;
node[i<<|].val+=node[i].val;
node[i<<].sum+=(LL)node[i].val*(m-(m>>));
node[i<<|].sum+=(LL)node[i].val*(m>>);
node[i].val=;
}
} void build(int l,int r,int i)
{
node[i].val=;
if(l==r)
{
scanf("%I64d",&node[i].sum);
return ;
}
int mid=(l+r)/;
build(l,mid,i<<);
build(mid+,r,i<<|);
pushup(i);
} LL query(int L,int R,int l,int r,int i)
{
if(L<=l&&r<=R)
{
return node[i].sum;
}
int mid=(l+r)>>;
pushdown(i,r-l+);
LL ans=;
if(L<=mid)ans+=query(L,R,l,mid,i<<);
if(mid<R)ans+=query(L,R,mid+,r,i<<|);
pushup(i);
return ans;
} void update(int L,int R,int add,int l,int r,int i)
{
if(L<=l&&r<=R)
{
node[i].sum+=(LL)add*(r-l+);
node[i].val+=add;
return ;
}
pushdown(i,r-l+);
int mid=(l+r)>>;
if(L<=mid)update(L,R,add,l,mid,i<<);
if(mid<R)update(L,R,add,mid+,r,i<<|);
pushup(i);
} int main()
{
int n,q,a,b;
LL c;
while(scanf("%d%d",&n,&q)!=EOF)
{
build(,n,);
char s[];
while(q--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%I64d",&a,&b,&c);
update(a,b,c,,n,);
}
else if(s[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%I64d\n",query(a,b,,n,));
}
}
}
return ;
}

线段树的区间更新---A Simple Problem with Integers的更多相关文章

  1. 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers

    线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...

  2. zoj3686(线段树的区间更新)

    对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a ...

  3. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  7. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  8. Color the ball (线段树的区间更新问题)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...

  9. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

随机推荐

  1. OAuth介绍

    1.认识OAUTH OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息 (如用户名与密码),即第三方无需使用 ...

  2. 哈夫曼算法(haffman)实现压缩和解压缩-C语言实现

    /* * ===================================================================================== * * Filen ...

  3. PowerDesigner 16.5对SQL Server 2012 生成数据库时"不支持扩展属性"问题

    团队合作设计一套系统数据模型,创建了PDM后,Table.View.Store Procedure等都创建好了,且创建了多个Schema方便管理这些数据库对象,但Table.view.Column等对 ...

  4. url rewrite

    http://www.microsoft.com/taiwan/technet/iis/expand/URLRewrite.aspx http://www.iis.net/learn/extensio ...

  5. Oracle数据库入门——pctfree和pctused详解

    一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...

  6. Unity不同平台生成中预处理的注意点

    http://blog.csdn.net/pandawuwyj/article/details/7959335 Unity3D的项目,这周吃亏在宏上了.大背景是项目需要在Unity中用Hudson自动 ...

  7. oracle vm virtualbox 如何让虚拟机可以上网

    刚安装了虚拟机,系统linux2.6. 可是想安装一些软件,发现没法联网.郁闷~  还要手动设置网络,可是也不是小白就可以干的事情,还要弄清楚原理才行. http://reverland.bitbuc ...

  8. VS2010 发布web项目 问题

    载:http://www.cnblogs.com/shaocm/archive/2012/08/10/2632116.html

  9. (oracle/mysql)启停,导入导出,DB字符集

    ==================================================================================== DB启动停止 ======== ...

  10. VS快捷键的简单总结

    一.一般的快捷键 Shift+Alt+Enter: 切换全屏编辑 Ctrl+B,T / Ctrl+K,K: 切换书签开关Ctrl+B,N / Ctrl+K,N: 移动到下一书签Ctrl+B,P: 移动 ...