poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 Accepted: 23286 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 4Sample Output
4
55
9
15Hint
The sums may exceed the range of 32-bit integers.Source
POJ Monthly--2007.11.25, Yang Yi
朴素的向下更新会产生很多不必要的操作,所以lazy思想便是,用到再更新,不用就暂存到当前区间。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std; //lazy思想的线段树区间更新,区间查询,感觉不难 const int MAX = ;
ll num[MAX]; struct nodes
{
int left,right;
ll large,add;
} tree[MAX*]; void pushup(int root)//代替递归向上更新
{
tree[root].large = tree[root*].large + tree[root*+].large;
}
void pushdown(int root)//递归向下更新
{
if(tree[root].add)
{
tree[root*].add += tree[root].add;
tree[root*+].add += tree[root].add;
tree[root*].large += tree[root].add * (tree[root*].right - tree[root*].left + );
tree[root*+].large += tree[root].add * (tree[root*+].right - tree[root*+].left + );
tree[root].add = ;
}
}
void build(int root,int left,int right)//建树过程,也是初始化更新过程
{
tree[root].left = left;
tree[root].right = right;
tree[root].add = ;//不能忘记这个值的初始化为0
if(left == right)
{
tree[root].large = num[left];
return;//注意这里的递归结束条件
} int mid = (left+right)/; build(*root,left,mid);
build(*root+,mid+,right); pushup(root);//该子区间建立好后,该区间更新
} void update(int root,int left,int right,ll val)
{
if(left == tree[root].left && right == tree[root].right) // 刚好进入到所需区间,就可以更新到当前区间直接返回
{
tree[root].add += val; // 用当前节点的add变量记录区间每个元素的累加量
tree[root].large += val * (right - left + );//更新当前区间和
return;
}
pushdown(root); //否则向下更新一层,继续找合适区间
int mid = (tree[root].left+tree[root].right)/;
if(left <= mid && right > mid)
{
update(*root,left,mid,val);
update(*root+,mid+,right,val);
}
else if(left > mid)
{
update(*root+,left,right,val);
}
else
{
update(*root,left,right,val);
}
pushup(root);
} ll query(int root ,int left,int right)
{
if(left == tree[root].left && right == tree[root].right) //如果是合适区间,直接返回节点值
{
return tree[root].large;
}
pushdown(root); //否则向下更新一层,继续查找合适区间
int mid = (tree[root].left+tree[root].right)/;
ll ans = ;
if(right > mid && left <= mid)
{
ans += query(*root,left,mid);
ans += query(*root+,mid+,right);
}
else if(left > mid)
{
ans += query(*root+,left,right);
}
else
{
ans += query(*root,left,right);
}
return ans;
} int main(void)
{
int n,q,i,x,y;
ll c;
char cmd;
scanf("%d %d",&n,&q);
for(i = ; i <= n; i++)
scanf("%lld",&num[i]);
build(,,n);
for(i = ; i < q; i++)
{
getchar();
scanf("%c",&cmd);
if(cmd == 'Q')
{
scanf("%d %d",&x,&y);
printf("%lld\n",query(,x,y));
}
else
{
scanf("%d %d %lld",&x,&y,&c);
update(,x,y,c);
}
}
return ;
}
poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)的更多相关文章
- [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 , 线段树+区间更新。
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 ...
随机推荐
- Dubbo Ecosystem - 从微服务框架到微服务生态
从微服务框架到微服务生态,这是微服务发展的必然趋势,也是Dubbo社区满足开发者更高效的构建微服务体系期望的使命和担当. 近期,Apache Dubbo PPMC 望陶(社区昵称:ralf0131)做 ...
- 莫烦PyTorch学习笔记(五)——分类
import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...
- CSS自动换行、强制不换行、强制断行、超出显示省略号
转自:https://blog.csdn.net/liuyan19891230/article/details/50969393 P标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是 ...
- lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(二) LVS+Keepalived
一.安装ipvs sudo apt-get install ipvsadm 二.安装keepalived sudo apt-get install keepalived 三.创建keepalived. ...
- hdu 4563
hdu 4563 把每个命令走的距离抽象成完全背包 枚举最后一个不是整点走完的命令 #include <iostream> #include <algorithm> #incl ...
- DotNetBar2设置窗体为office风格
帮同事写个桌面工具,刚学着用DotNetBar2.后面面分享学习过程.下面是给窗体设置成office风格的方法.(1)拷贝DotNetBar2的dll文件,添加引用. (2)在需要用office风格的 ...
- PAT甲级——A1001A+BFormat
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- Java超简明入门学习笔记(三)
Java编程思想第4版学习笔记(三) 第五章 初始化与清理(构造器和垃圾回收机制) Java有和C++类似的构造函数来为新创建的对象执行初始化及完成一些特殊的操作,有的类数据成员可能会 ...
- 从0开始学习ssh之搭建环境
ssh即struts+spring+Hibernate,从头开始学习这个框架. struts环境配置,首先在apps目录下找到struts2-blank-xxx.war这个文件,这是已经发布好的war ...
- 查看MySql版本号命令
转自:https://blog.csdn.net/qq_38486203/article/details/80324014 这里介绍四中不同的方法,它们分别运行在不同的环境中,最后对每种方法的优劣以 ...