【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 ...
随机推荐
- SqlDataAdapter类
SqlDataAdapter类常用操作 SqlDataAdapter常用于从数据库中返回一个结果集时. 常用操作: Fill(); 示例: static void Main(string[] args ...
- 《how to design programs》第10章表的进一步处理
返回表的函数: 下面是一个求工资的函数: ;; wage : number -> number ;; to compute the total wage (at $12 per hour) ...
- Linux MySql安装步骤
本文将以MySQL 5.5.47为例,以CentOS 6.5为平台,讲述MySQL数据库的安装和设置. 源码包方式安装 1.新建MySql用户和用户组 groupadd mysql useradd - ...
- 【转】android 电池(二):android关机充电流程、充电画面显示
关键词:android 电池关机充电 androidboot.mode charger关机充电 充电画面显示 平台信息:内核:linux2.6/linux3.0系统:android/android4. ...
- IOS使用C#预处理命令,多种SDK共存
当我们使用Unity接 91,XY助手等等SDK时候. 我们需要使用[DllImport("__Internal")] 来声明一个C++的方法调用. 不同的SDK总会有不同的方法. ...
- CTSC1999补丁VS错误题解
题目描写叙述 Description 错误就是人们所说的Bug.用户在使用软件时总是希望其错误越少越好.最好是没有错误的.可是推出一个没有错误的软件差点儿不可能,所以非常多软件公司都在疯狂地发放补丁( ...
- UIColor-Hex-Swift
// // UIColorExtension.swift // HEXColor // // Created by R0CKSTAR on 6/13/14. // Copyright (c) 2014 ...
- [Manacher][HDU3613][Best Reward]
题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 裸的: 枚举分割点,计算, ...
- 2、vector的实现
看侯捷老师的<STL源码剖析>有一段时间了,打算自己整理一下思路,试着实现一下.主要目的有两个:1.巩固自己对源码的理解,让自己更加深刻的体会其中各种机制的奥妙.别人的知识 ...
- C++程序设计实践指导1.4正整数转换为字符串改写要求实现
改写要求1:改为适合处理超长整数 #include <cstdlib> #include <iostream> #include <string> using na ...