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. Eclipse CDT Linux下内存分析 实战历险

    C++产品开发,上线集成时,都需要内存泄露.覆盖率等检测,这些在Windows下都有很好的工具,如 Visual Studio: 这个内置了很多的工具 Devpartner: VC6时BoundChe ...

  2. Quartz2D复习(一)--- 基础知识 / 绘制线段圆弧 / 图片水印 / 截图

    1.Quartz 2D是一个二维绘图引擎,同时支持ios和Mac系统: Quart2D的API是纯C语言的,API来自于Core  Graphics框架: 2.Quartz 2D可以绘制图形(线段/三 ...

  3. CoreData数据库迁移的操作

    CoreData数据库迁移操作步骤,操作是基于Xcode7. 1.添加新的数据库.选中当前数据库版本:Editor->Add Model Verson,创建一个新的数据库版本. 2.Comman ...

  4. C#复习②

    C#复习② 2016年6月15日 09:08 1.C#之Symbols Identifier = (letter|'_'|'@'){letter|digit|'_'}. 需要注意: 1.Unicode ...

  5. HTML基础(二)——表单,图片热点,网页划区和拼接

    一.表单 <form id="" name="" method="post/get" action="负责处理的服务端&qu ...

  6. linux下文件的特殊权限s和t

    先看看这两个文件的权限:[root@localhost ~]# ls -ld /usr/bin/passwd /tmpdrwxrwxrwt 4 root root 4096 Jun 2 17:33 / ...

  7. Qt使用自带的windeployqt 生成exe来发布软件

    集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application  和 Qt Quick Application.下面分别介绍这两类exe ...

  8. linux开机自动连接无线网络

           1.右击无线网络图标的“编辑连接”. 2.在“无线”选项卡里,选择“编辑”. 3.在“无线安全性”选项卡里,输入无线密匙,并选中左下角的“对所有用户可      用”的选项点击应用,会提 ...

  9. 【转】在Android布局中使用include和merge标签

    内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...

  10. WCF自定义Header

    MiscWebSrvcInfClient client = new MiscWebSrvcInfClient("MiscWSBeanPort", ConfigurationMana ...