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 ...
随机推荐
- mysql 安装employees db的步骤
因为准备要开始学习ASP.NET的高级部分,所以今晚想安装一个数据库示例,百度发现原来mysql有个employees db 但是单纯按照网上的方法,也是没有办法导入的,所以写了这篇博文,作为笔记. ...
- css 实现页面加载中等待效果
<!DOCTYPE html> <html> <head> <title>css实现页面加载中,请稍候效果</title> <meta ...
- 谁是谁的first-child
看过CSS伪类选择器之后,心想也就如此嘛,:first-child选择元素的第一个子元素,有什么难的,可一到实践中,还是到处碰壁啊. <body> <ul class="f ...
- CSS3 box-sizing 属性
定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...
- java 对于url地址的实体符号的处理
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 <dependency> <g ...
- hdoj 4310 贪心
不知为毛,过不了 我的代码: #include<stdio.h> int main(){ int n,a[30],b[30],temp,i,j,s1,s2; double c[30]; w ...
- js 之 复制一段代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP 用Class构造JSON数据
header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...
- windows core audio apis
这个播放流程有一次当初不是很理解,做个记录,代码中的中文部分,原文档是有解释的:To move a stream of rendering data through the endpoint buff ...
- phpcms V9 内容模型管理(转)
转自:http://www.cnblogs.com/Braveliu/p/5102627.html [1]理解模型 模型,系统知识的抽象表示.既然抽象了,那就得脑补一下.大家都是面向对象设计的专业人员 ...