A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K

Total Submissions: 77964 Accepted: 24012

Case Time Limit: 2000MS

Description

You have N integers, A1, A2, … , 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 A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.

“Q a b” means querying the sum of Aa, Aa+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

POJ Monthly–2007.11.25, Yang Yi

线段树的区间查询与区间更新,lazy优化,不然会超时

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
const int MAX = 110000;
struct node
{
LL lazy;
LL sum;
} Tree[MAX*12];
LL a[MAX];
void Build(int L,int R,int site)
{
if(L==R)
{
Tree[site].lazy=0;
Tree[site].sum=a[L];
return ;
}
Tree[site].lazy=0;
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
void update(int L,int R,int l,int r,int site,LL w)
{
if(L==l&&r==R)
{
if(L==R)
Tree[site].lazy=0;
else
Tree[site].lazy+=w;
Tree[site].sum+=((R-L+1)*w);
return ;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
}
Tree[site].sum+=((r-l+1)*w);
if(mid>=r)
{
update(L,mid,l,r,site<<1,w);
}
else if(l>mid)
{
update(mid+1,R,l,r,site<<1|1,w);
}
else
{
update(L,mid,l,mid,site<<1,w);
update(mid+1,R,mid+1,r,site<<1|1,w);
}
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
LL Query(int L,int R,int l,int r,int site)
{
if(L==l&&r==R)
{
return Tree[site].sum;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
} if(mid>=r)
{
return Query(L,mid,l,r,site<<1);
}
else if(mid<l)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int n;
int Q;
char s[5];
int u,v;
LL w;
while(~scanf("%d %d",&n,&Q))
{
for(int i=1; i<=n; i++)
{
scanf("%I64d",&a[i]);
}
Build(1,n,1);
while(Q--)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d %d",&u,&v);
printf("%I64d\n",Query(1,n,u,v,1));
}
else
{
scanf("%d %d %I64d",&u,&v,&w);
update(1,n,u,v,1,w);
}
}
}
return 0;
}

A Simple Problem with Integers的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

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

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

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

随机推荐

  1. scala学习笔记(1)

    下载和安装Scala 前往http://www.scala-lang.org/downloads下载Scala在各个平台的安装包,安装后即可在运行scala编译器和交互式命令行环境(interacti ...

  2. R语言putty中直接使用X11(Xming)绘图

    1.下载Xming地址 http://pan.baidu.com/s/1o6ilisU,安装,推荐默认安装在C盘,推荐快捷方式放在与putty快捷方式同一个文件夹: 2.打开putty,在SSH的X1 ...

  3. PostgreSQL9.2.4内核源码结构介绍

    PostgreSQL的源代码可以随意获得,其开源协议也允许研究者任意修改,这里介绍一下PostgreSQL的源码结构以及部分实现机制.下载PostgreSQL源代码并减压后,其一级目录结构如下图: P ...

  4. 、JAVA-异常

    异常 1.种类(error 系统异常,无法处理)(exception 程序异常,可以处理) 1.算数异常 2.空指针异常 原因:对象没有实例化就调用他的实例方法,会造成空指针异常 2.常见异常 1.R ...

  5. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  6. SQL查询 练习题

    设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...

  7. C++之路起航——标准模板库(list)

    list(链表):http://baike.baidu.com/link?url=gkVdBlHEzy6ssrgT5Iy2wze4jl37ka1G45TRpUHrQSYFZQg2HimtUCePV0t ...

  8. yii框架中应用jquery表单验证插件

    效果图: 视图层: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  9. sql xpath 查找包含

    select xcontent.query('/root//*[contains(text()[1], ''中'')]'), column1 from table

  10. 夺命雷公狗TP下关联查询

    记录下我们常用的关联查询: public function add4(){ $id=$_GET['id']; $this->list = M("student")->t ...