题意:容易理解。

分析:时隔很久,再一次写了一道线段树的代码,之前线段树的题也做了不少,包括各种延迟标记,但是在组队分任务之后,我们队的线段树就交给了另外一个队友在搞,

然后我就一直没去碰线段树的题了,但是我现在发现这种做法不是很好,导致我现在的思维受到了很大的局限性,所以我现在想纠正这种错误,该做的就应该去做,就像

高中一样不能太偏科!一道比较简单的线段树延迟标记!

#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(线段树的延迟标记)的更多相关文章

  1. FZU 2171 线段树 区间更新求和

    很模板的题 在建树的时候输入 求和后更新 #include<stdio.h> #include<string.h> #include<algorithm> #inc ...

  2. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  3. 线段树初步&&lazy标记

    线段树 一.概述: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a, ...

  4. Fast Arrangement (线段树,延迟标志)

    个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...

  5. 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP

    2892: 强袭作战 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 45  Solved: 30[Submit][Status][Discuss] D ...

  6. BZOJ 1798 (线段树||分块)的标记合并

    我原来准备做方差的.. 结果发现不会维护两个标记.. 就是操作变成一个 a*x+b ,每次维护a , b 即可 加的时候a=1 ,b=v 乘的时候a=v ,b=0 #include <cstdi ...

  7. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  8. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

  9. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

随机推荐

  1. LR实现http协议性能测试脚本

    1.  GET方式的HTTP请求性能测试脚本 Action() { web_set_max_html_param_len("); web_reg_save_param("retCo ...

  2. 笔者带你剖析淘宝TDDL(TAOBAO DISTRIBUTE DATA LAYER)

    注:本文部分内容引用本人博客http://gao-xianglong.iteye.com/blog/1973591   前言 在开始讲解淘宝的TDDL(Taobao Distribute Data L ...

  3. SQL Server下实现利用SQL Server Agent Job对索引重建实现Balance Load

    昨天工作中遇到这样一个场景,有个项目需要把某台服务器下所有的表和索引都启用数据压缩(data_compression=page),已经启用了的表和索引就不需要再压缩一次了.统计一下后发现要运行的REB ...

  4. 在struts-config.xml中配置validator-plugin导致404 Servlet action is not available

    就是在struts-config.xml中添加了这么一段 <plug-in className="org.apache.struts.validator.ValidatorPlugIn ...

  5. USACO Section 3.2: Stringsobits

    这题看了网上的答案的.还是很巧妙的 /* ID: yingzho1 LANG: C++ TASK: kimbits */ #include <iostream> #include < ...

  6. GMT与UTC

    时间概念 — 24时区.GMT.UTC的意涵 许多人都知道两地时间表简称为GMT或UTC,而世界时区表则通称为World Time ,那么GMT与UTC的实质原意又是为何?世界时区又是怎么区分的?面盘 ...

  7. REST简析

    内容译自英文原文:A Brief Introduction to REST 不知你是否意识到,围绕着什么才是实现异构的应用到应用通信的“正确”方式,一场争论正进行的如火如荼:虽然当前主流的方式明显地集 ...

  8. Spring Transaction + MyBatis SqlSession事务管理机制[marked]

  9. Git server安装和配置

    yum install git yum install git-dameon toiseGit

  10. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...