线段树 区间更新 访问POJ3468 A Simple Problem with Integers解题报告
2015-08-28 20:21
441 查看
A Simple Problem with Integers
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 <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;
}
相关文章推荐
- 线段树题集
- hdu1754
- HDU1394
- 敌兵布阵 (1)
- I Hate It (1)
- LCIS (2)
- A Simple Problem with Integers (2)
- Mayor's posters (3)
- Buy Tickets (3)
- 线段树
- UVA - 12532 Interval Product
- POJ 3264 Balanced Lineup
- hdu 1542 求矩形并的面积
- 关于数据结构之线段树
- poj 3225 关于集合运算
- poj 2352
- hdu1166敌兵布阵(线段树点修改)
- POJ 2352 Stars 线段树 pascal
- hdu 1698 Just A Hook 线段树的一道题
- HDU 1754 I Hate It