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. Shiro--权限控制

    来自: http://m.oschina.net/blog/92003 1.applicationContext-shiro.xml配置:实现认证和授权 <!-- shiro start --& ...

  2. android ANR问题

    一.什么是ANR ANR:Application Not Responding: 具体请参考:http://blog.csdn.net/dadoneo/article/details/8270107

  3. ArrayList和LinkList区别

    ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除

  4. Android 利用Service实现下载网络图片至sdk卡

    package com.example.myapp5; import android.app.Activity; import android.content.Intent; import andro ...

  5. DDR(一)

    P-Bank:计算机早期的一个概念.目的:匹配内存芯片和CPU芯片的数据总线的宽度.方法:并联多个内存模块. L-Bank:对内部存储阵列的分割,避免寻址冲突,提高内存效率.通过ba信号选择bank, ...

  6. 《zw版·Halcon-delphi系列原创教程》 水果自动分类脚本(机器学习、人工智能)

    <zw版·Halcon-delphi系列原创教程> 水果自动分类脚本(机器学习.人工智能) 前面介绍了超市,流水线,酸奶的自动分类算法,下面再介绍一个水果的自动分类算法. Halcon强大 ...

  7. 对DotNet分布式应用搭建的考虑

    设计前的考虑和准备工作 1 对业务需求的理解重要性远远胜于对技术架构的理解 2 架构包含技术架构和业务架构 3 没有万能和通用的架构,只有符合自身业务需求的架构 4 架构本身的复杂性要截至在架构设计阶 ...

  8. iOS 学习笔记 三 (2015.03.05)

    服务和特征都是用UUID来唯一标识的,UUID的概念如果不清楚请自行google,国际蓝牙组织为一些很典型的设备(比如测量心跳和血压的设备)规定了标准的service UUID(特征的UUID比较多, ...

  9. Linux之sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  10. linux下异步IO的简单例子【转】

    转自:http://blog.chinaunix.net/uid-24567872-id-87677.html 首先,贴一下异步IO中用的的一些结构体,因为平常很少用,整理起来方便查看. aio.h中 ...