POJ 3468 A Simple Problem with Integers 【线段树,区间更新】
题意:你有N个整数,A1,A2,…,一个。你须要处理两种类型的操作。一种类型的操作是加入了一些给定的数字,每一个数字在一个给定的时间间隔。
还有一种是在给定的时间间隔要求数量的总和。
难点:主要是lazy标记,不好弄懂, 事实上lazy标记就是当前改变的值不所有更新。等到用的时候再更新,这样就节省了好多时间。
题目链接: id=3468">http://poj.org/problem?id=3468
代码:
#include<stdio.h>
#include<string.h>
#define MAXN 100005
#define LC l, m, rt<<1
#define RC m+1, r, rt<<1|1
#define LL __int64
LL sum[MAXN<<2], add[MAXN<<2];//add数组就是临时储存要添加的
void pushup(int rt)
{
sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt, int m)//这个是难点。理解了这个这道题就差点儿相同了
{
if(add[rt]){
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += add[rt]*(m-(m>>1));
sum[rt<<1|1] += add[rt]*(m>>1);
add[rt] = 0;
}
}
void creat(int l,int r,int rt) {
add[rt] = 0;
if (l == r) {
scanf("%I64d",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
creat(LC);
creat(RC);
pushup(rt);
}
void update(int le, int ri, int num, int l, int r, int rt)
{
if(le <= l&&r<=ri){
add[rt] += num;
sum[rt] += (LL)num*(r-l+1);
return;
}
pushdown(rt, r-l+1);
int m = (l+r)>>1;
if(le <=m) update(le, ri, num, LC);
if(ri > m) update(le, ri, num, RC);
pushup(rt);
}
LL query(int le, int ri, int l, int r, int rt)
{
if(le <= l&&r<=ri){
return sum[rt];
}
pushdown(rt, r-l+1);
LL res = 0;
LL m = (l+r)>>1;
if(le <= m) res += query(le, ri, LC);
if(ri > m) res += query(le, ri, RC);
return res;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
creat(1, n, 1);
char s[2];
int a, b, c;
while(m --){
scanf("%s", s);
if(s[0] == 'Q'){
scanf("%d%d", &a, &b);
printf("%I64d\n", query(a, b, 1, n, 1));
}
else{
scanf("%d%d%d", &a, &b, &c);
update(a, b, c, 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乱码解决
简述 乱码是JAVA开发时经常遇到的问题.主要出现在四种情况: 1. 系统接口之间 2. POST提交数据 3. GET提交数据和URL路径 4. ...
- Create a DAC from a microcontroller's ADC
Few microcontrollers include a DAC. Although you can easily find an inexpensive DAC to control from ...
- ARM 调用约定 calling convention
int bar( int a, int b, int c, int d, int e, int f, int g ) { ]; array2[ ] = a + b; array2[ ] = b + c ...
- C# 通过HttpWebRequest在后台对WebService进行调用
通过HttpWebRequest在后台对WebService进行调用 http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.ht ...
- linux下svn的用法
转载:http://blog.chinaunix.net/uid-22150747-id-189264.html 1.将文件checkout到本地目录 svn checkout path(path是服 ...
- 根据url下载图片和页面
需要将&tp=webp&wxfrom=5去掉,既可以在任何地方显示,也可以下载了 http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9Ac ...
- Python pass 语句
Python pass 语句 Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. Python 语言 pass 语句语法格式如下: pass 实例 ...
- 代码规范审查 - Sonar集成Visual Studio
开发人员可以使用Sonar的集成插件在开发阶段进行实时代码质量审核,在代码编写阶段分析出当前代码的质量及规范漏洞,并给出合理的修改建议.编写高质量.高维护性的代码变得更简单! VS集成Sonar之前 ...
- sqlserver 汉字转拼音
作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 select dbo.procGetPY ('中國') Create FUNCTION dbo.procGetPY ( ...
- 当Windows Server 2012的主DC出了问题, 如何迁移其上的FSMO角色?
步骤如下: 1. 遵循https://support.microsoft.com/kb/255504中的transfer FSMO的步骤. 2. 这样做之后还没完. 因为Windows Server ...