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.每次给定 ...
随机推荐
- webSocket学习与应用
非原创,版权归原作者所有http://www.cnblogs.com/shizhouyu/p/4975409.html 1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套 ...
- 一次SocketException:Connection reset 异常排查
问题描述 上一期的需求上线之后,线上多了一个异常:Connection reset.如下: [2017-03-22 00:45:00 ERROR] [creativeAuditTaskSchedule ...
- Ant.SOA微服务框架开源
开源地址:https://github.com/yuzd/AntServiceStack 框架特色0.Service Management(服务治理) 1.CodeGen Contract Fir ...
- sql server 去除字符中空格的方法
用的是REPLACE ( original-string, search-string, replace-string )方法,这三个参数分别是:原字符串.要替换的字符串.替换成的字符串 比如:UPD ...
- centos 6.5 搭建JSP运行环境
一.安装nginx yum install nginx #安装nginx,根据提示,输入Y安装即可成功安装 service nginx start #启动 chkconfig nginx on #设为 ...
- Vijos1523贪吃的九头龙【树形DP】
贪吃的九头龙 传说中的九头龙是一种特别贪吃的动物.虽然名字叫"九头龙",但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头 ...
- 【Electron】Electron开发入门(三):main process和web page 通信
一.main process 和 web page 通信 electron框架主进程(Main Process)与嵌入的网页(web page,也就是renderer process)之间的通信 Ma ...
- 老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态
老李推荐:第14章5节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态 poptest是国内唯一 ...
- python_21_线程+进程+协程
python_线程_进程_协程 什么是线程? -- os能够进行运算调度的最小单位,被包含在进程之中,是一串指令的集合 -- 每个线程都是独立的,可以访问同一进程下所有的资源 什么是进程? -- 每个 ...
- AngularJS1.X学习笔记2-数据绑定
上一篇从整体上认识了Angular,从现在开始更加深入的学习Angular的特性.本次学习的是数据绑定.应该所有的MVC框架都会用到数据绑定,比如我所知道的ThinkPHP.struts等,只有实现了 ...