题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
#include<string.h>
#include<cstdio>
using namespace std;
typedef long long LL;
const int maxn=;
int n,q,a,b;LL c,s[maxn],lazy[maxn<<],sum[maxn<<];char ch;
void build(int l,int r,int x){//建树基本操作
int mid=(l+r)>>;
if(l==r){sum[x]=s[mid];return;}
build(l,mid,x<<);
build(mid+,r,x<<|);
sum[x]=sum[x<<]+sum[x<<|];
}
void push_down(int x,int len){//下放懒标记
if(lazy[x]){
lazy[x<<]+=lazy[x];//延迟修改量是叠加的,沿着子树可以继续更新下去
lazy[x<<|]+=lazy[x];
sum[x<<]+=(LL)(len-(len>>))*lazy[x];//更新左子节点的值:加上左区间长度乘上父节点的懒标记
sum[x<<|]+=(LL)(len>>)*lazy[x];//更新右子节点的值:加上右区间长度乘上父节点的懒标记
lazy[x]=;//同时标记父节点已经修改完成,即置懒标记为0
}
}
void modify(int l,int r,int x,LL c){
if(a<=l&&r<=b){//如果[a,b]包含了当前子区间[l,r],则直接进行懒标记,不再递归下去
lazy[x]+=c;//叠加懒标记值
sum[x]+=(LL)c*(r-l+);//同时累加修改值乘上当前子区间的长度
return;
}
push_down(x,r-l+);//如果修改区间不包含当前子区间,并且当前子区间有懒标记,则下放懒标记
int mid=(l+r)>>;
if(b<=mid)modify(l,mid,x<<,c);
else if(a>mid)modify(mid+,r,x<<|,c);
else{
modify(l,mid,x<<,c);
modify(mid+,r,x<<|,c);
}
sum[x]=sum[x<<]+sum[x<<|];//修改某个区间后还要向上更新父节点的值
}
LL query(int l,int r,int x){
if(a<=l&&r<=b)return sum[x];//如果访问的区间[a,b]包含子区间[l,r],直接返回返回当前区间的值
int mid=(l+r)>>;
push_down(x,r-l+);//如果不包含子区间,并且当前节点有被懒标记,则应下放懒标记,因为查询的区间可能更小(最小到叶子节点),为避免少计算,还要这步操作,此时就不用向上更新了,修改区间值才要
if(b<=mid)return query(l,mid,x<<);
else if(a>mid)return query(mid+,r,x<<|);
else return query(l,mid,x<<)+query(mid+,r,x<<|);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=;i<=n;++i)scanf("%lld",&s[i]);
memset(lazy,,sizeof(lazy));//注意:将每个节点的懒标记都标记为0
build(,n,);//建树
while(q--){
getchar();//吃掉回车符避免对字符输入的影响
scanf("%c",&ch);
if(ch=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",query(,n,));
}
else{
scanf("%d%d%lld",&a,&b,&c);
modify(,n,,c);
}
}
return ;
}
AC代码二之树状数组(2125ms):裸题,套一下树状数组区间查询和区间修改模板即可。
#include<string.h>
#include<cstdio>
typedef long long LL;
const int maxn=;
LL n,q,l,r,k,val[maxn],sum1[maxn],sum2[maxn];char op;
void add(LL *sum,LL x,LL val){
while(x<=n){sum[x]+=val;x+=(x&-x);}
}
LL get_sum(LL *sum,LL x){
LL ans=;
while(x>){ans+=sum[x];x-=(x&-x);}
return ans;
}
LL ask(LL x){
return x*get_sum(sum1,x)-get_sum(sum2,x);
}
int main(){
while(~scanf("%lld%lld",&n,&q)){
memset(sum1,,sizeof(sum1));
memset(sum2,,sizeof(sum2));
memset(val,,sizeof(val));
for(LL i=;i<=n;++i){
scanf("%lld",&val[i]);
add(sum1,i,val[i]-val[i-]);//维护差分数组
add(sum2,i,(i-)*(val[i]-val[i-]));
}
while(q--){
getchar();//吸收回车符避免对单个字符读取的影响
scanf("%c",&op);
if(op=='C'){
scanf("%lld%lld%lld",&l,&r,&k);
add(sum1,l,k),add(sum1,r+,-k);
add(sum2,l,(l-)*k);add(sum2,r+,-r*k);
}
else{
scanf("%lld%lld",&l,&r);
printf("%lld\n",ask(r)-ask(l-));//区间查询[1,r]-[1,l-1]=[l,r]
}
}
}
return ;
}
题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)的更多相关文章
- 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 [题目链接]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 Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- 郁闷的出纳员(bzoj 1503)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- redis可视化界面的操作【二十一】
1.安装 2.linux服务器中开启linux服务 root@qiaozhi:~# cd /usr/local/redis root@qiaozhi:/usr/local/redis# ./bin/ ...
- Visual C++ 网络编程 笔记
第一章 网络分层模型 OSI模型应用层:服务于应用程序的协议,比如用于域名解析的DNS协议,用于下载界面内容的HTTP协议表示层:处理不同硬件和操作系统之间的差异,确保应用层之间顺利通信 and 加密 ...
- 18.9.22 noip模拟赛
此题为找规律.期望100 实际100 #include<cstdio> #include<cstring> #include<iostream> #include& ...
- LTTNG 使用
http://lttng.org/docs/#doc-channel http://www.cnblogs.com/suncoolcat/p/3366045.html http://blog.csdn ...
- oracle11g expdp/impdp数据库
oracle11g导入/导出数据库 导出 .创建目录 sqlplus / as sysdba create directory dbDir as 'd:\oralce_sdic_backup\'; g ...
- Mybatis中insert中返回主键ID的方法
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...
- 解决confluence的乱码问题
使用confluence时发现一些含有中文的页面中,中文都变成了问号. 继续搜索解决方案,发现时数据库中数据的格式不对, 在mysql中输入以下命令: mysql> show variabl ...
- 几种常见排序算法的java实现
一.几种常见的排序算法性能比較 排序算法 最好时间 平均时间 最坏时间 辅助内存 稳定性 备注 简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定 n小时较好 直接插入排序 O( ...
- react 项目实战(九)登录与身份认证
SPA的鉴权方式和传统的web应用不同:由于页面的渲染不再依赖服务端,与服务端的交互都通过接口来完成,而REASTful风格的接口提倡无状态(state less),通常不使用cookie和sessi ...