[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
Source
线段树功能:update:成段增减 query:区间求和
#include<cstdio>
#include<algorithm> #define clr(x,y) memset(x,y,sizeof(x))
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn=1e5+;
using namespace std; LL sum[maxn<<],Lazy[maxn<<]; void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void PushDown(int rt,int m)
{
if(Lazy[rt]) {
Lazy[rt<<]+=Lazy[rt];
Lazy[rt<<|]+=Lazy[rt];
sum[rt<<]+=(m-(m>>))*Lazy[rt];
sum[rt<<|]+=(m>>)*Lazy[rt];
Lazy[rt]=;
}
} void build(int l,int r,int rt)
{
int m;
Lazy[rt]=;
if(l==r) {
scanf("%lld",&sum[rt]);
return;
} m=(l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} void Updata(int L,int R,int c,int l,int r,int rt)
{
int m;
if(L<=l && r<=R) {
Lazy[rt]+=c;
sum[rt]+=(LL)c*(r-l+);
return;
} PushDown(rt,r-l+);
m=(l+r)>>;
if(L<=m) Updata(L,R,c,lson);
if(R>m) Updata(L,R,c,rson);
PushUp(rt); } LL query(int L,int R,int l,int r,int rt)
{
int m;
LL ret=;
if(L<=l && r<=R) {
return sum[rt];
} PushDown(rt,r-l+);
m=(l+r)>>;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson); return ret;
} int main()
{
int Q,n,a,b,c;
char st[]; scanf("%d%d",&n,&Q);
build(,n,); while(Q--) {
scanf("%s",st);
if(st[]=='C') {
scanf("%d%d%d",&a,&b,&c);
Updata(a,b,c,,n,);
} else {
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,,n,));
} } return ;
}
[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]的更多相关文章
- 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 , 线段树+区间更新。
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 线段树区间更新
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(线段树区间更新)
题目地址: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<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...
- 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 ...
随机推荐
- 局域网架个YUM源-HTTP的
在安装CDH时,这是个绕不过去的坎. 参考URL: http://www.21ops.com/linux/26465.html 奇怪的是,我并没执行creatrepo这个,直接将光盘MOUNT来用的. ...
- POJ 1225 Substrings
http://poj.org/problem?id=1226 题意:给定n个串.求一个最长的串,使得这个串或者其反串在每个串中都出现过? 思路:先在大串里面加入正反串,然后二分,判定即可. #incl ...
- bzoj 1193
http://www.lydsy.com/JudgeOnline/problem.php?id=1193 大范围贪心,小范围宽搜. 膜拜大神 http://blog.csdn.net/u0129155 ...
- joomla安装插件报错:上传文件到服务器发生了一个错误。 过小的PHP文件上传尺寸
在安装joomla的AKeeba插件的时候报错如下:上传文件到服务器发生了一个错误. 过小的PHP文件上传尺寸.解决方法是修改php.ini文件,打开文件后搜索upload_max_filesize! ...
- Manacher算法----最长回文子串
题目描述 给定一个字符串,求它的最长回文子串的长度. 分析与解法 最容易想到的办法是枚举所有的子串,分别判断其是否为回文.这个思路初看起来是正确的,但却做了很多无用功,如果一个长的子串包含另一个短一些 ...
- Android Studio:Error:Execution failed for task ':app:mergeDebugResources'. > Some file crunching failed, see logs for details
Gradle 编译错误: 14:39:58 Executing tasks: [clean, :app:generateDebugSources, :app:mockableAndroidJar, : ...
- 关于js对象引用的小例子
看完下面的代码,相信对js对象引用又有了新的认识,直接上代码: // split()把字符串分割成字符串数组 // reverse() 会改变数组本身,**并返回原数组的引用**.!!!! var a ...
- PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236
一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...
- AIX下RAC搭建 Oracle10G(一)检測系统环境
AIX下RAC搭建系列 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX203 AIX204 交换机 SAN光纤交换机 存储 S ...
- Android 自定义UI--电池
首先看一下效果图, 下面看代码: /** * */ package com.example.batterydemo; import android.content.Context; import an ...