A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 59798   Accepted: 18237
Case Time Limit: 2000MS

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.

Source


【题目大意】

一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。

【题目分析】

裸的区间更新线段树。

线段树单点更新和区间更新的区别:
1.每个结点中多了一个add值,代表该结点以下的结点需要增加的值;
2.build函数中,如果在建树的过程中就赋值给num,那么在建完树之后不要忘记pushup,因为此时只是叶子结点有值,上面的值都为空;这个在区间更新中很常用,因为区间更新中如果输入一个值,然后更新一个值,这样会很麻烦,会耗费更多的时间;
3.update函数中,区间更新多了一个upshdown函数,并且更新sum和add值的判断条件是树中结点的l~r和要更新的区间的l~r相等,此时sum加的值是整个区间的长度*要更新的值,然后add值记录后面每个结点需要加上的值,即:c;
4.upshdown函数最后不要忘了将延时标记add清零;


//Memory   Time
// K MS
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 110000
#define LL long long
using namespace std;
LL n,m;
LL ans;
struct Tree
{
LL l,r;
LL sum,add;
};
Tree tree[MAX*3]; void pushup(LL x)
{
LL tmp=2*x;
tree[x].sum=tree[tmp].sum+tree[tmp+1].sum;
} void pushdown(LL x)
{
LL tmp=2*x;
tree[tmp].add+=tree[x].add;
tree[tmp+1].add+=tree[x].add;
tree[tmp].sum+=tree[x].add*(tree[tmp].r-tree[tmp].l+1);
tree[tmp+1].sum+=tree[x].add*(tree[tmp+1].r-tree[tmp+1].l+1);
tree[x].add=0;
} void build(int l,int r,int x)
{
tree[x].l=l;
tree[x].r=r;
tree[x].add=0;
if(l==r)
{
scanf("%lld",&tree[x].sum);
return ;
}
int tmp=x<<1;
int mid=(l+r)>>1;
build(l,mid,tmp);
build(mid+1,r,tmp+1);
pushup(x); //如果在建树的过程中给sum赋值,记得后面要pushup
} void update(LL l,LL r,LL c,LL x)
{
if(r<tree[x].l||l>tree[x].r)
return ;
if(l<=tree[x].l&&r>=tree[x].r)
{
tree[x].add+=c;
tree[x].sum+=c*(tree[x].r-tree[x].l+1);
return ;
}
if(tree[x].add)
pushdown(x);
LL tmp=x<<1;
update(l,r,c,tmp); // !!!
update(l,r,c,tmp+1);
pushup(x);
} void query(LL l,LL r,LL x)
{
if(r<tree[x].l||l>tree[x].r) //要更新的区间不在该区间上
return ;
if(l<=tree[x].l&&r>=tree[x].r) //要更新区间包括了该区间
{
ans+=tree[x].sum;
return ;
}
if(tree[x].add)
pushdown(x);
LL tmp=x<<1;
LL mid=(tree[x].l+tree[x].r)>>1;
if(r<=mid)
query(l,r,tmp);
else if(l>mid)
query(l,r,tmp+1);
else
{
query(l,mid,tmp);
query(mid+1,r,tmp+1);
}
// pushup(x);
} int main()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
while(~scanf("%lld %lld",&n,&m))
{
build(1,n,1);
char str[5];
while(m--)
{
scanf("%s",str);
if(str[0]=='Q')
{
LL l,r;
scanf("%lld %lld",&l,&r);
ans=0;
query(l,r,1);
printf("%lld\n",ans);
}
else
{
LL l,r,c;
scanf("%lld %lld %lld",&l,&r,&c);
update(l,r,c,1);
}
}
}
return 0;
}

  

线段树 + 区间更新 + 模板 ---- poj 3468的更多相关文章

  1. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

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

  2. A Simple Problem with Integers(线段树区间更新模板)

    最基本的线段树的区间更新及查询和 用tag(lazy)数组来“延缓”更新,查询或添加操作必须进行pushdown操作,即把tag从p传到lp和rp并清楚tag[p],既然得往lp和rp递归,那么就可以 ...

  3. FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

    http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...

  4. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  5. CF#52 C Circular RMQ (线段树区间更新)

    Description You are given circular array a0, a1, ..., an - 1. There are two types of operations with ...

  6. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

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

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

随机推荐

  1. 用友iuap云运维平台支持基于K8s的微服务架构

    什么是微服务架构? 微服务(MicroServices)架构是当前互联网业界的一个技术热点,业内各公司也都纷纷开展微服务化体系建设.微服务架构的本质,是用一些功能比较明确.业务比较精练的服务去解决更大 ...

  2. 关于的 recorder robotium 的Eclipse插件(URL:http://recorder.robotium.com/updates/或者说不可用)

    最近在学robotium.看到别人说robotium的Eclipse的插件非常好用. 打算安装时.发现死活都无法连接http://recorder.robotium.com/updates/ 过程是  ...

  3. 互斥锁pthread_mutex_init()函数

    linux下为了多线程同步,通常用到锁的概念.posix下抽象了一个锁类型的结构:ptread_mutex_t.通过对该结构的操作,来判断资源是否可以访问.顾名思义,加锁(lock)后,别人就无法打开 ...

  4. Binlog中最容易踩到的坑

    MySQL高可用架构中,主库复制是非常常见的一种. 当主库宕机后,可以提升一个从库作为新的主库,保证服务可用性:同时可以通过扩展从库,提高整个集群的QPS. 在主从复制架构下,MySQL通过binlo ...

  5. android:3D垂直翻转动画-FlipAnimation

    需求 对ImageView进行相似于翻纸牌的动画 解决 各种Animator的组合 第一步动画: 动画代码文件1,card_flip_left_out.xml <? xml version=&q ...

  6. 关于CAE的那点儿破事儿【二】

    前面在<关于CAE的那点破事儿>一文中,主要提到了CAE是什么.CAE能做些什么.人在CAE应用中的作用以及CAE从业中应当具有哪些基本素质.然而CAE是一把双刃剑,如果不能在工程应用中很 ...

  7. 利用babel-cli搭建支持ES6的node环境

    现在ES6盛行,开始大量使用ES6的特性敲代码,但限于Node.js本身对ES6的特性支持的不够完备,那么需要借助于其他工具来完成. 基本上,现在都直接写ES6的代码,然后使用babel-cli提供的 ...

  8. Unity编辑器下,界面替换NGUI字体以及字号

    项目中有需要批量替换字体以及字号的需求,一般也就是多语言处理吧. 提供界面如下: 手机拍图,就这样凑合看吧.但是代码不打折. 紧急避让,我只提供修改UILabel以及UIPopupList 下的字体, ...

  9. jquery的extend函数

    var extend = (function () { var isObjFunc = function (name) {//返回的是一个函数 var toString = Object.protot ...

  10. FIDDLER的使用方法及技巧总结(连载一)FIDDLER快速入门及使用场景

    FIDDLER的使用方法及技巧总结 一.FIDDLER快速入门及使用场景 Fiddler的官方网站:http://www.fiddler2.com Fiddler的官方帮助:http://docs.t ...