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.每次给定 ...
随机推荐
- HTTP协议(三)
一.首先我们画一个图来看一下HTTP协议: 难道方法只有POST GET吗?NO,还有一些少用的方法. 二.请求方法有哪些? GET POST HEADER PUT TRACE DELETE OPTI ...
- apache和nginx原理上的不同之处
今天群里提到面试时问到apache和nginx原理有什么不同,一时还真没想起,想到的只是他们的优缺点,便搜索了下.记录学习下.顺便记录下优缺点吧. 原理不同之处: 为什么Nginx的性能要比Apach ...
- Android之仿京东淘宝的自动无限轮播控件
在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于Re ...
- HTML的语义化,你需要深入了解
有关HTML的一些基础课程,很多网站都有讲,于我而言,真正实践起来,我只要求我能够让它表现出我所想要的结果即可.然而,这种要求,对于后期的维护与测试,真的是......想起日前我们所做的这个项目,那里 ...
- SQL Server 备份所有数据库代码
今天让我备份一下网上所有数据库,猛地一看,几百个呢, 坑爹呢,只好网上找找有没有简便的,没想到还真有 记下来,以后好用,哈哈... use master declare @DbName varchar ...
- css简单实现火焰效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 第5章Zabbix自动化监控
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; t ...
- JDK中日期和时间的几个常用类浅析(五)
LocalDateTime LocalDateTime是JDK8中才引入的类,用来表示不包含时区信息的本地日期和时间.我们可以把LocalDateTime看作是LocalDate和LocalTim ...
- 【Electron】Electron开发入门(五):项目打包
一.安装 electron-packager PS:安装之前,先复制一份package.json文件到./app目录下,然后改下./app目录下package.json里 "main&quo ...
- Ubuntu上手动安装Kubernetes
背景 两台Ubuntu16.04服务器:ip分别为192.168.56.160和192.168.56.161.. Kubernetes版本:1.5.5 Docker版本:1.12.6 etcd版本:2 ...