FZU 2171(线段树的延迟标记)
题意:容易理解。
分析:时隔很久,再一次写了一道线段树的代码,之前线段树的题也做了不少,包括各种延迟标记,但是在组队分任务之后,我们队的线段树就交给了另外一个队友在搞,
然后我就一直没去碰线段树的题了,但是我现在发现这种做法不是很好,导致我现在的思维受到了很大的局限性,所以我现在想纠正这种错误,该做的就应该去做,就像
高中一样不能太偏科!一道比较简单的线段树延迟标记!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath> struct node{
int l , r;
int sum;
int color;
}tree[*]; int n,len,a[]; void buildTree(int l,int r,int n)
{
int mid = (l + r) >> ;
tree[n].l = l;
tree[n].r = r;
tree[n].color = ;
if(l == r)
{
tree[n].sum = a[l];
return ;
} buildTree(l,mid,n*);
buildTree(mid+,r,n*+);
tree[n].sum = tree[n*].sum + tree[n*+].sum;
} void pushDown(int n)
{
tree[n*].sum = tree[n*].sum - (tree[n*].r - tree[n*].l + ) * tree[n].color;
tree[n*+].sum = tree[n*+].sum - (tree[n*+].r - tree[n*+].l + ) * tree[n].color;
tree[n*].color += tree[n].color;
tree[n*+].color += tree[n].color;
} int calSum(int x,int y,int n)
{
int mid = (tree[n].l + tree[n].r) >> ;
if(tree[n].l==x && tree[n].r == y)
return tree[n].sum;
if(tree[n].color)
{
pushDown(n);
tree[n].color = ;
} if(y <= mid)
return calSum(x , y , n*);
else if(x>mid)
return calSum(x , y , n*+);
else
return calSum(x , mid , n*) + calSum(mid+ , y , n*+);
} void update(int x , int y , int n)
{
int mid = (tree[n].l + tree[n].r) >> ; if(tree[n].l == x && tree[n].r == y)
{
tree[n].sum = tree[n].sum - (tree[n].r - tree[n].l + );
tree[n].color ++;
return ;
}
if(y <= mid)
update(x , y , n*);
else if(x > mid)
update(x , y , n*+);
else
{
update(x , mid , n*);
update(mid+ , y , n*+);
}
tree[n].sum = tree[n*].sum + tree[n* + ].sum;
} int main()
{
int i,q,x;
while(scanf("%d%d%d",&n,&len,&q)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
buildTree(,n,);
while(q--)
{
scanf("%d",&x);
printf("%d\n",calSum(x , x+len- , ));
update(x , x+len- , );
}
}
return ;
}
FZU 2171(线段树的延迟标记)的更多相关文章
- FZU 2171 线段树 区间更新求和
很模板的题 在建树的时候输入 求和后更新 #include<stdio.h> #include<string.h> #include<algorithm> #inc ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 线段树初步&&lazy标记
线段树 一.概述: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a, ...
- Fast Arrangement (线段树,延迟标志)
个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...
- 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP
2892: 强袭作战 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 45 Solved: 30[Submit][Status][Discuss] D ...
- BZOJ 1798 (线段树||分块)的标记合并
我原来准备做方差的.. 结果发现不会维护两个标记.. 就是操作变成一个 a*x+b ,每次维护a , b 即可 加的时候a=1 ,b=v 乘的时候a=v ,b=0 #include <cstdi ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- poj3468 线段树的懒惰标记
题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...
随机推荐
- MFC的GUI窗口使用Console输出函数printf(AllocConsole后,使用GetStdHandle取得句柄,然后就可以操作了)
在GUI程序中使用printf函数: #include <io.h> #include <fcntl.h> void InitConsole() { int nRet= 0; ...
- CSS3通配符
在 CSS3 中,追加了三个属性选择器分别为: [att*=val] ----内容包含 [att^=val] ----开头匹配 [att$=val] ----结尾匹配 示例: <!DOCTYPE ...
- mysql和oracle的一个汉字占几个字符
以前一直使用oracle11g,一个汉字占3个字节,所以在操作mysql时也一直这样分配长度. 今天测试了下发现不对了 可以看到第一个的长度确实是15,但是第二个为什么是5? 在网上找到资料:char ...
- UML系列01之 UML用例图
UML,全称是Unified Modeling Language,中文是"统一建模语言".通俗点说,UML是一种创建模型的语言. UML是在开发阶段,说明,可视化,构建和书写一个面 ...
- 解决NetworkOnMainThreadException
今天在Android 访问 WebService 的时候遇到,错误Caused by: android.os.NetworkOnMainThreadException,查了下原因上在4.0之后在主线程 ...
- 9.cadence.封装1[原创]
一.封装中几个重要的概念 软件如下: ①.Regular pad(正规焊盘) 用在:top layer,bottom layer,internal layer(信号层) ②.thermal relie ...
- platform设备驱动全透析
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://21cnbao.blog.51cto.com/109393/337609 1.1 ...
- How Uuencoding Works
做题目学习 https://www.zhihu.com/question/26598476/answer/45396765 http://email.about.com/od/emailbehind ...
- leetcode:Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- POJ3415 Common Substrings
后缀数组 求长度不小于k的公共子串的个数 代码: #include <stdio.h> #include <string.h> ; int len, len1; int wa[ ...