poj-3468-A Simple Problem with Integers-线段树入门+区间更新
题意:
C:对区间[l,r]每一个数+c;
Q:查询区间[l,r]的所有元素的总和。
线段树修改和查找的时间复杂度都是O(logn)。
线段树基本思想:分治。
线段树基本操作:建树、区间查询(最值;和)、区间修改(更新)、单点修改、单点查询。
注意这题,输入说是一行 N、q 单组输入,但是会TLE,多组输入才可以AC。
AC代码:
//题意:
//C:对区间[l,r]每一个数+c;
// Q:查询区间[l,r]的所有元素的总和。 //线段树修改和查找的时间复杂度都是O(logn)。
//线段树基本思想:分治。
//线段树基本操作:建树、区间查询(最值;和)、区间修改(更新)、单点修改、单点查询。 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath> using namespace std;
#define inf 0x3f3f3f3f;
#define pi acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
typedef long long ll; const int N=1e5+;
ll a[N<<],lazy[N<<];//需要开到节点的四倍大小 void build(int L,int R,int i)
{
if(L==R)//当左右结点相同的时候,说明该节点可以建树,输入即可。
{
scanf("%lld",&a[i]);//即为叶子结点
return;//因为已经确定这个点可以输入了,也就类似叶结点,返回函数上次调用的地方即可。
} //否则往下继续找
int mid=(L+R)>>;
build(L,mid,i<<);//递归建立左子树
build(mid+,R,i<<|);//递归建立右子树
a[i]=a[i<<]+a[i<<|];//统计该点(i)的左子树和右子树之和
//a这个操作也可以另外写到一个函数pushup中(即pushup(i)),这个看自己怎么写代码
//节点数据向上更新 //根据题意写,这一题是求区间和,之前左区间和右区间相加即可
//例如如果求区间内最大值,则写成:a[i]=max(a[i<<1],a[i<<1|1]);
} void pushdown(int i,int len)//节点懒惰标记下推
{
if(lazy[i])//如果懒惰标记为真,说明之前有过懒惰标记,现在需要进行更新
{
lazy[i<<]+=lazy[i];//懒惰标记往左结点传
lazy[i<<|]+=lazy[i];//懒惰标记往右结点传
//左右用 |1 区分
//因为求区间和,所以当区间内每个元素加上一个值时,区间的和也加上这个值
//对于区间求和, 原数组值需要加上lazy标记*子树所统计的区间长度
a[i<<]+=lazy[i]*(len-(len>>));//(len-(len>>1)是左区间的长度
a[i<<|]+=lazy[i]*(len>>);//(len>>1)是右区间的长度
lazy[i]=;//由于懒惰标记向下传递,所以当前节点的懒惰标记取消
}
//对于区间求最大值, 子树的值不需要乘以长度, 所以不需要传递参数区间长度len。
} //注意:
// 1、单点更新, 不需要用到lazy标记
// 2、成段(区间)更新, 需要用到lazy标记来提高时间效率
void update(int x,int y,int L,int R,int i,int pluss)
{
if(L>=x&&R<=y)//当前节点区间包含在查询区间内
//范围缩小到left和right之间
{
a[i]+=pluss*(R-L+);
lazy[i]+=pluss;
return;
}
pushdown(i,R-L+);
int mid=(L+R)>>; //更新区间
if(x<=mid)//更新左区间
update(x,y,L,mid,i<<,pluss);
if(y>mid)//更新右区间
update(x,y,mid+,R,i<<|,pluss); //更新结点值
a[i]=a[i<<]+a[i<<|];
} ll query(int x,int y,int L,int R,int i)//查询操作
{
if(L>=x&&R<=y)//当前节点区间包含在查询区间内
return a[i];//返回当前值
pushdown(i,R-L+);
int mid=(L+R)>>;
ll ans=;
if(x<=mid)//递归查询左子树内部的的区间值
ans+=query(x,y,L,mid,i<<);
if(y>mid)//递归查询右子树内部的的区间值
ans+=query(x,y,mid+,R,i<<|);
return ans;//返回题目所需的区间和(左+右)
} int main()
{
int n,q;
while(~scanf("%d %d",&n,&q))
{
mem(lazy,);//如果多组数据lazy数组需要进行清空
mem(a,);
build(,n,);//开始建树,传入树的总区间(传入最左端点,最右端点)和树的根节点
//建树的过程中输入每一个节点
for(int i=;i<=q;i++)
{
char ch;
getchar();//吸收每次读入的空格
scanf("%c",&ch);
if(ch=='Q')//询问区间内的和
{
int x,y;
scanf("%d %d",&x,&y);
ll ans=query(x,y,,n,);
printf("%lld\n",ans);
}else if(ch=='C')//往区间内每一个数上都插入pluss
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
update(x,y,,n,,z);
}
}
}
return ;
}
poj-3468-A Simple Problem with Integers-线段树入门+区间更新的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- 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 ...
- 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 ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- IDEA使用maven插件打jar包流程
idea使用maven插件打jar包步骤以及遇到的问题 idea自带了maven工具,idea右边点击maven选项: 一.在pom中添加插件,直接复制就好,如下选项 <plugin> & ...
- leetcode-数组的相对排序
Python解法: def relativeSortArray(arr1, arr2): arr = [0 for _ in range(110)] new = [] for a in range(l ...
- Delphi实现屏幕截图、窗口截图、指定区域截图
Use Jpeg procedure TForm1.snapscreen(a,b,c,d:Integer); var bmpscreen:Tbitmap; jpegscreen:Tjpegimage; ...
- NX二次开发-获取坐标系信息UF_CSYS_ask_csys_info
NX9+VS2012 #include <uf.h> #include <uf_csys.h> UF_initialize(); //获取WCS标识 tag_t WcsId = ...
- Python-爬虫之股转系统下载文件自动翻页
上次代码只能抓取一个网页上的链接,本次可以自主设定抓取的页面个数. 代码如下: from selenium import webdriver import os, time class Downloa ...
- CSS:CSS 总结
ylbtech-CSS:CSS 总结 1.返回顶部 1. 你已经学习了CSS,下一步学习什么呢? CSS 总结 本教程已向你讲解了如何创建样式表来同时控制多重页面的样式和布局. 你已经学会如何使用 C ...
- 一幅图解决R语言绘制图例的各种问题
一幅图解决R语言绘制图例的各种问题 用R语言画图的小伙伴们有木有这样的感受,"命令写的很完整,运行没有报错,可图例藏哪去了?""图画的很美,怎么总是图例不协调?" ...
- 第36讲 谈谈MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景
在日常开发中,尤其是业务开发,少不了利用 Java 对数据库进行基本的增删改查等数据操作,这也是 Java 工程师的必备技能之一.做好数据操作,不仅仅需要对 Java 语言相关框架的掌握,更需要对各种 ...
- 剑指offer——18打印从1到最大的n位数
题目: 输入数字n,按顺序打印出从1到最大的n位十进制数.比如输入3,则打印出1.2.3一直到最大的3位数999. 题解: 注意大数溢出问题,故使用字符串更靠谱 class Solution { pu ...
- 更改idea启动内存信息
1.到idea的安装目录的bin下,找idea64.exe.vmoptions 文件 2.更改参数 对应的参数解释: -Xms1024m 设置IDEA初时的内存大小,提高Java程序的启动速度. ...