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. 【循序渐进学Python】6.Python中的函数

    1. 创建函数 一个函数代表一个行为并且返回一个结果(包括None),在Python中使用def关键字来定义一个函数,如下: def hello(name): print 'hello,' + nam ...

  2. 发布在IIS的网站,可以用本机IP登录访问,用localhost不可登录访问

    之前在IIS发布一个测试的网址,但是用本机IP可以访问,用localhost不可访问

  3. 内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统

    linux内核最小系统,使用内核版本:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.8.1.tar.bz2 1,FL2440板子的基本硬件: ...

  4. HttpClient总结一之基本使用

    最近工作中是做了一个handoop的hdfs系统的文件浏览器的功能,是利用webhdfs提供的rest api来访问hdfs来与hdfs进行交互的,其中大量使用HttpClient,之前一直很忙,没什 ...

  5. mysql 数据类型,字符集

    数据类型 1,数值类型2,字符串类型3,日期和时间4,ENUM和SET5,几何数据类型   数据类型选项 unsigned   无负值 zerofill        数值显示有影响,会前置0来填充不 ...

  6. 数码管问题(c++实现)

    描述:液晶数码管用七笔阿拉数字表示的十个数字,把横和竖的一 个短划都称为一笔,即7有3笔,8有7笔等.对于十个数字一种排列,要做到 两相邻数字都可以由另一个数字加上几笔或减去几笔组成,但不能又加又减. ...

  7. ArcGIS的许可文件问题

    ArcGIS我是从9.3版本开始使用的,当时破解非常麻烦,容易出现各种问题,当时生成的许可文件是一个单独的exe:后来10版本甚至10.1都出来,我还是使用的那个exe来生成许可文件,正常使用没有问题 ...

  8. Mybatis学习记录(四)----resultMap的使用

    resultMap使用方法 如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系. 1.定义resultMap 2.使用resultMap ...

  9. 高精度练习(hdoj1042)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  10. C安全编码--数组

    建议和规则 建议: 理解数组的工作方式 获取数组的长度时不要对指针应用sizeof操作符 显示地指定数组的边界,即使它已经由初始化值列表隐式地指定 规则: 保证数组索引位于合法的范围内 在所有源文件中 ...