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 ...
随机推荐
- [转]PHP base64_encode 在URL地址参数编码上使用
因为我使用公钥,使用php的 openssl 加密之后的资料 base64_encode()字符串,然后放入请求 url 中当作参数,urlencode(base64_encode()),然后用 ur ...
- 关于MySQL的insert添加自动获取日期的now()的用法
例如我的MySQL数据库里有个表table1,它的字段有id,date1,date2,除id外都是Datetime类型的.那么插值语句这样写:insert into table1(date1,date ...
- 数字IC设计工程师成长之路
学习的课程 仿真工具VCS实践学习 2019年12月9日-2019年12月23日
- Go 位运算符
Go 位运算符 package main import "fmt" func main() { var a uint = 60 /* 60 = 0011 1100 */ var b ...
- php上传(一)
php上传基础知识总结 1.文件上传 1>文件上传的原理:将客户端上的文件通过浏览器上传到服务器上, 在将服务器上的文件移动到指定目录. 2>客户端的配置 表单页面,浏览框 method= ...
- BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)
题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...
- Git 远程仓库分支管理
目录 目录 速查表 关联远程代码仓库 克隆远程仓库 分支管理 创建分支 切换分支 合并分支 删除分支 解决冲突 速查表 指令 作用 git branch 查看分支 git branch newBran ...
- 实现读取文本数据,在将数据导入mysql
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java ...
- Harry and magic string HDU - 5157 记录不相交的回文串对数
题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...
- 织梦自增函数[field:global name=autoindex/]常见用法
看来不少朋友需要不了解这个自增函数的用法,在这里我列举一些常见的写法以及作用. [field:global name=autoindex/] !--普通打印递增的数字-- [field:globa ...