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. 根据werservice代码用CXF生成WSDL

    原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...

  2. android键盘弹出头部上移处理

    <ScrollView android:id="@+id/top_bar" android:layout_width="fill_parent" andr ...

  3. Android闹钟 AlarmManager的使用

    Android闹钟 AlarmManager的使用 AlarmManager介绍 AlarmManager这个类提供对系统闹钟服务的访问接口. 你可以为你的应用设定一个在未来某个时间唤醒的功能. 当闹 ...

  4. asp.net实现动态添加table行

    asp.net动态的生成,删除table的行,主要是在后台动态创建单元行,单元表格,效果图: 2.代码: <%@ Page Language="C#" AutoEventWi ...

  5. 我的Android第三章:Android的组件介绍

    小编摘录了Android文档介绍Android四大组件的基本内容,感觉文档的内容写的很详细所以小编将它写入了博客 Android 使用Java语言开发.Android SDK 工具编译代码-以及任意数 ...

  6. 【代码笔记】iOS-拍照动画

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  7. new ActiveXObject("Scripting.FileSystemObject") 时抛出异常 .

    使用JScript读写本地文件时,会使用Scripting.FileSystemObject控件. IE默认是不允许运行这类“未标记为安全执行脚本的ActiveX控件”的. 因此执行下行代码时: fs ...

  8. MVC权限控制

    基本方法是重写AuthorizeAttribute类的AuthorizeCore方法 protected override bool AuthorizeCore(HttpContextBase htt ...

  9. Git笔记1

    1.简介 1.1 GIT(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非 ...

  10. selenium+python测试

    pip install selenium, 得有图形界面, 这里简单的先演示一个打开浏览器,输入网址的demo,以百度为例 # encoding = utf-8 from selenium impor ...