poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 58269 | Accepted: 17753 | |
| 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
Source


#include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 100010 struct Node{
long long L,R;
long long sum; //当前区间的所有数的和
long long inc; //累加量
}a[MAXN*]; void Build(long long d,long long l,long long r) //建立线段树
{ //初始化当前节点的信息
a[d].L = l;
a[d].R = r;
a[d].inc = ; if(l==r){ //找到叶子节点
scanf("%I64d",&a[d].sum);
return ;
} //建立线段树
long long mid = (l+r)>>;
Build(d<<,l,mid);
Build(d<<|,mid+,r); //更新当前节点的信息
a[d].sum = a[d<<].sum + a[d<<|].sum;
} void Updata(long long d,long long l,long long r,long long v) //更新区间[l,r]的累加量为v
{
if(a[d].L==l && a[d].R==r){ //找到终止节点
a[d].inc += v;
return ;
} long long mid = (a[d].L+a[d].R)/;
a[d].sum += a[d].inc*(a[d].R - a[d].L + ); if(mid>=r){ //左孩子找
Updata(d<<,l,r,v);
}
else if(mid<l){ //右孩子找
Updata(d<<|,l,r,v);
}
else{ //左孩子、右孩子都找
Updata(d<<,l,mid,v);
Updata(d<<|,mid+,r,v);
} a[d].sum = a[d<<].sum + a[d<<|].sum
+ a[d<<].inc*(a[d<<].R - a[d<<].L + )
+ a[d<<|].inc*(a[d<<|].R - a[d<<|].L + );
} long long Query(long long d,long long l,long long r) //查询区间[l,r]的所有数的和
{
if(a[d].L==l && a[d].R==r){ //找到终止节点
return a[d].sum + a[d].inc * (r-l+);
} long long mid = (a[d].L+a[d].R)/;
//更新每个节点的sum
a[d].sum += a[d].inc * (a[d].R - a[d].L + );
a[d<<].inc += a[d].inc;
a[d<<|].inc += a[d].inc;
a[d].inc = ; //Updata(d<<1,a[d<<1].L,a[d<<1].R,a[d].inc);
//Updata(d<<1|1,a[d<<1|1].L,a[d<<1|1].R,a[d].inc); if(mid>=r){ //左孩子找
return Query(d<<,l,r);
}
else if(mid<l){ //右孩子找
return Query(d<<|,l,r);
}
else{ //左孩子、右孩子都找
return Query(d<<,l,mid) + Query(d<<|,mid+,r);
}
a[d].sum = a[d<<].sum + a[d<<|].sum
+ a[d<<].inc*(a[d<<].R - a[d<<].L + )
+ a[d<<|].inc*(a[d<<|].R - a[d<<|].L + );
} int main()
{
long long n,q,A,B;
long long v;
scanf("%I64d%I64d",&n,&q);
Build(,,n);
while(q--){ //q次询问
char c[];
scanf("%s",&c);
switch(c[]){
case 'Q':
scanf("%I64d%I64d",&A,&B);
printf("%I64d\n",Query(,A,B)); //输出区间[A,B]所有数的和
break;
case 'C':
scanf("%I64d%I64d%I64d",&A,&B,&v);
Updata(,A,B,v);
break;
default:break;
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 3468:A Simple Problem with Integers(线段树,区间修改求和)的更多相关文章
- [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 线段树区间修改
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 线段树区间加,区间查询和
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 线段树区间更新
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(线段树区间更新)
题目地址: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<< ...
随机推荐
- linux/windows下启用和停止VMware后台服务的脚本
linux/windows下启用和停止VMware后台服务的脚本 linux/windows下启用和停止VMware后台服务的脚本 linux平台 windows平台 本文由乌合之众 lym瞎编,欢迎 ...
- apache2 + virtualenv +djangocms
命令记录: cd /var/www makedir django cd django/ virtualenv env --no-site-packages source /var/www/django ...
- 谷歌Chrome浏览器开发者工具的基础功能
上一篇我们学习了谷歌Chrome浏览器开发者工具的基础功能,下面介绍的是Chrome开发工具中最有用的面板Sources.Sources面板几乎是最常用到的Chrome功能面板,也是解决一般问题的主要 ...
- IOSGCD
http://blog.csdn.net/duxinfeng2010/article/details/8958681/ http://kyfxbl.iteye.com/blog/1997516
- Git对象模型
原文:http://gitbook.liuhui998.com/1_2.html 一.SHA 在git中,所有用来表示项目历史信息的文件,是通过一个40个字符的(40-digit)"对象名& ...
- oracle:安装笔记
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
- 解读Unity中的CG编写Shader系列二
转自 http://www.itnose.net/detail/6095974.html 上一篇文章的例子中我们可以看到顶点着色器的输出参数可以说是直接作为了片段着色器的形参传递过来,那么不由得一个问 ...
- 13.SpringMVC和Spring集成(一) && 14.SpringMVC和Spring集成(二)
1.概念 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,Spring致力于J2EE应用的各层的解决方案,Spring是企业应用开发的“一站式”选择,并贯 ...
- 前端easyui的简化调用
easyui近期一直都比较流行,虽然它在效果上被extjs爆了,它的使用难度低,在IE6下表现不错,的确受到了广泛企业程序员的好评. 但是他的API说明还是比较简陋的,刚上手可能还需要摸索一下,为什么 ...