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

_(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说。

【问题描述】

成段加某一个值,然后询问区间和。

【思路】

讲一下pushdown和pushup出现的几个位置。

pushup:

(1)build的结尾,当叶子节点分别有对应的值后,它的父亲们就等于它们求和。

(2)update的结尾,因为此时当前根的左右孩子已经更新了,故它也需要更新。

pushdown(延迟标记):

*pushdown会出现在一切要从当前结点往下的位置

query和update中,要先把add的值传给两个儿子。

接下来整理一下思路,每一个子过程分别需要如何操作:

build(建立线段树)

(1)将add清零

(2)如果左子树等于右子树,则说明已经到达叶子节点,读入sum的值,返回

(3)向左右子树递归

(4)pushup更新当前根

update(修改)

(1)如果当前的L和R包含在l和r中,则让add+δ,让sum+δ*当前区间的长度,返回

(2)pushdown向下延迟标记

(3)向左右子树递归

(4)pushup更新当前根

query(查询)

(1)如果当前的L和R包含在l和r中,则直接返回sum[rt]

(2)pushdown向下延迟标记

(3)向左右子树递归,将得到的返回值加到result总

(4)返回result

一般的线段树就是这样操作的,其实还挺简单(。・∀・)ノ゙最近学业有点忙好久没写程序了,整理一个当作恢复一下手速。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int MAXN=+;
int n,q;
LL add[MAXN];
LL sum[MAXN]; void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int l,int r,int rt)
{
add[rt]=;
if (l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void pushdown(int rt,int m)
{
if (add[rt])
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(m-(m>>));
sum[rt<<|]+=add[rt]*(m>>);
add[rt]=;
}
} LL query(int L,int R,int l,int r,int rt)
{
if (L<=l && R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+);
int m=(l+r)/;
LL rs=;
if (m>=L) rs+=query(L,R,lson);
if (m<R) rs+=query(L,R,rson);
return rs;
} void update(int L,int R,int delta,int l,int r,int rt)
{
if (L<=l && R>=r)
{
add[rt]+=delta;
sum[rt]+=(LL)delta*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)/;
if (m>=L) update(L,R,delta,lson);
if (m<R) update(L,R,delta,rson);
pushup(rt);
} int main()
{
scanf("%d%d",&n,&q);
build(,n,);
for (int i=;i<q;i++)
{
char c[];
scanf("%s",c);
if (c[]=='C')
{
int fr,ed,ad;
scanf("%d%d%d",&fr,&ed,&ad);
update(fr,ed,ad,,n,);
}
else
{
int a,b;
scanf("%d%d",&a,&b);
cout<<query(a,b,,n,)<<endl;
}
}
return ;
}

【成端更新线段树模板】POJ3468-A Simple Problem with Integers的更多相关文章

  1. 线段树专题 POJ3468 A Simple Problem with Integers

    题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long ...

  2. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  3. POJ 3468 (线段树 区间增减) A Simple Problem with Integers

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

  4. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  5. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

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

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  7. poj3468 A Simple Problem with Integers(zkw区间修改模板)

    此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板 #include <cstdio> #include <cstring> #include <iostrea ...

  8. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  9. poj------(3468)A Simple Problem with Integers(区间更新)

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

随机推荐

  1. poj 1797

    2013-09-08 09:48 最大生成树,输出生成树中最短的边儿即可 或者对边儿排序,二份答案+BFS判断是否1连通N 时间复杂度都是O(NlogN)的 附最大生成树pascal代码 //By B ...

  2. esp8266 IOT Demo 固件刷写记录

    将编译好的固件按照下面地址刷写到esp8266 出现下面错误是因为刷写的设置不对,按照图上设置: load 0x40100000, len 26828, room 16 tail 12chksum 0 ...

  3. [Leetcode Week15]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...

  4. HEER-Easing Embedding Learning by Comprehensive Transcription of Heterogeneous Information Networks

    来源:KDD 2018 原文:HEER code:https://github.com/GentleZhu/HEER 注: 若有错误,欢迎指正   这篇KDD’18的文章,没有按照常规的方法将所有的n ...

  5. Name Disambiguation in AMiner-Clustering, Maintenance, and Human in the Loop

    Name Disambiguation in AMiner: Clustering, Maintenance, and Human in the Loop paper:http://keg.cs.ts ...

  6. 属性名、变量名与 内部关键字 重名 加&

    procedure TForm4.btn3Click(Sender: TObject); var MyQj: TQJson; MyPrinter: TPrinter; begin MyQj := TQ ...

  7. NYOJ 10 skiing(好题)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  8. hdu 2881(LIS变形)

    Jack's struggle Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. mysql中MAX()函数和count()函数的技巧使用

    1.max()函数 在考虑提高数据库io的情况下,可以创建索引 ===>create index 索引名称 on 表名(列名); 2.count()函数 问题:count(*)与count(某列 ...

  10. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...