http://poj.org/problem?id=3468

 

题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),

现在有一些操作:

C a b c, 把区间a--b内全部加上c

Q a b,求区间ab的值和。

线段树 改变整个区间的数

这题不能直接更新到树的叶子节点, 因为那样时间复杂度太高,我们可以在每个节点上加一个变量k,表示这个节点的所有点(L到R)都要增加 k, 所以我们可以在从上往下查找的过程中如果不是所求区间,那么我们就把本区间加上应该加的数,否则的话,就停止,这样每次更新的过程时间复杂度也是log n,查找时, 我们需要把含有K值得那些点放到它的子节点上去,只需要一层就可以了,具体过程看代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define INF 0xfffffff
#define N 400050
#define Lson root<<1
#define Rson root<<1|1 struct SegmentTree
{
int L, R;
long long sum, k;
int Mid()
{
return (L+R)>>1;
}
int len()
{
return R-L+1;
}
} a[N<<2]; void BuildSegTree(int root, int L, int R)
{
a[root].L = L;
a[root].R = R;
a[root].k = 0;
if( L == R )
{
scanf("%lld", &a[root].sum);
return ;
} BuildSegTree(Lson, L, a[root].Mid());
BuildSegTree(Rson, a[root].Mid()+1, R); a[root].sum = a[Rson].sum + a[Lson].sum;
} void Update(int root, int L, int R, int k)
{
a[root].sum += (R - L + 1) * k; if(a[root].L == L && a[root].R == R)///当到达要更新的那个区间时,把k值更新,并返回;
{
a[root].k += k;
return ;
} if(R <= a[root].Mid())///右边;
Update(Lson, L, R, k);
else if(L > a[root].Mid())///左边;
Update(Rson, L, R, k);
else///中间;
{
Update(Lson, L, a[root].Mid(), k);
Update(Rson, a[root].Mid()+1, R, k);
}
} void Down(int root)
{
a[Lson].sum += a[Lson].len()*a[root].k;
a[Rson].sum += a[Rson].len()*a[root].k;///左右儿子的和都要增加对应的值,注意这里看清楚增加的是谁;
a[Lson].k += a[root].k;
a[Rson].k += a[root].k;///接着往下传递K值;
a[root].k = 0;///传下去之后就置0;
}
long long Query(int root, int L, int R)
{
if(a[root].L==L && a[root].R == R)///当刚好是这个区间时返回结果;
return a[root].sum; Down(root);///往下传递K值; if(R <= a[root].Mid())
return Query(Lson, L, R);
else if( L > a[root].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[root].Mid());
long long Rsum = Query(Rson, a[root].Mid()+1, R);
return Lsum + Rsum;
}
} int main()
{
int n, m, L, R, k;
long long ans;
char s[10];
while(scanf("%d %d", &n, &m) != EOF)
{
BuildSegTree(1, 1, n);
for(int i=0; i<m; i++)
{
scanf("%s", s);
if(s[0] == 'Q')
{
scanf("%d %d", &L, &R);
ans = Query(1, L, R);
printf("%lld\n", ans);
}
else
{
scanf("%d %d %d", &L, &R, &k);
Update(1, L, R, k);
}
}
}
return 0;
}
/*
100
2 3 4 5 6 7 8 9 10
Q 1 5
C 1 10 1
Q 3 5 */

  

A Simple Problem with Integers---poj3468线段树的更多相关文章

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

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

  2. POJ3468:A Simple Problem with Integers(线段树模板)

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

  3. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

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

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

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

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

  6. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

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

  7. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

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

  8. POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)

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

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

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

  10. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

随机推荐

  1. dedeCMS解码

    var str = 'arrs1[]=99&arrs1[]=102&arrs1[]=103&arrs1[]=95&arrs1[]=100&arrs1[]=98& ...

  2. 使用 urllib 解析 URL 链接

    urllib 库还提供了 parse 模块,它定义了处理 URL 的标准接口,例如实现 URL 各部分的抽取.合并以及链接转换,常用的方法如下: In []: from urllib.parse im ...

  3. Qt Creator build遇到error lnk1158 无法运行rc.exe

    解决办法: 将C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x64 目录下的rc.exe 和rcdll.dll 复制到 C:\Prog ...

  4. Zabbix的自定义键值和自动发现功能监控Oracle数据库的表空间

    前面介绍了利用Orabbix监控了,参考zabbix通过Orabbix监控oracle数据库,这里我们原先的模板中进行了修改,使用自动发现功能实现监控tablespace的使用情况. 1. 在被监控的 ...

  5. JTAG、JLink、ULINK、ST-LINK仿真器区别(转)

    首先要了解一下JTAG. JTAG协议 JTAG(Joint Test Action Group,联合测试行动小组)是一种国际标准测试协议(IEEE 1149.1兼容),主要用于芯片内部测试.现在多数 ...

  6. struts1的配置文件详解11111

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...

  7. 驱动保护中的ObjectType_Callback探索

    最近学习驱动保护,有点小小心德与大家分享下. 当前环境:VM中的win7 32 保护程序是某游戏的驱动保护. 具体现象是:在用PCHunter工具查看object钩子时发现如下的信息: 疑问点1:在H ...

  8. WP8.1学习系列(第八章)——透视Pivot设计指南

    在本文中 描述 应做事项和禁止事项 其他使用指南 相关主题 重要的 API Pivot class (XAML) PivotItem class (XAML) Windows Phone 应用:具有透 ...

  9. 金蝶KIS问题解决汇总

    1.帐套结转时,提示t_subsys插入重复键 解决: I.删除索引  alter table t_subsys drop constraint pk_subsys  II.t_rp_initial表 ...

  10. 关于SQL优化(转载,格式有调整)

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...