A Simple Problem with Integers

Time Limit: 5000MS

Memory Limit: 131072K

Total Submissions: 90736

Accepted: 28268

Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to dealwith two kinds of operations. One type of operation is to add some given numberto each number in a given interval. The other is to ask for the sum of numbersin a given interval.

Input

The first linecontains 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 queryingthe sum of AaAa+1, ... , Ab.

Output

You need to answerall Q commands in order. One answer in a line.

SampleInput

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

SampleOutput

4

55

9

15

Hint

The sumsmay exceed(超过) the range of 32-bit integers(整数).

Source

POJMonthly--2007.11.25, Yang Yi

注意:数组要用long long

线段树解法

Problem: 3468

User: ksq2013

Memory: 4776K

Time: 2532MS

Language: G++

Result: Accepted

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int n,q;
long long col[400001],sum[400001];
void pushup(int k)
{sum[k]=sum[k<<1]+sum[k<<1|1];}
void pushdown(int k,int m)
{
if(col[k]){
col[k<<1]+=col[k];
col[k<<1|1]+=col[k];
sum[k<<1]+=col[k]*(m-(m>>1));
sum[k<<1|1]+=col[k]*(m>>1);
col[k]=0;
}
}
void build(int s,int t,int k)
{
if(!(s^t)){
scanf("%lld",&sum[k]);
return;
}int m=(s+t)>>1;
build(s,m,k<<1);
build(m+1,t,k<<1|1);
pushup(k);
}
void update(int s,int t,int k,int l,int r,int c)
{
if(l<=s&&t<=r){
col[k]+=c;
sum[k]+=c*(t-s+1);
return;
}pushdown(k,t-s+1);
int m=(s+t)>>1;
if(l<=m)update(s,m,k<<1,l,r,c);
if(m<r)update(m+1,t,k<<1|1,l,r,c);
pushup(k);
}
long long query(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return sum[k];
pushdown(k,t-s+1);
int m=(s+t)>>1;
long long res=0;
if(l<=m)res+=query(s,m,k<<1,l,r);
if(m<r)res+=query(m+1,t,k<<1|1,l,r);
return res;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
for(int i=1;i<=q;i++){
char ak[3];
scanf("%s",ak);
if(ak[0]=='C'){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(1,n,1,a,b,c);
}
else{
int a,b;
scanf("%d%d",&a,&b);
printf("%lld\n",query(1,n,1,a,b));
}
}
return 0;
}

poj 3468的更多相关文章

  1. poj 3264 & poj 3468(线段树)

    poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...

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

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

  3. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  4. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  5. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  6. POJ 3468 线段树区间修改查询(Java,c++实现)

    POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

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

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  8. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

  9. poj 3468(线段树+lazy思想)

    题目链接:http://poj.org/problem?id=3468 思路:如果直接去做,每次都更新到叶子节点,那必然会TLE,我们可以采用lazy的思想:没必要每次更新都更新到叶子节点,只要有一个 ...

  10. POJ 3468 A Simple Problem with Integers(树状数组)

    题目链接:http://poj.org/problem?id=3468 题意:给出一个数列,两种操作:(1)将区间[L,R]的数字统一加上某个值:(2)查询区间[L,R]的数字之和. 思路:数列A,那 ...

随机推荐

  1. 微信小程序管理后台介绍

    微信小程序的管理后台,每次进入都需要扫码,还是特别不爽,现在微信小程序还没正式发布,很多人都还没看到管理后台,这里抢先发布出来 ------------------------------------ ...

  2. equals()方法

    equals()方法是根类Object中的一个方法,子类可以根据需要重写该方法(比如:String类). 一.Object类中的equals()方法实现如下: public boolean equal ...

  3. 小试SQL SERVER 2014 加密备份

            数据库加密: http://blog.csdn.net/u012992506/article/details/25283035   create master key encrypti ...

  4. [转载]50个Demo展示HTML5无穷的魅力

    Flash和HTML5的比较已经成为现在最热门的主题之一,我们不去争论哪个好哪个不好.和HTML5在很酷的动画和简单的游戏等方面一样,除非HTML5在未来几年有一些重大发展,否则Flash在富内容网页 ...

  5. Spring为某个属性注入值或为某个方法的返回值

    项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入. 一.Field值的注入 filed值注入需要使用org.springframework.beans.fa ...

  6. App.Config 和 WebConfig 特殊字符的转义码对应关系

    Web.Config默认编码格式为UTF-8,对于XML文件,要用到实体转义码来替换.对应关系如下: 字符 转义码 & 符号 & & 单引号 ' &apos; 双引号 ...

  7. WPF学习之路(十)实例:用户注册

    通过一个注册用户的实例了解页面间数据的传递 首先构建一个User类  User.cs public class User { private string name; public string Na ...

  8. HttpServletResponse对象 学习

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  9. python urllib2 发起http请求post

    使用urllib2发起post请求 def GetCsspToken(): data = json.dumps({"userName":"wenbin", &q ...

  10. DROP_SNAPSHOT_RANGE过程不能清理表RM$_SNAPSHOT_DETAILS

    今天在测试.验证DROP_SNAPSHOT_RANGE不能彻底快照的过程中遇到了DROP_SNAPSHOT_RANGE无法清理WRM$_SNAPSHOT_DETAILS表中数据的情况,测试服务器版本为 ...