题解报告: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 ...
随机推荐
- 2018/2/20 Springretry,Feign,以及用通俗的语言(自认为)教会你关于Hystrix的复杂概念
本来想昨天写的,但临时有事.弄的一晚上都没睡觉,现在头好晕,所以此笔记如果有语言措辞的组织不当,还请见谅:最后,本文可能涉及到大量专业名词,我会尽量用通俗的语句去阐述清楚它们的意思,但如果还是没看懂, ...
- [NOIP2006] 提高组 洛谷P1066 2^k进制数
题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后 ...
- Linux中kill,pkill,killall和xkill命令汇总讲解
终止一个进程或终止一个正在运行的程式,一般是通过 kill .killall.pkill.xkill 等进行.比如一个程式已死掉,但又不能退出,这时就应该考虑应用这些工具. 另 外应用的场合就是在服务 ...
- Linux下汇编语言学习笔记47 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- web文件管理系统和日志实时监控工具
https://blog.csdn.net/xuesong123/article/details/52752384
- Codeforces 645C Enduring Exodus【二分】
题目链接: http://codeforces.com/contest/645/problem/C 题意: 给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的 ...
- python 交互模式 方向键乱码问题解决
python交互模式下通常用向上键来找到之前执行的命令,用左右键移动光标.这很方便.但有的时候这些键在按完后却会出现乱码. 本文只解决CentOS 6.4 下 python2.7.8 的乱码问题. 这 ...
- seajs入门使用
使用 Sea.js 进行模块化开发还能够带来非常多优点: 模块的版本号管理. 通过别名等配置,配合构建工具,能够比較轻松地实现模块的版本号管理. 提高可维护性.模块化能够让每一个文件的职责单一,很有利 ...
- Growth: 一个关于怎样成为优秀Web Developer 的 App
想了想还是决定在今天公布一个预览版.这样才干持续改进.Growth是一个关于怎样成为优秀的Web Developer的APP--结合技能树.成长路线图.进阶书单.Web七日谈以及一些小測验. 它是我对 ...
- protobuf入门
,step1:准备.proto文件 syntax = "proto3"; message gps_data { int64 id = 1; string terminalId = ...