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 ...
随机推荐
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 读书笔记_Effective_C++_条款二十六:尽可能延后变量定义式的出现时间
这个条款从字面意思还是很好理解的,就是在使用这个变量前才去定义,而不是很早就定义了它,而在很后面的时候才去使用.这个条款只适用于对变量声明位置没有要求的语言,比如C++.对于像C或者一些脚本语言,语法 ...
- linux基础命令学习(二)文件和目录操作
1.变换当前目录(change directory) cd /home 进入 '/ home' 目录' (change directory) cd .. 返回上一级目录 cd .. ...
- mongoDB系列之(一):10分钟玩转mongoDB
1. mongoDB是什麽 mongodb是时下流行的NoSql数据库,它的存储方式是文档式存储,并不是Key-Value形式. 存储在集合中的文档,被存储为键-值对的形式.键用于唯一标识一个文档,为 ...
- loading(正在加载特效)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- dao层知识点总结
1.dao层要有connection 2.dao层进行分页,mysql limit关键字 3.dao层进行结果集转换为java bean 4.dao层queryforlist
- mongodb的分布式集群(2、副本集)
概述 副本集是主从复制的一种,是一种自带故障转移功能的主从复制.攻克了上述主从复制的缺点.实现主server发生问题后.不需人为介入.系统自己主动从新选举一个新的主server的功能. ...
- 能力成熟度模型CMM
能力成熟度模型(Capability Maturity Model,英文缩写为CMM)[1]是 一种开发模型.Carnegie Mellon大学的研究人员从美国国防部合同承包方那里收集数据并加以研究, ...
- 浏览器User-Agent的详细信息
PC端: safari 5.1 – MACUser-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit ...
- 加解密 3DES AES RSA 简介 示例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...