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 ...
随机推荐
- nodejs实现Websocket的数据接收发送
在去年的时候,写过一篇关于websocket的博文:http://www.cnblogs.com/axes/p/3586132.html ,里面主要是借助了nodejs-websocket这个插件,后 ...
- .Net分布式异常报警系统-简介
系统简介 分布式异常报警系统就是收集系统运行过程中产生的未处理异常,检查系统运行的状态,并将异常信息统一发送到服务端,由服务端将信息通知到相关的责任人. 问题 我们在项目开发中可能遇到以下几个问题: ...
- ModernUI教程:处理内容导航事件
Modern UI包含了一个机遇uri的内容导航框架用来处理区域加载.卸载和处理访问记录页面间的导航. 如果你希望你的页面具有这些处理事件,你需要继承一个IContent接口,该接口位于FirstFl ...
- 遍历Arraylist的方法:
遍历Arraylist的几种方法: Iterator it1 = list.iterator(); while(it1.hasNext()){ System.out ...
- UITextView的使用详解
//初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...
- C++11的default和delete关键字
C11的新特性实在是太多了,这2个关键字关注的人倒是少了很多,其中有一个原因便是编译器支持得太慢了(VS到VS2013才支持上),不过这2个关键字那真是极为有用的,下面我们来看看. [default关 ...
- canvas拖动
var canvas=document.getElementById("canvas"); var cxt=canvas.getContext("2d"); v ...
- Eclipse+Maven创建webapp项目<一>
Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...
- 折叠ListView
转自 http://blog.csdn.net/hnyzwtf/article/details/50487228 1 activity_main.xml <?xml version=" ...
- [转]html js中name和id的区别和使用分析
js中web页面元素的调用可以有两种识别方法:id和name 自己在用的过程中总结一下id和name的使用区别. 一,使用范围 除 BASE, HEAD, HTML, META, SCRIPT, ST ...