POJ 3468 A Simple Problem with Integers(详细题解) 线段树
这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的。
此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的
具体思路
在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新Sum(加上本次增量),再将增量往下传。
这样更新的复杂度就是O(log(n))在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到Sum上后将Inc清0,接下来再往下查询。
Inc往下带的过程也是区间分解的过程,复杂度也是O(log(n))
明白思路就好写了。
下面是代码
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define min(a,b) (a<b?a:b)
#define maxn 100010 #define lson root<<1///左儿子 相当于 root*2
#define rson root<<1|1///右儿子
typedef __int64 LL;
int n, m, val[maxn]; struct node
{
int L, R;
LL Sum, Inc;///Sum保存区间的和, Inc保存这个区间内所有的数字都加上Inc
int Mid()
{
return (L + R)/;
}
} Tree[maxn*]; void Bulid(int root,int L,int R)
{/**递归建树,并且将值进行更新*/
Tree[root].L = L;
Tree[root].R = R;
Tree[root].Sum = Tree[root].Inc = ;
if(L == R)
{
Tree[root].Sum = val[L];
return ;
}
Bulid(lson, L, Tree[root].Mid() );
Bulid(rson, Tree[root].Mid()+, R);
Tree[root].Sum = Tree[lson].Sum + Tree[rson].Sum;
} void Add(int root,int L,int R,int v)
{/**更新区间内所有的值*/
if(L == Tree[root].L && R == Tree[root].R)
{/**如果上述条件满足了,说明整个区间都要加上一个 v,这个时候我们只需要更新 Inc就可以了*/
Tree[root].Inc += v;
return ;
}
/**如果这个区间并不能完全更新完,则将这个值加到Sum上*/
Tree[root].Sum += (R - L + )*v; /**继续向下递增*/
if( R <= Tree[root].Mid() )
Add(lson, L, R, v);
else if(L > Tree[root].Mid() )
Add(rson, L, R, v);
else
{
Add(lson, L, Tree[root].Mid(), v);
Add(rson, Tree[root].Mid()+, R, v);
}
} LL QuerySum(int root,int L,int R)
{
LL Sum = ;
/**查询操作**/
if(Tree[root].L == L && Tree[root].R == R)/**如果区间完全吻合了,可以直接算出来*/
return Tree[root].Inc * (R - L + ) + Tree[root].Sum; /**否则我们需要向下继续更新 Inc*/
Tree[root].Sum += Tree[root].Inc * (Tree[root].R - Tree[root].L + ); Tree[lson].Inc += Tree[root].Inc;
Tree[rson].Inc += Tree[root].Inc; Tree[root].Inc = ;
/**向下递归求和*/
if(L > Tree[root].Mid() )
Sum += QuerySum(rson,L,R);
else if(R <= Tree[root].Mid() )
Sum += QuerySum(lson,L,R);
else
{
Sum += QuerySum(lson,L, Tree[root].Mid() );
Sum += QuerySum(rson,Tree[root].Mid()+, R);
} return Sum;
} int main()
{
int Q;
char ch[];
scanf("%d %d",&n, &Q);
Bulid(,,n);
for(int i=; i<=n; i++)
scanf("%d",&val[i]);
Bulid(,,n); while( Q-- )
{
int a, b, c;
scanf("%s", ch); if(ch[] == 'Q')
{
scanf("%d %d",&a, &b);
printf("%I64d\n", QuerySum(,a,b) );
}
else
{
scanf("%d %d %d",&a, &b, &c);
Add(,a,b,c);
}
}
return ;
}
POJ 3468 A Simple Problem with Integers(详细题解) 线段树的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- 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(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
随机推荐
- asp.net手动填充TreeView生成树
最近在做项目发现需要用到树的地方,页面的前台任然是使用一个asp.net的控件TreeView来显示树的结构,当然也可以自己在前台写一个树来展示,这在后期跟局功能的不同很大可能会要用到异步的知识,废话 ...
- iOS 事件处理机制与图像渲染过程(转)
iOS 事件处理机制与图像渲染过程 iOS RunLoop都干了什么 iOS 为什么必须在主线程中操作UI 事件响应 CALayer CADisplayLink 和 NSTimer iOS 渲染过程 ...
- KTV2
自制KTV点歌系统经验 重唱与切歌 1.重唱 这个简单,会了播放,就会了这个; 我们用的数组下标来确定播放歌曲,自然如果下标没变的话,那播放的就还是这首了.所以只要确定了要执行的命令是重唱就行了. 那 ...
- 【转】jquery两稳定版本比较~~
博客分类: Web前端 jquery jquery历经了多个版本的更新,版本上的比较貌似没什么必要性,一般来说新的版本会比旧的版本各方面都略有提升,但由于新版中增加了各种新的功能,难免会引起bug的 ...
- POJ 2391.Ombrophobic Bovines (最大流)
实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...
- sendmail服务器的安装
1.检查sendmail是否已安装: rpm -qa | grep sendmail 2.yum -y install sendmail 安装 yum -y remove sendmail ...
- linux之uniq
Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...
- php基础知识【函数】(4)时间date
一.time() -- 返回当前的 Unix 时间戳 $nextWeek = time() + (7 * 24 * 60 * 60); echo 'Next Week: '. date('Y-m-d' ...
- YII 小部件实现Area textArea
<?php echo $form->textArea($user_model,'introduce',array('cols'=>50,'rows'=>5)); ?>
- jQuery简单的轮播特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...