POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解
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
思路:
之前学了线段树单点更新,所以想都没想直接一个个点更新果断超时,然后发现是区间更新orz
这是道区间更新模板题。
线段树区间更新引入了一个新东西叫做“懒标记”,它的用处是当我们进行区间更新时不用更新到根节点,比如在[1,10]区间对[1,5]区间更新,我们不需要对1~5都更新(因为目前还没用到1~5),所以我们只要暂时把值存在[1,5]这个节点就行了。如果我们需要访问[1,5]的子节点时,我们需要将暂存的值往下加。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=100005;
const int MOD=20071027;
using namespace std;
struct node{
int l,r;
ll num,add;
}node[N<<2];
int arr[N];
void build(int l,int r,int rt){
node[rt].l=l,
node[rt].r=r;
node[rt].add=0;
if(l==r){
node[rt].num=arr[l];
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
node[rt].num=node[rt<<1].num+node[rt<<1|1].num;
}
void add(int rt,int l,int r,int v){
if(node[rt].l==l && node[rt].r==r){ //暂时储存
node[rt].add+=v;
return;
}
node[rt].num+=v*(r-l+1);
int m=(node[rt].l+node[rt].r)>>1;
if(r<=m){ //改变区间属于左子节点子集
add(rt<<1,l,r,v);
}
else if(l>m){ //属于右子节点子集
add(rt<<1|1,l,r,v);
}
else{ //属于左右子节点子集
add(rt<<1,l,m,v);
add(rt<<1|1,m+1,r,v);
}
}
ll query(int rt,int l,int r){
if(node[rt].l==l && node[rt].r==r){
return node[rt].num+(r-l+1)*node[rt].add;
}
node[rt].num+=(node[rt].r-node[rt].l+1)*node[rt].add; //需要继续往下找,在这里加上之前暂时存放的值
int m=(node[rt].l+node[rt].r)>>1;
add(rt<<1,node[rt].l,m,node[rt].add);
add(rt<<1|1,m+1,node[rt].r,node[rt].add);
node[rt].add=0; //暂存值归零
if(r<=m){ //查询区间属于左子节点子集
return query(rt<<1,l,r);
}
else if(l>m){ //属于右子节点子集
return query(rt<<1|1,l,r);
}
else{ //属于左右子节点子集
return query(rt<<1,l,m)+query(rt<<1|1,m+1,r);
}
}
int main(){
int n,q,a,b,c;
ll ans;
char order[2];
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
build(1,n,1);
while(q--){
scanf("%s",order);
if(order[0]=='Q'){
scanf("%d%d",&a,&b);
ans=query(1,a,b);
printf("%lld\n",ans);
}
else{
scanf("%d%d%d",&a,&b,&c);
add(1,a,b,c);
}
}
}
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 ...
随机推荐
- React-生命周期的相关介绍
1.mounting/组件插入相关 (1)componentWillMount 模板插入前 (2)render 模板插入 (3)componentDidMount 模板插入后 2.Updating/ ...
- 【虫师Python】第二讲:元素定位
一.六种定位方式 1.id 2.name 3.class name 4.tag name:定位标签 5.link text:定位一个链接,如果是中文,需要在代码文最前面加一句I话|:#coding=u ...
- if判断代码 转变为 流程图
转换规则如下: if 代表一个菱形+问号 if后面的条件代表菱形里的内容+↓yes(向下箭头和yes) if同级的else代表右拐向下箭头和no 执行语句块代表一个矩形. if 今天发工资: 先还信用 ...
- [py]监控内存并出图
监控内存出图 先将内存数据搞到数据库 已使用内存算法 used = int(total) - int(free) - int(butffers) - int(cache) pymysql模块使用 db ...
- liferay总结的通用的工具类
在写增删改查的时候,自己动手写了几个通用的工具类,这几个通用的工具类也是基于API写的 第一个是liferay中的分页.跟我们做普通的web开发,分页是一样的首先需要建立一个分页的实体的类 packa ...
- 使用Linux工作之Fedora KDE
小明拿着在Windows下不断蓝屏的T440和公司建议不使用云笔记的规定,心下想着,是时候回归linux了... 一.系统的获取与启动盘的制作 fedora20 KDE版 liveusb-creato ...
- bootstrap3中关于布局的两种样式
container:用.container包裹的内容即可实现居中对齐.注意,由于在各分辨率下面都设置了padding 和 固定宽度,.container不能嵌套.row:栏栅系统是把父容器平均分为12 ...
- uva12083 二分图 求最大独立集 转化为求最大匹配 由题意推出二分图
这题大白书例题 : Frank 是一个思想有些保守的高中老师,有一次,他需要带一些学生出去旅行,但又怕其中一些学生在旅途中萌生爱意.为了降低这种事情的发生概率,他决定确保带出去的任意两个学生至少要满足 ...
- pycharm中内看内建函数的定义
鼠标方法在内建函数上,Ctrl+B,看内建函数的定义 如果想要看内置函数的具体实现细节,可以到python的lib目录下C:\Python27\Lib\,或者python的官网上 如果要看非内建的函数 ...
- selinux配置错误实例介绍
错误原因 配置关闭SELinux,结果误操作 应修改配置文件/etc/selinux/config中的“SELINUX”参数的值, # SELINUX=enforcing 原始配置 SELINUX= ...