A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 55273   Accepted: 16628
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

解题思路:

某个区间内的数都加上同一个数,成段更新。

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=100005;
long long sum[maxn<<2];
long long add[maxn<<2];
int n,k;
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
if(add[rt])
{
//add[rt<<1]=add[rt<<1|1]=add[rt];//不能写成这样
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*(m - (m>>1));
sum[rt<<1|1]+=add[rt]*(m>>1);
add[rt]=0;
}
}
void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
add[rt]+=c;
sum[rt]+=(long long)c*(r-l+1);
return;
}
PushDown(rt,r-l+1);
int m=(r+l)>>1;
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,rson);
PushUp(rt);
}
long long query(int L,int R,int l,int r,int rt)//注意long long类型
{
if(L<=l&&r<=R)
{
return sum[rt];
}
PushDown(rt,r-l+1);//查询的时候要向下更新。懒惰标记
int m=(l+r)>>1;
long long ans=0;//注意long long
if(L<=m)
ans+=query(L,R,lson);
if(R>m)
ans+=query(L,R,rson);
return ans;
}
int main()
{
scanf("%d%d",&n,&k);
build(1,n,1);
char c;int a,b,d;
while(k--)
{
scanf("%s",&c);
if(c=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else
{
scanf("%d%d%d",&a,&b,&d);
update(a,b,d,1,n,1);
}
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

[ACM] poj 3468 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 (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  3. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  4. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  5. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  6. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  7. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  8. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

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

  9. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

随机推荐

  1. RFC2889转发性能測试用例设计和自己主动化脚本实现

    一.203_TC_FrameRate-1.tcl set chassisAddr 10.132.238.190 set islot 1 set portList {9 10} ;#端口的排列顺序是po ...

  2. hdu5176(并查集)

    传送门:The Experience of Love 题意:一个叫Gorwin的女孩和一个叫Vivin的男孩是一对情侣.他们来到一个叫爱情的国家,这个国家由N个城市组成而且只有N−1条小道(像一棵树) ...

  3. set echo on/off,set term on/off,set feedback off,set heading off命令(转)

    1.term命令:  当和SPOOL命令联合使用时,可以取消SQLPLUS输出,查询结果仅仅存在于假脱机文件中  set term on:查询结果既显示于假脱机文件中,又在SQLPLUS中显示:  s ...

  4. 不可不知的DIP、IoC、DI以及IoC容器

    面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.当中.OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念. 本文首先用实例阐述四个概 ...

  5. php 汉字转拼音 [包含20902个基本汉字+5059生僻字]

    原文:php 汉字转拼音 [包含20902个基本汉字+5059生僻字] 昨天在转换拼音的时候发现个bug,有好多字都无法转换,不过也不能怪他,毕竟人家的库才8k,应该只有常用的.无奈上网找了下,发现一 ...

  6. 什么样的企业造什么样的软件最easy成功?

    事件1: 一般软件企业按功能分,大体分业务应用型软件和系统工具型软件. 按市场分,应用型软件企业较多,直接贴近生活:系统工具类较少,间接贴近大众较少. 事件2: 软件企业中,当中中小型企业老板存在非常 ...

  7. WPF界面设计技巧(1)—不规则窗体图文指南

    原文:WPF界面设计技巧(1)-不规则窗体图文指南 初到园子,奉上第一篇入门级教程,请勿见笑. 以往WinForm编程中,实现不规则窗体是有一定难度的,更难的是不规则窗体的边缘抗锯齿及局部透明处理.而 ...

  8. 再探vim经常使用命令

     最開始学习过vim,见 http://blog.csdn.net/u011848617/article/details/12837873 之后以前不了了之,当再次学习后,发现经常使用命令的掌握还 ...

  9. HDU 1253-大逃亡(裸-DBFS)

    G - 胜利大逃亡 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  10. openwrt教程 第一章 物联网&amp;openwrt开发概述

    1.1 我们的宗旨 互联网.移动互联网的时代已经过去,物联网的时代已经来临!2014年,是物联网元年,2016年,物联网将达到高潮!为了迎接该潮流,我们工作室(F403科技创意室:http://f40 ...