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 ...
随机推荐
- 几种任务调度的 Java 实现方法与比较
综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quartz 以及 JCron Tab,并对其优缺 ...
- c++ 静态持续变量
c++为静态存储持续性变量提供了3种链接性: 外部链接性(可在其他文件中访问) 内部链接性(只能在当前文件中访问) 无链接性(别有用心能在当前函数或代码中访问) 如果没有显示的初始化静态变量会把它设置 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- <实训|第五天>通过搭建NFS,FTP实现共享文件附Vim脚本游戏
先说个事情:我周末是不更新这个系列教程的,不过其他内容的会更新,我周末就整理这一周的各种内容到我的微信公众号中,提供给大家! 期待已久的linux运维.oracle"培训班"终于开 ...
- des解密不完整,前面几位是乱码的解决办法
在工作中遇到的Des解密问题,第三方发来的数据需要我们进行des解密,但是解密的结果前几位始终是乱码.废了半天劲,终于找到了问题所在. 下面先介绍一下des,了解des的同学可以直接看下面的解决办法. ...
- 正式版/免费版 Xamarin 体验与拥抱
感谢MS, 感谢老纳.终于把 Xamarin 这个磨人的小妖精给收了,在也不用向大神要破解补丁了, 终于可以光明正大的使用了!! 跟据实践, 如果你们想体验一下 .NET 开发 IOS /Androi ...
- C# 利用反射动态将字符串转换成属性对应的类型值
/// <summary> /// 为指定对象分配参数 /// </summary> /// <typeparam name="T">对象类型& ...
- SVN 修改log信息报错的解决方案
要实现允许修改log这个功能,只需要在hooks目录下增加一个名为:pre-revprop-change.bat的文件,重启svn即可.该文件内容为:------------------------- ...
- canvas三角函数应用
这个是圆圈旋转的简单案例 var canvas=document.getElementById("canvas"); var cxt=canvas.getContext(" ...
- Qt5.3.0 for Android开发环境配置
1.去官网下载Qt5.3.0 for Android 2.去http://developer.android.com下载Ndk 和SDk 3.去http://ant.apache ...