线段树 区间更新 访问POJ3468 A Simple Problem with Integers解题报告
来源:互联网 发布:max优化命令英文 编辑:程序博客网 时间:2024/05/17 06:54
A Simple Problem with Integers
Time Limit: 5000MS
Memory Limit: 131072K
Total Submissions: 78802
Accepted: 24292
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MID(x,y) ( (x+y)>>1 )
#define L(x) ( x<<1 )
#define R(x) ( x<<1|1 )
#define MAXN 100005
struct {
intleft,right;
__int64 sum; //该区间元素的总和
__int64 delta; //该区间每个元素的总增量
}segTree[3*MAXN];
int num[MAXN],N,Q;
void lazy(int father)
{//下放过程,lazy步骤,更新左右儿子
if (segTree[father].delta )
{
segTree[L(father)].sum += ( segTree[L(father)].right -segTree[L(father)].left + 1 )*segTree[father].delta;
segTree[R(father)].sum += ( segTree[R(father)].right -segTree[R(father)].left + 1 )*segTree[father].delta;
segTree[L(father)].delta += segTree[father].delta;
segTree[R(father)].delta += segTree[father].delta;
segTree[father].delta = 0;
}
}
void Build(int father,int left,intright) //口诀:赋值,判断(叶子),递归,(回溯)更新
{
segTree[father].left = left;segTree[father].right = right;//赋值
if (left == right ) //判断
{
segTree[father].sum = num[left]; return;
}
intmid = MID(left,right);//递归
Build(L(father),left,mid);
Build(R(father),mid+1,right);
segTree[father].sum = segTree[L(father)].sum +segTree[R(father)].sum; //建完子树对根节点进行更新
}
void Add(int father,int left,int right,__int64numAdd)
{
if (segTree[father].left == left && segTree[father].right == right ) //下放终止条件
{
segTree[father].sum += numAdd*( segTree[father].right -segTree[father].left + 1 );
segTree[father].delta += numAdd;//Lazy关键一步,暂时存储增量而不是更新儿子。下次用到该节点的儿子时再更新儿子
return;
}
lazy(father); //下降一层
intmid = MID(segTree[father].left,segTree[father].right);
if (right <= mid ) Add(L(father),left,right,numAdd);
elseif ( mid < left ) Add(R(father),left,right,numAdd);
else
{
Add(L(father),left,mid,numAdd);
Add(R(father),mid+1,right,numAdd);
}
//前面递归将father的左右儿子节点加了之后,回溯计算父亲节点应该加上多少
segTree[father].sum = segTree[L(father)].sum +segTree[R(father)].sum;
}
__int64 query(int father,int left,intright)
{
if (segTree[father].left == left && segTree[father].right == right )
return segTree[father].sum;
lazy(father);
intmid = MID(segTree[father].left,segTree[father].right);
if (right <= mid )
return query(L(father),left,right);
elseif ( mid < left ) returnquery(R(father),left,right);
else return query(L(father),left,mid) + query(R(father),mid+1,right);
}
int main()
{
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
inti,from,to;
__int64 ans,numAdd;
charcmd;
scanf("%d%d",&N,&Q);
for(i=1; i<=N; i++) scanf("%d",&num[i]);
Build(1,1,N);
while ( Q-- )
{
scanf("%1s",&cmd);
if( cmd == 'C' )
{
scanf("%d%d%I64d",&from,&to,&numAdd);
Add(1,from,to,numAdd);
}
else
{
scanf("%d%d",&from,&to);
ans= query(1,from,to);
printf("%I64d\n",ans);
}
}
return 0;
}
- 线段树 区间更新 访问POJ3468 A Simple Problem with Integers解题报告
- poj3468- A Simple Problem with Integers-解题报告-线段树
- poj3468 A Simple Problem with Integers 线段树区间更新
- Poj3468 A Simple Problem with Integers 线段树、区间更新
- 【poj3468-A Simple Problem with Integers】-线段树区间更新
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和
- 线段树 区间更新 区间查询 poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers 线段树延迟标记区间更新区间求和
- poj3468 A Simple Problem with Integers 线段树区间更新,区间求和
- poj3468 A Simple Problem with Integers(线段树,区间更新,区间和查询)
- poj3468 A Simple Problem with Integers 线段树 延时更新
- poj3468 A Simple Problem with Integers 线段树 更新延迟
- POJ3468 - A Simple Problem with Integers (线段树 区间更新)
- POJ3468 A Simple Problem with Integers(线段树 lazy思想 区间查询更新)
- poj3468——A Simple Problem with Integers(线段树,区间更新)
- poj3468 A Simple Problem with Integers 线段树 区间更新 lazy
- POJ3468 A Simple Problem with Integers(线段树区间更新,lazy标记)
- POJ3468 A Simple Problem with Integers (线段树区间更新)
- j2ee学习方法摘要
- 线性回归的java实现
- tesseract-ocr ubuntu 安装使用
- 【峰回路转】Excel技巧百例 10.数据透视表的简单使用-分类汇总
- startActivityForResult和setResult详解
- 线段树 区间更新 访问POJ3468 A Simple Problem with Integers解题报告
- 分析json、xml的区别?json、xml解析方式的底层是如何处理的?
- UGUI处理ScrollRect的滑动被OnDrag函数拦截的一点技巧
- jsp开发环境的搭建
- 输入有符号整型数组,去掉重复值后,按照升序输出整型数组
- Ascii表中字符的个数
- Hibernate面试题+答案
- Android应用如何开机自启动、自启动失败的原因
- MyEclipse6.5下struts2+spring2+hibernate3 整合