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. Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)

    控制台程序. Arrays类中的binarySearch()静态方法使用二叉树搜索算法,在有序数组中查找包含给定值的元素.只有当数组的元素按升序方式排序时,该方法才是最有效的,否则就应在调用binar ...

  2. css less

    LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编写和维护. LESSCSS可以在多种语 ...

  3. PostgreSQL Replication之第十五章 与Walbouncer 一起工作

    与Walbouncer 一起工作 在本书的最后一章,将引导您通向2014年发布的一个工具,称为walbouncer.本书中的大多数技巧说明了如何复制整个数据库实例,如何分片,等等.在最后一章,是关于w ...

  4. TImageList 和 TlistView 组件(C++Builder)

    __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { //加载图标到Imagelist Graphics::TBitmap *bm ...

  5. nyist 593 Take it easy

    http://acm.nyist.net/JudgeOnline/problem.php?pid=593 Take it easy 时间限制:1000 ms  |  内存限制:65535 KB 难度: ...

  6. myeclipse项目里有红色感叹号

    myeclipse项目里有红色感叹号 这种情况是因为 .classpath 文件里面配置引用了某个jar,但是实际上你的 lib 里面并没有这个jar 所以才会有红色的提示. 不用拿.classpat ...

  7. URAL 1416 Confidential(次小生成树)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416 Zaphod Beeblebrox — President of the Impe ...

  8. 变形--原点 transform-origin

    任何一个元素都有一个中心点,默认情况之下,其中心点是居于元素X轴和Y轴的50%处.如下图所示: 在没有重置transform-origin改变元素原点位置的情况下,CSS变形进行的旋转.位移.缩放,扭 ...

  9. struts_22_xwork校验器列表使用说明

    系统提供的校验器列表如下: required (必填校验器,要求field的值不能为null) requiredstring (必填字符串校验器,要求field的值不能为null,并且长度大于0,默认 ...

  10. [javascript] ajaxfileupload.js 跨域上传文件

    原文地址: http://www.ueffort.com/jqueryajaxfileupload-js-duo-wen-jian-shang-chuan-chuan-zhi-kua-yu/ 跨域 这 ...