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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
using namespace std;
const int maxn=;
ll sum[maxn<<];
ll cov[maxn<<];
int n,m,t;
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int m)
{
if(cov[rt])
{
cov[rt<<]+=cov[rt];
cov[rt<<|]+=cov[rt];
sum[rt<<]+=cov[rt]*(m-(m>>)); //注意括号
sum[rt<<|]+=cov[rt]*(m>>);
cov[rt]=;
}
}
void build(int l,int r,int rt)
{
cov[rt]=;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int add,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
cov[rt]+=add;
sum[rt]+=(ll)add*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(add,L,R,lson);
if(m<R) update(add,L,R,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if (L<=l&&r<=R)
{
return sum[rt];
}
pushdown(rt,r-l+);
int m=(l+r)>>;
ll ret=;
if (L<=m) ret+=query(L,R,lson);
if (m<R) ret+=query(L,R,rson);
return ret;
}
int main()
{
int i,j,k,q;
//freopen("1.in","r",stdin);
scanf("%d%d",&n,&q);
build(,n,);
while(q--)
{
char s[];
scanf("%s",s);
if(s[]=='Q')
{
int L,R;
scanf("%d%d",&L,&R);
printf("%lld\n",query(L,R,,n,));
}
else
{
int L,R,c;
scanf("%d%d%d",&L,&R,&c);
update(c,L,R,,n,);
}
}
return ;
}

poj 3468 成段增减的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. poj 3468 线段树 成段增减 区间求和

    题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...

  3. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  4. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  5. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  6. 线段树之成段更新( 需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候)

    HDU  1698 链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直 ...

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

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

  8. hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***

    不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...

  9. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

随机推荐

  1. linux 的iptables防火墙

    .a文件就是*.o文件的集合, 是object文件的归档文件, 所以, 用nm -A  ???.a看到的 symbolic符合名称都是 相应的,  包含的  .o文件.... linux 2.4内核中 ...

  2. CSS3 transforms 3D翻开

    R T L B   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  3. 初步揭秘node.js中的事件

    当你学习node.js的时候,Events是一个非常重要的需要理解的事情.非常多的Node对象触发事件,你能在文档API中找到很多例子.但是关于如何写自己的事件和监听,你可能还不太清楚.如果你不了解, ...

  4. Base64封装类

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  5. 字符串模拟赛T2

    // source code from laekov for c0x17 #define PRID "fkqh" #include <cstdio> #include ...

  6. sql批量获取wordpress所有留言者的邮件地址

    如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...

  7. Protocol Buffers介绍

    基本概念 Protocol Buffers(以下简称PB)是一种独立于语言.独立于开发平台.可扩展的序列化数据结构框架,它常常被用在通信.数据序列化保存等方面. PB是一种敏捷.高效.自动化的用于对数 ...

  8. 生成唯一编号(序列号)--sql存储过程

    CREATE procedure [dbo].[P_Sys_GetSerialNo] --取业务序列号 @SeqType int, --序列号类别,4位数,如:10+2+1 即1021 , --要取的 ...

  9. 在pc游览器端模拟移动端幻灯片

    用简单的思路写了下pc端模拟web端的图片滑动效果... 效果卡,bug多,完毕,继续学习c3方法写这个,iscroll就是可以模拟这种效果,还在学习中,难点<触点判断> 代码一份 < ...

  10. ROS2.9.27架设网吧软路由实战篇之端口映射与回流

    转载:http://blog.csdn.net/zm2714/article/details/7924280 上一篇:ROS2.9.27架设网吧软路由实战篇之连通网络,主要讲述了网吧架设软路由ROS2 ...