题解报告: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 ...
随机推荐
- UVA 129_ Krypton Factor
题意: 一个字符串含有两个相邻的重复的子串,则称这个串为容易的串,其他为困难的串,对于给定n,l,求出由前l个字符组成的字典序第n小的困难的串. 分析: 按字典序在字符串末尾增加新的字符,并从当前字符 ...
- poj——2367 Genealogical tree
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6025 Accepted: 3969 ...
- MongoDB环境搭建教程收集(待实践)
先收集,后续再实践. https://my.oschina.net/leezhen/blog/207262 http://www.360doc.com/content/11/0708/09/26606 ...
- MySQL主从复制搭建教程收集(待实践)
先收集一下,后续再搭建测试. https://zhangge.net/4019.html http://www.cnblogs.com/jiangwenju/p/6098974.html http:/ ...
- mybatis bug之resultmap缺少object-relation匹配参数password,造成设置密码不成功
1.mybatis bug之resultmap缺少object-relation匹配参数password,造成设置密码不成功 在resultmap里没有设置user类中password属性和数据库表t ...
- linux命令dmesg查看进程被杀死原因
有一次一个python进程挂了,使用了下这个命令. 可以看到原因:虚拟机总共内存8082608KB,结果python项目就消耗掉了7341764KB,内存泄露,导致python进程被系统杀死 顺带介绍 ...
- golang 中可变参数的个数
package main import "fmt" func Greeting(prefix string, who ... string) { fmt.Println(prefi ...
- ubuntu Install Firefox
Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox,给每个版本的 Firefox 创建一个单独的文件夹. 例如:1.Firefox 版 ...
- 手动安装Firefox Linux
(2015-06-05 17:22:19)[编辑][删除] 转载▼ 标签: 股票 Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox, ...
- vuex 存值 及 取值 的操作
1.传值 // 定义参数 let params = { workItemId: workItemId, flowInstId: flowInstId, itemStatus: itemStatus, ...