[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 ...
随机推荐
- (转)Oracle Data Guard学习
一.Data Guard提供如下三种数据保护模式: 1)最高保护模式(Maximum Protection) 这里的”最高保护“是指最大限度的保护数据不丢失,也就是至少有一个standby和prima ...
- Why Memory Barrier?
引言:xchg做了什么? 首先,xchg eax, ecx并不会比mov edx, eax + mov eax, ecx + mov ecx, edx这三条指令加一起快,原因是xchg有副作用. Mi ...
- VMware虚拟机相关文件问题
.vmx VM的配置文件 .vmdk VM的虚拟硬盘 .vmsd VM快照和相关联的vmdk的字典文件 .vswap 虚拟交换文件 .nvram 虚拟机的BIOS信息.VM会生成VMX, VMDK, ...
- sqlplus与sqlplusw (转)
一.sqlplus与sqlplusw两者统称SQLPlus,是Oracle的一个命令行执行工具. 二.SQLPlus的有两种运行方式: 1.在命令行窗口运行.sqlplus 2.在窗口中运行.sq ...
- Oracle 11gR2 RAC Votedisk and OCR Diskgroup Recovery
check votedisk and OCR [root@vzwc1 ~]# ocrcheck Status of Oracle Cluster Registry is as follows : Ve ...
- phpStorm 各种快捷键
1. ctrl + shift + n: 打开工程中的文件(类似于eclipse中的ctrl+shift+R),目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl ...
- JAVA并发实现二(线程中止)
package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...
- poj 3666 Making the Grade(dp)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- java 实例化是调用了子类重写方法
java 实例化时调用了抽象方法或者class里面某个方法,如果子类有重写改方法,实际运行的是子类重写方法 package auto.test; //抽象父类 public abstract clas ...
- HttpServletRequest对象方法的用法(转)
原文地址:http://blog.csdn.net/xh16319/article/details/8450715 原文作者:ITSTAR 文章太赞,忍不住就想转..... 1. 获得客户机信息 ...