[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 ...
随机推荐
- “express不是内部或外部命令”解决办法
由于安装的Express是最新版本4.13.1,4.x版本就需要安装express-generator.命令如下:npm install -g express-generator ps: 1.卸载: ...
- 理想与现实——观电影《Dead Poets Society》有感
我们每一个人都注定要死去,看看那些旧照片,照片里的年轻人现在都在哪里呢?也许有的人曾经充满活力,曾经信誓旦旦地要去改变这个世界,但如今却变得只知道顺从,如果你去问他们,他们会说:大概这就是现实吧. 现 ...
- hadoop集群的故障概率估算
hadoop集群的机器数业界(国内)最大的在5000左右,是什么限制了集群的规模呢?有好几个原因. 1. namenode的内存大小限制 2. 机器故障概率随着机器数目增大而增大,通常一份数据存储在h ...
- Effective Java Item3:Enforce the singleton property with a private constructor or an enum type
Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...
- android特效
http://houxiyang.com/archives/89/ http://blog.csdn.net/hjj0212/article/details/8535817 http://www.li ...
- 如何解决Bluetooth系统设计的棘手问题
我们若想设计一套完善的蓝牙 (Bluetooth) 系统,就必须充分掌握其中的技术知识,例如协议堆栈.射频设计及系统集成等方面的专门知识.LMX9820 芯片的面世令蓝牙系统的设计工作变得更为容易.以 ...
- 多线程操作UI界面的示例 - 更新进度条
http://blog.csdn.net/liang19890820/article/details/52186626
- 如何让 Qt 的程序使用 Sleep(主线程没有Sleep函数,但线程可用自带的保护函数Sleep)
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- Global build settings
[ ] Select all packages by default *** General build options *** [ ] Show packages that require gr ...
- VC++如何在程序中用代码注册和卸载ocx控件(代码)
方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx //注册netshare.ocx控件regsvr ...