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. jQuery的观察者模式详解 转载

    jQuery的观察者模式详解 投稿:hebedich 本文主要是介绍了jQuery中on方法及trigger方法,以及围绕这个方法来体验的观察者模式,是篇非常不错的文章,对我们理解观察者模式很有帮助. ...

  2. android listVIew实现button按钮监听程序

    1.重写simpleAdapter 方法@Override public HashMap<String,String> getItem(int position) { // TODO Au ...

  3. 杜教筛--51nod1239 欧拉函数之和

    求$\sum_{i=1}^{n}\varphi (i)$,$n\leqslant 1e10$. 这里先把杜教筛的一般套路贴一下: 要求$S(n)=\sum_{i=1}^{n}f(i)$,而现在有一数论 ...

  4. Same Tree (二叉树DFS)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. 选择器的使用(nth-child和nth-last-child选择器)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  6. mybatis xml标签,批量插入

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  7. 【nginx】nginx与apache的优缺点比较

    参考: http://zyan.cc/nginx_php_v6/ nginx相对于apache的优点: 1.轻量级,同样的web 服务,比apache服务器占用更少的内存及资源 2.抗并发,nginx ...

  8. Setting up Storm and Running Your First Topology

    http://www.haroldnguyen.com/blog/2015/01/setting-up-storm-and-running-your-first-topology/ --------- ...

  9. Vmware worksiation中使用ISO

    Vmware技巧: 用ISO安装系统,需要添加2个CD设备. IDE 1  中选择 autoinst.iso IDE 2  中选择 “要安装的系统”.iso 简单讲:Vmware模拟机上需要模拟两次i ...

  10. iOS开发项目实战——Swift实现图片轮播与浏览

    近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...