数据结构--线段树--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非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
随机推荐
- AEAI DP开发平台升级说明
本次发版的AEAI DP_v3.5.0版本为AEAI DP _v3.4.0版本的升级版本,该产品现已开源并上传至开源社区http://www.oschina.net/p/aeaidp. 1 升级说明 ...
- 使用innerHTML获取HTML代码时,HTML标记属性的双引号好多都消失不见了,原来是属性值中包含空格才会保留双引号
最近搞的一个项目中所使用的方式比较奇怪,用Label显示HTML内容,然后不断地使用JS把Label的innerHTML复制到TextBox中. 但是,昨天发现了一个问题,获取元素值的时候,有时候正常 ...
- MySQL Query Profile
MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用量, 执行过程 System lock, Table lock 花多少时间等等.从 ...
- vim 正则替换
http://www.cppblog.com/kefeng/archive/2010/10/20/130574.html Vim中的正则表达式功能很强大,如果能自由运用,则可以完成很多难以想象的操作. ...
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
1.ExecutorService: 是一个接口,继承了Executor: public interface ExecutorService extends Executor { } 2.Execut ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- windows下mongodb安装与使用
首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb\d ...
- Android代码混淆官方实现方法
首先查看一下 “project.properties” 这个文件: # This file is automatically generated by Android Tools.# Do not m ...
- Android PopupWindow使用之地区、学校选择二级联动
最近在做一个社交类APP时,希望用户在注册时根据地区来选择自己所在的学校,由于用户手动输入学校,可能会出现各种问题,不利于后面对用户信息的统计.于是决定在客户端做好设置,用户只要根据地区来选择就好.第 ...
- iOS开发笔记7:Text、UI交互细节、两个动画效果等
Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...