A Simple Problem with Integers

    题目大意:给出n个数,区间加、查询区间和。

    注释:1<=n,q<=100,000.(q为操作次数)。

      想法:嗯...学了这么长时间线段树,发现我tm竟然不会lazy标记??!好吧,看来线段树又要重新来了。

        关于这道题,主要是联系lazy标记的使用。如果每一次查询的时间复杂度都输满nlogn,那么就gg了。线段树之所以快的原因在于我们在查询到一个可以代替原来查询区间一部分的节点,我们就在这个节点上打一个lazy标记。那么我就不需要继续想该节点之后去遍历。而一个lazy标记表示这个节点的所有子节点都进行了同样的操作。那么如果我在同一个节点处打两个lazy标记,本题是区间加法,我们需要将两个lazy标记相加。我们在需要遍历的时候发现一个节点有lazy标记,我们需要将lazy标记向下推。为什么?

          因为我们打标记是为了节约时间,但是lazy标记下面的节点在当前时刻的值是没有发生更改的,所以若我们需要下面节点的值,则我就必须将上面的lazy标记向下推,就是pushdown。

          我们发现这样的话最上面的lazy标记显然是最新的。紧接着,推下lazy标记之后,我们需要将lazy标记所影响的点的值上传给father,就是pushup。

      如果最后存在一个节点,它仍然有lazy标记但是它始终没有没第二次查询或者修改,我们就节省了将lazy标记作用在它子树里的时间,这就是lazy标记节省时间的根本。

    最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson pos<<1
#define rson pos<<1|1
#define N 100010
using namespace std;
typedef long long ll;
ll lazy[4*N];
int high[N];
ll sum[4*N];
void build(int pos,int l,int r)//建树
{
int mid=(l+r)>>1;
if(l==r)
{
sum[pos]=high[l];
return;
}
build(lson,l,mid);
build(rson,mid+1,r);
sum[pos]=sum[lson]+sum[rson];//pushup
}
inline void pushdown(int pos,int l,int r)
{
if(lazy[pos])
{
int mid=(l+r)>>1;
sum[lson]+=(mid-l+1)*lazy[pos];//推标记
lazy[lson]+=lazy[pos];
sum[rson]+=(r-mid)*lazy[pos];
lazy[rson]+=lazy[pos];
lazy[pos]=0;
}
}
void fix(int pos,int l,int r,int x,int y,ll val)
{
int mid=(l+r)>>1;
if(x<=l&&r<=y)
{
lazy[pos]+=val;
sum[pos]+=(r-l+1)*val;
return;
}
pushdown(pos,l,r);
if(x<=mid)//OTZ ZTY
{
fix(lson,l,mid,x,y,val);
}
if(y>mid)//OTZ ZTY
{
fix(rson,mid+1,r,x,y,val);
}
sum[pos]=sum[lson]+sum[rson];//pushup
}
ll query(int pos,int l,int r,int x,int y)
{
int mid=(l+r)>>1;
ll temp=0;
if(x<=l&&r<=y)
{
return sum[pos];
}
pushdown(pos,l,r);
if(x<=mid)
{
temp+=query(lson,l,mid,x,y);
}
if(y>mid)
{
temp+=query(rson,mid+1,r,x,y);
}
return temp;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&high[i]);
}
build(1,1,n);
char s[10];
for(int a,b,c,i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(1,1,n,a,b));
}
else
{
scanf("%d%d%d",&a,&b,&c);
fix(1,1,n,a,b,c);
}
}
return 0;
}

    小结:重新开始线段树qwq

[poj3468]A Simple Problem with Integers_线段树的更多相关文章

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

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

  2. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  3. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  4. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  9. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

随机推荐

  1. Android常见Crash原因总结(二)

    Android平台程序崩溃大家都应该遇到过,force close和ANR应该是大家遇到较多的. 这里把Android平台程序崩溃的各种类型做一个简述和原因列举. 1.ANR(可见ANR): 发生场景 ...

  2. Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "</url-pattern>" 终止

    1.错误描述 严重: Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "< ...

  3. C# 对象数据转换Json帮助类 JsonHelp

    C# 对象数据转换Json帮助类 JsonHelp using System; using System.Data; using System.Configuration; using System. ...

  4. VMware vSphere学习整理

    知识点整理 内存选择 一般来说,每个虚拟机需要的内存在1~4GB甚至更多,还要为VMware ESXi预留一部分内存 2个6核的2U服务器配置64GB内存,4个6核或8核心的4U服务器配置128GB或 ...

  5. RobotFramework下的http接口自动化Set Request Header 关键字的使用

    Set Request Header 关键字用来设置http请求时的请求头部信息. 该关键字接收两个参数,[ header_name | header_value ] 示例1:设置http请求时的Re ...

  6. 【BZOJ5020】【THUWC2017】在美妙的数学王国中畅游(Link-Cut Tree,组合数学)

    [BZOJ5020][THUWC2017]在美妙的数学王国中畅游(Link-Cut Tree,组合数学) 题解 Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙 ...

  7. [BZOJ4825][HNOI2017]单旋spaly

    BZOJ Luogu 题目太长了,就不放了. 题解 首先声明一点,无论是splay还是spaly,插入一个新的元素,都要rotate到根!所以说题目也算是给了一个错误示范吧. 我们发现把最值旋转到根并 ...

  8. [BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)

    Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...

  9. C# 委托Delegate的使用 笔记

    使用delegate总是一头雾水,记录一下笔记,备忘. 主要用于线程间操作UI上的控件,以便使用.或者是大家统一操作入口使用. using System.Windows.Forms; namespac ...

  10. 误操作导致 lvdisplay 命令不存在解决

    1.lvdisplay 命令不存在 查看lvm2 包被卸载2.执行 yum install lvm2 命令 发现 yum 被锁 3.删除yum.lock 发现/ 目录只读4.mount -o remo ...