Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.
解题思路:题意很清楚,大区间修改和询问:线段树+lazy懒标记,or树状数组,详解在代码里,注意求和用long long类型!
AC代码一之线段树懒标记(2500ms):
 #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树状数组)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  5. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  6. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  7. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  8. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  9. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. Frequent values(poj 3368)

    题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 代码: /* rmq算法 当询问到x,y时,设在x之后并且与a[x]相同的最后一个数编号为t,那么x到t之 ...

  2. 移动端click事件延迟300ms该如何解决

    window.addEventListener( "load", function() {     FastClick.attach( document.body ); }, fa ...

  3. [bzoj2179]FFT快速傅立叶_FFT

    FFT快速傅立叶 bzoj-2179 题目大意:给出两个n位10进制整数x和y,你需要计算x*y. 注释:$1\le n\le 6\times 10^4$. 想法: $FFT$入门题. $FFT$实现 ...

  4. dtrace-conf 2016

    https://www.joyent.com/about/events/2016/dtrace-conf

  5. Chains (链 )

    Indy 中的工作队列系统,调度器,和链引擎都被叫做链. 当使用链的时候,一个基于链的 IOHandler 存储工作项目到有关的工作队列中.在一个工作单元被完成以前,执行这个工作单元的纤程是无法做其它 ...

  6. 【结果发布】第六届SeedCoder编程大赛初赛结果发布

    微软俱乐部科技文化月seedcoder2014编程大赛已经初审完成. 评审小组选出最棒的作品进入决赛(现场答辩+陈述环节,由评委现场打分).终于排名由"初赛分数+现场答辩分"决定. ...

  7. NA交换②

    虚拟局域网VLAN的核心目的:     将一个大的网络划分为小的网络,也称为网络分片(Segementation):一个VLAN对应着一个广播域,最好对应一个网络子网(为VLAN间的路由作准备).   ...

  8. MySQL系列:innodb源代码分析之内存管理

    在innodb中实现了自己的内存池系统和内存堆分配系统,在innodb的内存管理系统中,大致分为三个部分:基础的内存块分配管理.内存伙伴分配器和内存堆分配器.innodb定义和实现内存池的主要目的是提 ...

  9. Android 网络学习之获取server文本文件

    上次我们学习怎样从网络上获取一张图片,今天我们学习怎样从网络上获取文本文件.以XML文件为样例. 由于XML文件在实际开发中最为常见. 我们以以下图片为样例学习怎样从网络上获取XML文件 我们的xml ...

  10. Enterprise Library 5.0 学习笔记

    近期了解了微软提供的企业开发框架Enterprise Library,眼下最新版本号是6.0,可是不支持FW3.5.所以就学习了5.0的版本号,EL5.0支持FW3.5和4.0,官网下载地址是:htt ...