A Simple Problem with Integers

Time Limit: 5000MS
Memory Limit: 131072K

Total Submissions: 53169
Accepted: 15897

Case Time Limit: 2000MS

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 <cstdio>
using namespace std;
typedef long long ll; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=111111;
ll add[maxn<<2];
ll sum[maxn<<2]; void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
} void PushDown(int rt,int m)
{
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=(m-(m>>1))*add[rt];
sum[rt<<1|1]+=(m>>1)*add[rt];
add[rt]=0;
}
} void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);//
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
sum[rt]+=(r-l+1)*c;
add[rt]+=c;
return ;
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,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+1);
int m=(l+r)>>1;
ll res=0;
if(L<=m)
res+=query(L,R,lson);
if(R>m)
res+=query(L,R,rson);
return res;
} int main()
{
int N,Q;
scanf("%d%d",&N,&Q);
build(1,N,1);
while(Q--)
{
char s[6];
int a,b;
scanf("%s%d%d",s,&a,&b);
if(s[0]=='Q')
printf("%lld\n",query(a,b,1,N,1));//
else
{
int c;
scanf("%d",&c);
update(a,b,c,1,N,1);
}
}
return 0;
}

POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  2. poj3468A Simple Problem with Integers(线段树,在段更新时要注意)

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

  3. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  4. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

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

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

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  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 w ...

  8. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

随机推荐

  1. java 接口学习

    你应该知道接口是一种契约,它与实现方式无关 但是类,即使是抽象类,你都能自定义成员变量,而成员变量往往就与实现方式有关. 这一点的实际意义不大. 但是有一点,类会暴露太多不必要,甚至不能暴露的东西,你 ...

  2. Python入门笔记(26):Python执行环境

    一.python特定的执行环境 在当前脚本继续进行 创建和管理子进程 执行外部命令或程序 执行需要输入的命令 通过网络来调用命令 执行命令来创建需要处理的输出 动态生成Python语句 导入Pytho ...

  3. 011_URL和Ajax辅助器方法

    创建基本的链接和URL 在我们介绍链接或URL之前先做一些准备,我们这部分要介绍的知识将要使用的项目就是之前建立的HelperMethods项目,现在需要先为其添加一个People控制器,并在其中定义 ...

  4. AC自动机---病毒侵袭持续中

    HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...

  5. C语言范例学习02

    第二章 指针 算是重点吧,这也是C语言的特色啊,直接访问物理存储. 重点: 指针就是一个存放它指向变量地址的变量,好绕口.   区分*在定义是与引用是的作用.   区分*.&的不同.   指针 ...

  6. nginx跨域处理

    http://www.nginx.cn/nginx-download nginx.conf配置 if ($request_method = ‘OPTIONS’) {         add_heade ...

  7. 线上mysql内存持续增长直至内存溢出被killed分析(已解决)

    来新公司前,领导就说了,线上生产环境Mysql库经常会发生日间内存爆掉被killed的情况,结果来到这第一天,第一件事就是要根据线上服务器配置优化配置,同时必须找出现在mysql内存持续增加爆掉的原因 ...

  8. 我为什么要做富文本编辑器【wangEditor5个月总结】

    请访问wangEditor官网:www.wangEditor.com ----------------------------------------------------------------- ...

  9. R语言学习笔记:分析学生的考试成绩

    孩子上初中时拿到过全年级一次考试所有科目的考试成绩表,正好可以用于R语言的统计分析学习.为了不泄漏孩子的姓名,就用学号代替了,感兴趣可以下载测试数据进行练习. num class chn math e ...

  10. 桥牌笔记:Show up Squeeze显露挤牌法

    南主打4S,注意一个叫牌过程,西家叫过加倍,东家应叫过2D. 西连打红桃K.A,然后再打红桃J让东家将吃.东家上手后,回小方块.此时庄家已经失了3墩了,如何完成这个4S? 庄家必须拿到所有剩下的牌墩. ...