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. CPU特性漏洞测试(Meltdown and Spectre)

    2018年1月4日,国外安全研究人员披露了名为"Meltdown"和"Spectre"两组CPU特性漏洞,该漏洞波及到近20年的Intel, AMD, Qual ...

  2. mac 卸载idea

    卸载MAC中的IDEA Intellij 首先在应用里面右键移动到垃圾桶 然后使用命令行: cd Users/xxx/Library/ 上面的xxx对应你的用户名,然后输入 rm -rf Logs/I ...

  3. thinkjs 中增加过期时间

    使用thinkjs搭建的项目需要实现一小时后过期的功能:于是对比了新建项目与原有项目的不同之处:         官网中给的介绍:https://thinkjs.org/zh-cn/doc/2.2/a ...

  4. 如何在taro的map循环中使用if条件渲染

    在taro的jsx中,鉴于编译的机制,官方明确的表示了不能在map循环中使用if循环, 但是呢,官方也给出了解决办法,那就是提取变量或者是用三目运算嵌套的方法: 链接奉上:https://github ...

  5. iOS富文本组件的实现—DTCoreText源码解析 渲染篇

    本文转载至 http://blog.cnbang.net/tech/2729/ 上一篇介绍了DTCoreText怎样把HTML+CSS解析转换成NSAttributeString,本篇接着看看怎样把N ...

  6. Android数据存储之SD卡

    为了更好的存取应用程序的大文件数据,应用程序需要读. 写SD卡上的文件.SD卡大大扩充手机的存储能力. 操作SD首先要加权限: <!--在SDCard中创建与删除文件权限 --> < ...

  7. JS基础---->javascript的基础(二)

    记载javascript的一些基础的知识.我们在春风秋雨中无话不说,又在春去秋来中失去了联系. js中string类型 一.字符方法:charAt() 和 charCodeAt() var strin ...

  8. 解决VMware安装Ubuntu的过程中窗口过小无法看到按钮的问题

    最近在用VMware安装Ubuntu的时候,发现竟然只能看到部分界面,鼠标拖拽也没有用,就是看不到完整的界面,那要我怎么按下一步啊~(真是哭笑不得%>_<%),或者按TAB键,靠自己的想象 ...

  9. Linux 安装GCC讲解(在线和无网离线)

    本文主要介绍如何在无网络的环境下怎么离线安装GCC,如果有网,只需要通过命令 yum install gcc 进行安装就可以了,yum会自动把所有关联的依赖包也一起安装了,一键安装. yum inst ...

  10. vsCode_1.27.2

    User Settings: 一,当前行高亮显示: "editor.renderLineHighlight": "line", 二,如何呈现空白字符(一般选no ...