【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
Hint
#include <cstdio>
#include <cstring> typedef long long ll;
const int LEN = * ; struct line
{
int left;
int right;
ll value;
ll lazy; //懒惰标记
}line[LEN]; void buildt(int l, int r, int step) //建树初始化
{
line[step].left = l;
line[step].right = r;
line[step].lazy = ;
line[step].value = ;
if (l == r)
return;
int mid = (l + r) / ;
buildt(l, mid, step<<);
buildt(mid+, r, step<<|);
} void pushdown(int step)
{
if (line[step].left == line[step].right) //如果更新到最深处的子节点,返回
return;
if (line[step].lazy != ){ //如果有懒惰标记,向下传递懒惰标记且更新两个子节点的值
line[step<<].lazy += line[step].lazy;
line[step<<|].lazy += line[step].lazy;
line[step<<].value += (line[step<<].right - line[step<<].left + ) * line[step].lazy;
line[step<<|].value += (line[step<<|].right - line[step<<|].left + ) * line[step].lazy;
line[step].lazy = ;
}
} void update(int l, int r, ll v, int step)
{
line[step].value += v * (r-l+); //更新到当前节点,就在当前节点的value中加上增加的值
pushdown(step);
if (line[step].left == l && line[step].right == r){ //如果到达目标线段,做上懒惰标记,返回
line[step].lazy = v;
return;
}
int mid = (line[step].left + line[step].right) / ;
if (r <= mid)
update(l, r, v, step<<);
else if (l > mid)
update(l, r, v, step<<|);
else{
update(l, mid, v, step<<);
update(mid+, r, v, step<<|);
}
} ll findans(int l, int r, int step)
{
if (l == line[step].left && r == line[step].right) //如果找到目标线段,返回值
return line[step].value;
pushdown(step);
int mid = (line[step].left + line[step].right) / ;
if (r <= mid)
return findans(l, r, step<<);
else if (l > mid)
return findans(l, r, step<<|);
else
return findans(l, mid, step<<) + findans(mid+, r, step<<|);
} int main()
{
//freopen("in.txt", "r", stdin);
int n, q;
scanf("%d %d", &n, &q);
buildt(, n, );
for(int i = ; i <= n; i++){
ll t;
scanf("%I64d", &t);
update(i, i, t, );
}
for(int i = ; i < q; i++){
char query[];
scanf("%s", query);
if (query[] == 'C'){
int a, b;
ll c;
scanf("%d %d %I64d", &a, &b, &c);
update(a, b, c, );
}
else if (query[] == 'Q'){
int a, b;
scanf("%d %d", &a, &b);
printf("%I64d\n", findans(a, b, ));
}
}
return ;
}
【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
		
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
 - POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
		
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
 - POJ3468_A Simple Problem with Integers(线段树/成段更新)
		
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
 - POJ 3468 线段树 成段更新 懒惰标记
		
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
 - 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 ...
 - poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
		
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
 - [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 (线段树区间更新求和lazy思想)
		
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
 
随机推荐
- COM组件开发实践(七)---多线程ActiveX控件和自动调整ActiveX控件大小(上)
			
声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改而来,因此同样遵循Code Project Open L ...
 - Grid++Report支持CS/BS模式的表报插件
			
Grid++Report 可用于开发桌面C/S报表与WEB报表(B/S报表),C/S报表开发适用于VB.NET.C#.VB.VC.Delphi等.WEB报表开发适用于ASP.ASP.NET.JSP/J ...
 - MongoDBAuth
			
1,mogoDB 认证登陆
 - Linux MySql安装步骤
			
本文将以MySQL 5.5.47为例,以CentOS 6.5为平台,讲述MySQL数据库的安装和设置. 源码包方式安装 1.新建MySql用户和用户组 groupadd mysql useradd - ...
 - UGUI Image控件
			
今天一起学习Image控件O(∩_∩)O~ 介绍一下基本的属性 Source:Image: 指定图片源, 图片设置2DSprite(2D and UI)格式Color: ...
 - 利用Python完成一个小游戏:随机挑选一个单词,并对其进行乱序,玩家要猜出原始单词
			
一 Python的概述以及游戏的内容 Python是一种功能强大且易于使用的编程语言,更接近人类语言,以至于人们都说它是“以思考的速度编程”:Python具备现代编程语言所应具备的一切功能:Pytho ...
 - Oracle基础学习5-- Oracle权限之”角色”
			
不论什么与权限相关的东西都少不了"角色"的概念,Java如此,.Net如此,Oracle当然也不例外. 角色事实上就是权限的集合,将多个权限打包到一个角色中,这样每一个角色有特定的 ...
 - Unity 安卓Jar包的小错误
			
好久没写博客了,也就意味着好久没有学习了,近几天在搞Unity接入有米的SDk遇到了一点小错误,今天早上解决了,和大家分享下! 1,我们的目的是在在U3D中调用Android产生的Jar包,首先在Ec ...
 - 【Java基础】setter与getter方法
			
//下面代码实现设置和获取学生姓名和成绩. class lesson5homework { public static void main(String[] args) { TestCode TC=n ...
 - 反射操作辅助类ReflectionUtil
			
这篇文章的目的是介绍这样一种方式,就是在写一个函数的时候,传递的参数是object类型的,在这个函数里面想访问这个参数对象的某一属性值,我们知道这个属性值的name,但是一般情况下,object对象是 ...