这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的。

此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的

具体思路

在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的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(详细题解) 线段树的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  7. 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 ...

  8. 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 ...

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

随机推荐

  1. PhoneGap 在eclipse上开发Android程序

    本文将记录在Eclipes上开发Android App,在使用的方法是Hybrid App(混合模式移动应用), 由于本人的工作需要,将要开发在车间使用的数据录入程序,但是其中有非常多的逻辑验证和判断 ...

  2. C#学习第一天

    主要看了一些关于C#的发展期情况,对这门语言有了初步的了解,下面慢慢道来. 首先是C#语言的特点,相比较其他的语言,C#具有以下突出的特点: 1.语法简洁,不允许直接操作内存,去掉了指针操作: 2.彻 ...

  3. Conversion Between DataTable and List in C#

    1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...

  4. Axiom3D学习日记 4.地形,天空,雾

    首先需要引用Axiom.SceneManagers.Octree.dll. 地形: 载入地形配置,从一个文件中. scene.LoadWorldGeometry( "Terrain.xml& ...

  5. Premature optimization is the root of all evil.

    For all of we programmers,we should always remember that "Premature optimization is the root of ...

  6. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  7. (转)[开发笔记]-js判断用户的浏览设备是移动设备还是PC

    <script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...

  8. ReactiveCocoa入门教程——第一部分

      ReactiveCocoa iOS 翻译    2015-01-22 02:33:37    11471    6    15 本文翻译自RayWenderlich  ReactiveCocoa ...

  9. pthread_rwlock_t读写锁函数说明

    读写锁 索引: 初始化一个读写锁pthread_rwlock_init 读锁定读写锁      pthread_rwlock_rdlock 非阻塞读锁定 pthread_rwlock_tryrdloc ...

  10. iOS 数据持久性存储-对象归档

    对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化) 主要涉及两个类:NSKeyedArichiver.NSKey ...