【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 ...
随机推荐
- Linux系统编程(19)——正则表达式在sed和awk中的使用
sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出.sed和 ...
- 【转】Linux内核调试方法总结
目录[-] 一 调试前的准备 二 内核中的bug 三 内核调试配置选项 1 内核配置 2 调试原子操作 四 引发bug并打印信息 1 BUG()和BUG_ON() 2 dump_sta ...
- PHP 表单验证 - 必填字段
-------------------------------------------------------------------------- 本节展示如何制作必填输入字段,并创建需要时所用的错 ...
- hdu 5625 Clarke and chemistry
Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...
- 监控父元素里面子元素内容变化 DOMSubtreeModified
1监控ul的li的变化情况,并实时输出li的长度 布局: <ul id="isSelected"></ul> <span id="modal ...
- .net项目中上传大图片失败
.net项目中有时用户提出要上传大图片,一张图片有可能十几兆,本来用的第三方的上传控件,有限制图片上传大小的设置,以前设置的是2M.按照用户的要求,以为直接将限制图片上传大小的设置改下就可以了,但是当 ...
- SQL With(递归 CTE 查询)
本文来自:http://www.cnblogs.com/smailxiaobai/archive/2012/01/16/2323291.html 指定临时命名的结果集,这些结果集称为公用表表达式 (C ...
- APPCAN学习笔记004---AppCan与Hybrid,appcan概述
APPCAN学习笔记004---AppCan与Hybrid,appcan概述 技术qq交流群:JavaDream:251572072 本节讲了appcan的开发流程,和开发工具 笔记不做具体介绍了,以 ...
- 一个操作EXCEL的C#类ExcelUtils
近期在公司里一直从事服务类的工作,涉及到非常多excel的处理.部分工作内容是每天反复的,仅仅是每天的数据不同而已.我遇到的一个问题是客户每天发送的几种数据有些excel中的字段顺序是一致的,有些是不 ...
- LAMP的安装
一,LAMP的安装流程:mysql.apache.php或者apache.mysql.php.php放到最后的原因是,php在编译安装的时候是依赖于前2者的. 二,Mysql的安装: 1.下载mysq ...