题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556

简单的线段树的应用

直接贴代码了:

代码:

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#define maxn 100100
using namespace std;
class node
{
public:
int l;
int r;
int add;
};
int n;
node segTree[maxn*];
int ans[maxn];
void Build(int num ,int l,int r)
{
segTree[num].l=l;segTree[num].r=r;
segTree[num].add=;
if(l==r) return ;
int mid=(l+r)/;
Build(num*,l,mid);
Build(num*+,mid+,r);
}
void Update(int num,int l,int r)
{
if(segTree[num].l ==l && segTree[num].r==r)
{
segTree[num].add+=;
return ;
}
int mid=(segTree[num].l + segTree[num].r)/;
if(segTree[num].add)
{
segTree[num*].add+=segTree[num].add;
segTree[num*+].add+=segTree[num].add;
segTree[num].add=;
}
if(r<=mid ) Update(num*,l,r);
else if(l>mid) Update(num*+,l,r);
else
{
Update(num*,l,mid);
Update(num*+,mid+,r);
}
}
void Answer(int num)
{
if(segTree[num].l == segTree[num].r)
{ans[segTree[num].l]=segTree[num].add; return ;}
if(segTree[num].add)
{
segTree[num*].add+=segTree[num].add;
segTree[num*+].add+=segTree[num].add;
segTree[num].add=;
} Answer(num*);
Answer(num*+);
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
memset(ans,,sizeof(ans)); Build(,,n);
int l,r;
for(int i=;i<=n;i++)
{
scanf("%d%d",&l,&r);
Update(,l,r);
}
Answer();
cout<<ans[];
for(int i=;i<=n;i++)
cout<<" "<<ans[i];
cout<<endl;
}
return ;
}

hdu1556 Color the ball 简单线段树的更多相关文章

  1. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. hdu 1556 Color the ball (技巧 || 线段树)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  6. Color the ball(线段树)

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. hdu 1199 Color the Ball 离散线段树

    C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  9. HDU1556:Color the ball(简单的线段树区域更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定 ...

随机推荐

  1. oracle学习 笔记(2)

    题记:在使用Oracle数据库的时候,发现Oracle是没有自动增长列来实现主键的,所以在此记录学习.(PS:如果哪里有错误或者不足的地方还请大家帮忙指出来) 二.序列(自动增长列) 为此问题博主也是 ...

  2. parentNode和parentElement区别

    parentNode跟parentElement除了前者是w3c标准,后者只ie支持 当父节点的nodeType不是1,即不是element节点的话,它的parentElement就会是null 一般 ...

  3. 将node.js程序作为服务,并在windows下开机自动启动(使用forever)

    手上项目中有一块服务是用node.js实现的,运行环境是windows server 2008 R2,刚开始着手实现这块功能的时候时间很紧迫,随便写了个console程序就部署上去了--启动方式就是在 ...

  4. opencv配置(win10+VS2015+opencv3.1)

    Step 1:准备工作 a.win10 b.vs2015 c.opencv3.1[从http://opencv.org/downloads.html下载] Step 2.开始安装 a. 双击openc ...

  5. RDCの自我介绍

    ........................................... 大家好,这里是RUSH_D_CAT.一只ACMer,19岁的少年,From SDU,大二. Q: 1950499 ...

  6. D3.js-坐标轴生成测试SVG

    Bottom top Bottom top   // =0) return "translate("+margin+","+i*offset+")&q ...

  7. Selenium测试专项三班隆重开班

    Selenium测试专项三班隆重开班 应广大测试技术人员要求,以及企业技术需求.Selenium提前一周开课了,只针对合作的每家企业提供1-2个参训名额.预计培训60人次.但报名人数却远远超出我们预期 ...

  8. 老李分享:JVM调优

    老李分享:JVM调优   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨 ...

  9. js正则表达式匹配字符串与优化过程

    前言 有时候需要实现对js源文件中的url字符串做拦截预处理,或者前端js语法高亮,或者需要对动态加载的关键源码做混淆保护,在某些步骤实现之前,有一个步骤是需要提炼出所有的合法字符串. 目标:检测源文 ...

  10. JavaEE开发之Spring中的条件注解组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...