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 ...
随机推荐
- E/Trace: error opening trace file: No such file or directory
E/Trace: error opening trace file: No such file or directory (2) 有这一个错误,想了一下,然后发现是 AdroidManifest.xm ...
- CentOS 6.5 64位,调整分区大小
调整硬盘分区大小 想增加root空间,减少home空间. 1.查看硬盘使用情况. [root@npm ~]# df -h Filesystem Size Used Avail Use% Mounted ...
- 打造属于前端的Uri解析器
今天和大家一起讨论一下如何打造一个属于前端的url参数解析器.如果你是一个Web开发工程师,如果你了解过后端开发语言,譬如:PHP,Java等,那么你对下面的代码应该不会陌生: $kw = $_GET ...
- [转]单例模式与静态变量在PHP中
在PHP中,没有普遍意义上的静态变量.与Java.C++不同,PHP中的静态变量的存活周期仅仅是每次PHP的会话周期,所以注定了不会有Java或者C++那种静态变量. 所以,在PHP中,静态变量的存在 ...
- UIView用户事件响应
UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为 ...
- 增强iOS应用程序性能的提示和技巧(25个)
转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...
- php文件粘贴上传
<?php header("Access-Control-Allow-Origin:*"); $url = 'http://'.$_SERVER['HTTP_HOST']; ...
- PSD 转化成 HTML
一般情况下,网页设计制作完成的工作实际是:psd 效果图 转成 html+CSS 的模板页面,一般情况下,我们会拿到美工的 psd,不同的人会有不同的做法: 打开fireworks将图片切割导出为ht ...
- 将日期和时间作为 struct tm型的值直接向二进制文件进行读写
#include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...
- OpenCV2.4.9 & Visual Studio 2010 环境配置篇
1. 准备工作 1.1. 安装 Visual Studio 2010, 需要安装 VC++ 相关功能.具体可求助度娘. 1.2. 下载 OpenCV 2.4.9 For Windows:https:/ ...