数据结构--线段树--lazy延迟操作
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 53749 | Accepted: 16131 | |
| Case Time Limit: 2000MS | ||
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
//更新某一段区域的时候,采用延迟标记~~
代码:
#include "cstdio" //poj 3468 lazy操作
#include "cstring"
#include "iostream"
using namespace std; #define N 100005
#define LL long long struct node{
int x,y;
LL sum;
LL add; //记录以当前节点为根节点的树中需要增加的值
}a[*N]; void Build(int t,int x,int y)
{
a[t].x = x;
a[t].y = y;
a[t].sum = a[t].add = ;
if(a[t].x == a[t].y) //到了叶子节点
{
scanf("%lld",&a[t].sum);
return ;
}
int mid = (a[t].x + a[t].y)/;
Build(t<<,x,mid);
Build(t<<|,mid+,y);
a[t].sum = a[t<<].sum + a[t<<|].sum;
} void Push_down(int t) //将add(增值)向下推一级
{
LL add = a[t].add;
a[t<<].add += add;
a[t<<|].add += add;
a[t<<].sum += add*(a[t<<].y-a[t<<].x+);
a[t<<|].sum += add*(a[t<<|].y-a[t<<|].x+);
a[t].add = ;
} LL Query(int t,int x,int y)
{
if(a[t].x==x &&a[t].y==y)
return a[t].sum;
Push_down(t);
int mid = (a[t].x + a[t].y)/;
if(y<=mid)
return Query(t<<,x,y);
if(x>mid)
return Query(t<<|,x,y);
else
return Query(t<<,x,mid) + Query(t<<|,mid+,y);
} void Add(int t,int x,int y,int k)
{
if(a[t].x==x && a[t].y==y) //不在推到叶子节点,t下的子孙要增加的值存入a[t].add中
{
a[t].add += k;
a[t].sum += (a[t].y-a[t].x+)*k;
return ;
}
a[t].sum += (y-x+)*k;
Push_down(t);
int mid = (a[t].x+a[t].y)/;
if(y<=mid)
Add(t<<,x,y,k);
else if(x>mid)
Add(t<<|,x,y,k);
else
{
Add(t<<,x,mid,k);
Add(t<<|,mid+,y,k);
}
} int main()
{
int n,m;
char ch;
int x,y,k;
scanf("%d %d",&n,&m);
Build(,,n);
while(m--)
{
getchar();
scanf("%c %d %d",&ch,&x,&y);
if(ch=='Q')
printf("%lld\n",Query(,x,y));
else
{
scanf("%d",&k);
Add(,x,y,k);
}
}
return ;
}
数据结构--线段树--lazy延迟操作的更多相关文章
- 线段树区间更新操作及Lazy思想(详解)
此题题意很好懂: 给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c. 需要用到线段树的,update:成段增减,query:区间求 ...
- POJ 2777——线段树Lazy的重要性
POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...
- 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- JuQueen(线段树 lazy)
JuQueen Time Limit: 5 Sec Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- Fast Arrangement (线段树,延迟标志)
个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...
- HDU 3954 Level up(多颗线段树+lazy操作)
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...
- poj 3237 树链剖分模板(用到线段树lazy操作)
/* 本体在spoj375的基础上加了一些操作,用到线段树的lazy操作模板类型 */ #include<stdio.h> #include<string.h> #includ ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
随机推荐
- .NET 配置文件简单使用
当我们开发系统的时候要把一部分设置提取到外部的时候,那么就要用到.NET的配置文件了.比如我的框架中使用哪个IOC容器需要可以灵活的选择,那我就需要把IOC容器的设置提取到配置文件中去配置.实现有几种 ...
- 译:重置/还原Windows IIs设置为默认设置
译文出处:http://www.codeproject.com/Tips/870858/Reset-Restore-IIS-Settings-to-its-Default-in-Windo 简介: I ...
- 面向对象的JavaScript(3):私有成员和公开成员
在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...
- 【EF 译文系列】模型和数据库连接
原文链接:Connections and Models 本篇文章主要包括 Entity Framework 是如何选择数据库进行连接,以及我们如何去改变它的连接.无论是通过 Code First 还 ...
- 安装win8、ubuntu双系统的过程
弄了一个晚上,终于完成了,之前是用虚拟机的,但是觉得不带劲,并且折腾来时菜鸟变大神的捷径,虽然现在还一直在爬坑.继续奋斗吧...王小二 首先是看 ubuntu 百度贴吧的安装帖子(http://tie ...
- sql语句分页多种方式ROW_NUMBER()OVER
sql语句分页多种方式ROW_NUMBER()OVER 摘自: http://www.cnblogs.com/CodingArt/articles/1692468.html 方式一 select to ...
- c#调用word com组件 替换书签套打
安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...
- foreach---集合已修改;可能无法执行枚举操作。
小结 : foreach是取只读的,在取的时候数据不能变(包括修改,删除,添加等).要避免这个问题,就应该使用for循环--- 原因: 当用foreach遍历Collection时,如果对Collec ...
- 百度FIS入门
1.fis作为nodejs的模块来管理的,所以首先得安装nodejs,看我前面的安装nodejs的文章. 2.官方的案例下载包https://github.com/hefangshi/fis-quic ...
- Scala on Visual Studio Code
Download and install Scala Download a scala installation package from here. Then install it. Linux s ...