A Simple Problem with Integers
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的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
		A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ... 
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
		题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ... 
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
		A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ... 
- ACM:  A Simple Problem with Integers  解题报告-线段树
		A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ... 
- poj3468 A Simple Problem with Integers (线段树区间最大值)
		A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ... 
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
		A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ... 
- 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 ... 
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
		A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ... 
- A Simple Problem with Integers(树状数组HDU4267)
		A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ... 
随机推荐
- ADO.net      扩展属性
			扩展属性 处理:有外键关系时将代号化信息处理成原始文字,让用户可看懂的(粗略解释) 利用扩展属性 如:users表中的民族列显示的是民族代号处理成Nation表中的民族名称 需要在users类里面扩展 ... 
- Eclipse中进行Gradle+Jetty部署的web项目的断点调试
			1.自行配置好build.gradle文件和按照gradle的web项目目录结构规范建立java.resourece和webapp文件夹 可在build.gradle文件中自由设定“http端口” 1 ... 
- [原创]java WEB学习笔记54:Struts2学习之路--- 编写Struts2 的第一个程序,HelloWord,简述 package ,action,result
			本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ... 
- 修改linux下mysql目录权限
			1.进入当前目录:例cd /srun3/mysql 2.修改权用户限 chown -R mysql ipt_authd_remote(表示给ipt_authd_remotemysql权限) 3.修改用 ... 
- C语言初学者代码中的常见错误与瑕疵(9)
			题目 字母的个数 现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个. 输入:第一行输入一个正整数T(0<T<25) 随后T ... 
- 使用UEFI BIOS Updater(UBU)来更新CPU微代码
			原文地址:http://www.win-raid.com/t154f16-Tool-Guide-News-quot-UEFI-BIOS-Updater-quot-UBU.html 链接: http:/ ... 
- OpenStack 镜像制作
			Contents [hide] 1 Centos6.5 img制作 1.1 基础环境安装 1.2 下载或从本地上传系统镜像 1.3 启动服务 1.4 建立镜像文件 1.5 通过virt-install ... 
- android 百度地图定位开发2
			先下载了示例代码 进行配置(可查看开发 指南 Hello BaiDuMap) 第一步:创建并配置工程(具体方法参见工程配置部分的介绍): 第二步:在AndroidManifest中添加开发密钥.所需权 ... 
- [转]Delphi多线程编程入门(一)
			最近Ken在比较系统地学习Delphi多线程编程方面的知识,在网络上查阅了很多资料.现在Ken将对这些资料进行整理和修改,以便收藏和分享.内容基本上是复制粘贴,拼拼凑凑,再加上一些修改而来.各个素材的 ... 
- 单例模式在Java和C#中的实现
			单例模式算是最常见和最容易理解一种设计模式了.通常是指某一个类只有一实例存在,存在的空间我认为可以理解为该类所在的应用系统内,还有一种是在某一个容器内单一存在,比如像spring的IOC容器(作用域为 ... 
