POJ 3468 A Simple Problem with Integers(线段树区间求和)
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
区间求和:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=100000;
int num[maxn];
LL sum[maxn<<2],add[maxn<<2];
int N,Q;
void pushup(int rs)
{
sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int l)
{
if(add[rs])
{
add[rs<<1]+=add[rs];
add[rs<<1|1]+=add[rs];
sum[rs<<1]+=add[rs]*(l-(l>>1));
sum[rs<<1|1]+=add[rs]*(l>>1);
add[rs]=0;
}
}
void build(int rs,int l,int r)
{
if(l==r)
{
scanf("%I64d",&sum[rs]);
return ;
}
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
pushup(rs);
}
void update(int c,int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
{
add[rs]+=c;
sum[rs]+=(LL)c*(r-l+1);
return ;
}
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
if(x<=mid) update(c,x,y,l,mid,rs<<1);
if(y>mid) update(c,x,y,mid+1,r,rs<<1|1);
pushup(rs);
}
LL query(int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
return sum[rs];
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
LL ans=0;
if(x<=mid) ans+=query(x,y,l,mid,rs<<1);
if(y>mid) ans+=query(x,y,mid+1,r,rs<<1|1);
return ans;
}
int main()
{
int x,y,z;
std::ios::sync_with_stdio(false);
while(~scanf("%d%d",&N,&Q))
{
CLEAR(sum,0);
CLEAR(add,0);
build(1,1,N);
char str[2];
while(Q--)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(z,x,y,1,N,1);
}
else
{
scanf("%d%d",&x,&y);
printf("%I64d\n",query(x,y,1,N,1));
}
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers(线段树区间求和)的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) 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 ...
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- Spring(二)——IoC
IoC(Inversion of Control)称之为控制反转,指的是在Spring框架的配置文件中声明对象,由框架负责创建对象,这叫做控制反转.实现方式有两种:DI(Dependency Inje ...
- static静态属性和静态方法的原理与调用技巧
这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下 本文实例讲述了php中static静态属 ...
- html css js 框架
html css js 框架 Bootstrap http://www.bootcss.com/ http://www.cnblogs.com/aehyok/p/3404867.html ...
- 谈谈javascript的函数表达式及其应用
我们都知道定义函数的方式有两种,一种是函数声明,另外一种就是函数表达式. 函数声明 语法为:function关键字后跟函数名.例如: function functionName(arg0) { //函 ...
- .NET 下成熟开源的BPM产品四款推荐
.net下的BPM产品相比JAVA的确实不多,这里主要提4款. 1.博客园.github.codeplex上的开源的流程组件AppInOne BPM,目前已有不少的企业开始使用. 优点:产品框架较全面 ...
- jdbc——CallableStatement,cst调用存储过程
package CST对象调用存储过程; import java.sql.CallableStatement; import java.sql.Types; import org.junit.Test ...
- NSNumber 和 NSValue 的部分使用
1.NSNumber 在Objective-c中有int,float,char等基本数据类型,但这些基本数据类型并不是对象,而数组,字典,字符串等容器中存放的都是对象类型,因此我们需要用到NSNumb ...
- 用canvas绘制一个时钟
实现一个时钟的绘制和时间的显示 一,首先是页面的搭建html部分以及一点点的css代码,因为css这块用的比较少,所以就没有单独出来: <!DOCTYPE html> <html l ...
- avalon前端js直接通过ajax请求传一个对象到后台
代码如下: //企业开票信息 vm.invoiceInfo = { companyId : "", //企 ...
- Java数组的复制
初学Java的时候,需要复制数组的时候,一下子就想到使用赋值语句“=”,例如:array1 = array2:但后来慢慢发现,这个语句并不能将array2的内容复制给array1,而是将array2的 ...