hdu1556 Color the ball 简单线段树
题目链接: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 简单线段树的更多相关文章
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdoj 1556 Color the ball【线段树区间更新】
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1556 Color the ball (技巧 || 线段树)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
- Color the ball(线段树)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 1199 Color the Ball 离散线段树
C - Color the Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- HDU1556:Color the ball(简单的线段树区域更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定 ...
随机推荐
- ABP入门系列(15)——创建微信公众号模块
ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 1. 引言 现在的互联网已不在仅仅局限于网页应用,IOS.Android.平板.智能家居等平台正如 ...
- 老李分享:接电话扩展之uiautomator 1
老李分享:接电话扩展之uiautomator poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq ...
- 操作系统之cache、伙伴系统、内存碎片、段式页式存储管理
存储管理是操作系统非常重要的功能之一,本文主要介绍操作系统存储管理的基础知识,包括缓存相关知识.连续内存分配.伙伴系统.非连续内存分配.内存碎片等,并结合linux系统对这些知识进行简单的验证.文章内 ...
- HTML基础学习(二)—CSS
一.CSS概述 CSS(Cascading Stytle Sheets)层叠样式表,用来定义网页的显示效果.可以解决HTNL代码对样式定义的重复,提高了后期样式代码的可维护性,并增强了网页的显 ...
- Reflux中文教程——概览
翻译自github上的reflux项目,链接:https://github.com/reflux/refluxjs 〇.安装及引入 安装: npm install reflux 引入: var Ref ...
- nagios报错HTTP WARNING: HTTP/1.1 403 Forbidden解决方法
Nagios--localhost报警:"WARNING: HTTP/1.1 403 Forbidden "解决方法: In dashboard it shows alert on ...
- Java中boolean类型占用多少个字节
为什么要问这个问题,首先在Java中定义的八种基本数据类型中,除了其它七种类型都有明确的内存占用字节数外,就boolean类型没有给出具体的占用字节数,因为对虚拟机来说根本就不存在 boolean 这 ...
- Mybatis(一) mybatis入门
学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...
- 【Tomcat源码学习】-4.连接管理
前面几节主要针对于Tomcat容器以及内容加载进行了讲解,本节主要针对于连接器-Connector进行细化,作为连接器主要的目的是监听外围网络访问请求,而连接器在启动相关监听进程后,是通过NIO方式进 ...
- T-SQL编程语句
书接上回 一起学习下SQL中的表连接 一般情况下咱们会使用鼠标去进行表连接操作,那针对于像我比较懒的并且眼盲的,不喜欢来回切换,咱们就用到了点T-SQL表连接语句 一般情况咱们从两个表中查出来相似的内 ...