poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 Accepted: 23286 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 4Sample Output
4
55
9
15Hint
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>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std; //lazy思想的线段树区间更新,区间查询,感觉不难 const int MAX = ;
ll num[MAX]; struct nodes
{
int left,right;
ll large,add;
} tree[MAX*]; void pushup(int root)//代替递归向上更新
{
tree[root].large = tree[root*].large + tree[root*+].large;
}
void pushdown(int root)//递归向下更新
{
if(tree[root].add)
{
tree[root*].add += tree[root].add;
tree[root*+].add += tree[root].add;
tree[root*].large += tree[root].add * (tree[root*].right - tree[root*].left + );
tree[root*+].large += tree[root].add * (tree[root*+].right - tree[root*+].left + );
tree[root].add = ;
}
}
void build(int root,int left,int right)//建树过程,也是初始化更新过程
{
tree[root].left = left;
tree[root].right = right;
tree[root].add = ;//不能忘记这个值的初始化为0
if(left == right)
{
tree[root].large = num[left];
return;//注意这里的递归结束条件
} int mid = (left+right)/; build(*root,left,mid);
build(*root+,mid+,right); pushup(root);//该子区间建立好后,该区间更新
} void update(int root,int left,int right,ll val)
{
if(left == tree[root].left && right == tree[root].right) // 刚好进入到所需区间,就可以更新到当前区间直接返回
{
tree[root].add += val; // 用当前节点的add变量记录区间每个元素的累加量
tree[root].large += val * (right - left + );//更新当前区间和
return;
}
pushdown(root); //否则向下更新一层,继续找合适区间
int mid = (tree[root].left+tree[root].right)/;
if(left <= mid && right > mid)
{
update(*root,left,mid,val);
update(*root+,mid+,right,val);
}
else if(left > mid)
{
update(*root+,left,right,val);
}
else
{
update(*root,left,right,val);
}
pushup(root);
} ll query(int root ,int left,int right)
{
if(left == tree[root].left && right == tree[root].right) //如果是合适区间,直接返回节点值
{
return tree[root].large;
}
pushdown(root); //否则向下更新一层,继续查找合适区间
int mid = (tree[root].left+tree[root].right)/;
ll ans = ;
if(right > mid && left <= mid)
{
ans += query(*root,left,mid);
ans += query(*root+,mid+,right);
}
else if(left > mid)
{
ans += query(*root+,left,right);
}
else
{
ans += query(*root,left,right);
}
return ans;
} int main(void)
{
int n,q,i,x,y;
ll c;
char cmd;
scanf("%d %d",&n,&q);
for(i = ; i <= n; i++)
scanf("%lld",&num[i]);
build(,,n);
for(i = ; i < q; i++)
{
getchar();
scanf("%c",&cmd);
if(cmd == 'Q')
{
scanf("%d %d",&x,&y);
printf("%lld\n",query(,x,y));
}
else
{
scanf("%d %d %lld",&x,&y,&c);
update(,x,y,c);
}
}
return ;
}
poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)的更多相关文章
- [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 , 线段树+区间更新。
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 线段树区间更新
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 (伸展树区间更新求和操作 , 模板)
伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...
- 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 ...
随机推荐
- 【10.6NOIP普及模拟】MATH——枚举法
[10.6NOIP普及模拟]MATH 题目简化 一个数列任意删k个数,是得数列中最大的差+最小的差最小 思路 程序1--时超40 暴搜+剪枝. 用类似排列组合的方式,暴搜删或不删 剪枝就是看看剩下的数 ...
- 【转载】gdb基本命令总结
本文介绍使用gdb调试程序的常用命令. 主要内容: [简介] [举例] [其他] [简介] ============= GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.如果你是在 U ...
- Django如何自定义漂亮的404页面
目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...
- LUOGU P2476 [SCOI2008]着色方案
传送门 解题思路 毒瘤题,,刚开始写了个奇奇怪怪的哈希,结果T了5个点..后来深(kan)入(le)思(ti)考(jie),发现c的范围很小,设$f[a][b][c][d][e][pre]$表示还能 ...
- pb_ds(平板电视)简介
据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...
- (转)剖析Linux文件编码的查看及修改
Linux文件编码的查看和修改都有不止一种做法,如果你需要在Linux中操作windows下的文件,那么很可能会经常遇到文件编码转换的问题,如何进行这项工作,也应该是经常工作在双系统下的操作者的必须掌 ...
- C# StructLayout(LayoutKind.Sequential)]
结构体是由若干成员组成的.布局有两种1.Sequential,顺序布局,比如struct S1{ int a; int b;}那么默认情况下在内存里是先排a,再排b也就是如果能取到a的地址,和b的 ...
- 隐藏/显示jeecg-boot 后端管理页面的右侧的系统设置
登录后台,通过添加一个下拉选项[系统设置]来控制系统的后侧系统设置,布局如下: 修改UserMenu.vue文件 1.全局搜索“账户设置”,找到对应的vue文件:UserMenu.vue 2.添加[系 ...
- CheckBox自定义样式
效果: xmal代码: <Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}"> & ...
- C++【stack/queue】用法和例子
Stack的常用基本操作: s.push() // 压栈 s.emplace() // 插入,相当于push(目前掌握的唯一区别是emplace可以自行调用构造函数,push不行) s.empty() ...