POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l ,m , rt << 1
#define rson m + 1 , r ,rt << 1 | 1
typedef __int64 LL;
const int maxn = 100005;
LL sum[maxn<<2],lazy[maxn<<2];
void PushUp(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
if (lazy[rt])
{
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
sum[rt<<1] += (m - (m>>1)) * lazy[rt];
sum[rt<<1|1] += (m>>1) * lazy[rt];
lazy[rt] = 0;
}
}
void build(int l,int r,int rt)
{
if (l == r)
{
scanf("%I64d",&sum[rt]);
lazy[rt] = 0;
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void upd(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
sum[rt] += (LL)(r - l + 1) * c;
lazy[rt] += c;
return;
}
PushDown(rt,r - l + 1);
int m = (l + r) >> 1;
if (L <= m) upd(L,R,c,lson);
if (R > m) upd(L,R,c,rson);
PushUp(rt);
}
LL qry(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
PushDown(rt,r - l + 1);
int m = (l + r) >> 1;
LL ret = 0;
if (L <= m) ret += qry(L,R,lson);
if (R > m) ret += qry(L,R,rson);
return ret;
}
int main()
{
int N,Q,a,b,c;
char opt;
while (~scanf("%d%d",&N,&Q))
{
build(1,N,1);
while (Q--)
{
getchar();
scanf("%c",&opt);
if (opt == 'C')
{
scanf("%d%d%d",&a,&b,&c);
upd(a,b,c,1,N,1);
}
else
{
scanf("%d%d",&a,&b);
printf("%I64d\n",qry(a,b,1,N,1));
}
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers(线段树/区间更新)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- 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 ...
- 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 ...
随机推荐
- POJO, DTO, VO, JavaBean的区别
POJO(plain ordinary java object 简单java对象) POJO是一个简单的普通的Java对象,它不包含业务逻辑或持久逻辑等,但不是JavaBean.EntityBean等 ...
- Google Zxing 二维码生成与解析
生成二维码的开源项目可谓是琳琅满目,SwetakeQRCode.BarCode4j.Zxing...... 前端有JQuery-qrcode,同样能实现生成二维码. 选择Zxing的原因可能是对 Go ...
- lecture15-自动编码器、语义哈希、图像检索
Hinton第15课,本节有课外读物<Semantic Hashing>和<Using Very Deep Autoencoders for Content-Based Image ...
- PHP Date ( I need to use)
本文记录项目中用到的 PHP Date 相关,备忘. 日期格式约定为 xx-xx-xx 格式(字符串),例如 2016-03-09. xx-xx-xx -> 时间戳 $date = " ...
- 自己画WinForm 皮肤包括默认控件
好久没来博客园,今天捣鼓到现在就是为了把之前的皮肤控件完善好, 之前也看了很多技术文章,大多数都是自己重写系统控件实现换肤,几乎没有像东日的(IrisSkin)控件一样 添加一个组件 把系统的皮肤全换 ...
- 【抄】更改eclipse配置
序言:网上流传着eclipse更改代码提示的一种方法,在eclipse IDE可视化界面没法更改一些东西,比如content assist auto activition trigger在js里面只允 ...
- 1018LINUX中crontab的用法
转自http://blog.csdn.net/ethanzhao/article/details/4406017#comments 基本格式 :* * * * * command分 时 日 月 周 命 ...
- 网页之间信息传递方式(Cookie,Session)
1.使用header()函数的重定向方式实现网页跳转. EXE:header("Location: http://www.example.com/"); 2.URL的GET ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- Okio 1.9简单入门
Okio 1.9简单入门 Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问.存储和处理你的数据.而OkHttp的底层也使用该库作为支持. 该库极 ...