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<stdio.h>
using namespace std; const int MAXN = ;
int a[MAXN];
long long int tree[MAXN*];
long long int lazy[MAXN*];
int N,Q;
string s;
int x , y, z;
void push_down(int l ,int r ,int rt)
{
int m = (l+r)/; if(lazy[rt])
{
tree[rt*] += lazy[rt]*(m-l+);
tree[rt*+] += lazy[rt]*(r-m);
lazy[rt*] += lazy[rt];
lazy[rt*+] += lazy[rt];
lazy[rt] = ;
}
}
void bulid_tree(int l ,int r ,int rt)
{
if(l==r)
{
tree[rt] = a[l];
return ;
}
int m = (l+r)/; bulid_tree(l,m,rt*);
bulid_tree(m+,r,rt*+);
tree[rt] = tree[rt*]+tree[rt*+];
} long long int Query(int x ,int y ,int l ,int r ,int rt)
{
long long sum = ;
if(x<=l&&r<=y)
{
return tree[rt];
}
int m = (l+r)/;
push_down(l,r,rt);
if(x<=m)
{
sum += Query(x,y,l,m,rt*);
}
if(m<y)
{
sum += Query(x,y,m+,r,rt*+);
}
return sum;
}
void Update(int x ,int y ,int k ,int l ,int r ,int rt)
{
if(x<=l&&y>=r)
{
tree[rt] += k*(r-l+);
lazy[rt] += k;
return ;
}
push_down(l,r,rt);
int m = (l+r)/;
if(x<=m)
{
Update(x,y,k,l,m,rt*);
}
if(y>m)
{
Update(x,y,k,m+,r,rt*+);
}
tree[rt] = tree[rt*]+tree[rt*+];
}
int main()
{
scanf("%d%d",&N,&Q);
for(int i = ; i <= N;i++)
{
scanf("%d",&a[i]);
}
bulid_tree(,N,);
while(Q--)
{
cin>>s;
if(s[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",Query(x,y,,N,)); }
else
if(s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
Update(x,y,z,,N,);
}
}
return ;
}

POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)的更多相关文章

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

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

  2. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Python numpy函数:reshape()

    reshape()函数用于改变数组对象的形状: import numpy as np a = np.array([1,2,3,4,5,6,7,8]) #转换成2D数组 b = a.reshape((2 ...

  2. 机器学习:集成学习(OOB 和 关于 Bagging 的更多讨论)

    一.oob(Out - of - Bag) 定义:放回取样导致一部分样本很有可能没有取到,这部分样本平均大约有 37% ,把这部分没有取到的样本称为 oob 数据集: 根据这种情况,不对数据集进行 t ...

  3. ps命令,性能监控,grep命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  4. Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明

    Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...

  5. Celery-4.1 用户指南: Optimizing (优化)

    简介 默认的配置做了很多折中考虑.它不是针对某个情况优化的,但是大多数情况下都工作的非常好. 基于一个特殊的使用场景,有很多优化可以做. 优化可以应用到运行环境的不同属性,可以是任务执行的时间,使用的 ...

  6. appium_python 实现手势密码

    直接上代码吧: from appium.webdriver.common.touch_action import TouchAction from driver import AppiumTest # ...

  7. sql server小知识

    SELECT TOP 10000 * FROM [LogFeedback].[dbo].[ahwater_perf_monitor] order by timestramp desc   降序 asc ...

  8. Java中Exception的种类

    通常来讲,Java中的异常会被分为三种: Error: 这种异常被设计成不被捕获,因为这种异常产生于JVM自身. Runtime Exception: 运行时异常往往与环境有关,编译时无法检查,并且可 ...

  9. mahout in Action研读(1)-给用户推荐图书

    1.mahout in Action2.2第一个例子   Running a first recommender engine   数据: 第一个数字是用户ID 第二个是书的ID,第三个是用户对书的评 ...

  10. JAVA基础知识总结10(包类)

    包:定义包用package关键字. 1:对类文件进行分类管理. 2:给类文件提供多层名称空间. 如果生成的包不在当前目录下,需要最好执行classpath,将包所在父目录定义到classpath变量中 ...