hdu 1556 Color the ball (线段树做法)
Problem Description
Input
当N = 0,输入结束。
Output
#include<stdio.h>
#pragma comment(linker,"/STACk:1024000000,1024000000")
struct CNode
{
int L,R;
//int nSum;
int Inc;
CNode *pLeft,*pRight;
};
CNode Tree[];
int nCount;
int Mid(CNode *pRoot)
{
return (pRoot->L+pRoot->R)/;
}
void BuildTree(CNode *pRoot,int L,int R)
{
pRoot->L=L;
pRoot->R=R;
//pRoot->nSum=0;
pRoot->Inc=;
if(L==R)
return ;
nCount++;
pRoot->pLeft=Tree+nCount;
nCount++;
pRoot->pRight=Tree+nCount;
BuildTree(pRoot->pLeft,L,(L+R)/);
BuildTree(pRoot->pRight,(L+R)/+,R);
}
/*void Insert(CNode *pRoot,int i,int v)
{
if(pRoot->L==i&&pRoot->R==i)
{
pRoot->nSum=v;
return ;
}
pRoot->nSum+=v;
if(i<=Mid(pRoot))
Insert(pRoot->L,i,v);
else
Insert(pRoot->R,i,v);
}*/
void Add(CNode *pRoot,int a,int b)
{
if(pRoot->L==a&&pRoot->R==b)
{
pRoot->Inc++;
return ;
}
//pRoot->nSum+=c*(b-a+1);
if(b<=Mid(pRoot))
Add(pRoot->pLeft,a,b);
else if(a>=Mid(pRoot)+)
Add(pRoot->pRight,a,b);
else
{
Add(pRoot->pLeft,a,Mid(pRoot));
Add(pRoot->pRight,Mid(pRoot)+,b);
}
}
int ans;
int QuerySum(CNode *pRoot,int a,int b,int i)
{
ans+=pRoot->Inc;
if(pRoot->L==i&&pRoot->R==i)
return ans;//pRoot->nSum+(pRoot->R-pRoot->L+1)*pRoot->
//pRoot->nSum+=(pRoot->R-pRoot->L)*pRoot->Inc; //Add(pRoot->pLeft,pRoot->L,Mid(pRoot),pRoot->Inc);
//Add(pRoot->pRight,Mid(pRoot)+1,pRoot->R,pRoot->Inc);
//pRoot->Inc=0;
if(i<=Mid(pRoot))
return QuerySum(pRoot->pLeft,a,b,i);
else //(i>=Mid(pRoot)+1)
return QuerySum(pRoot->pRight,a,b,i);
/*else
{
return QuerySum(pRoot->pLeft,a,Mid(pRoot),i)
+QuerySum(pRoot->pRight,Mid(pRoot)+1,b,i);
}*/
}
int main()
{
int n,a,b,i;
while(scanf("%d",&n)!=EOF&&n)
{
nCount=;
BuildTree(Tree,,n); for(i=;i<n;i++)
{
scanf("%d%d",&a,&b);
Add(Tree,a,b);
}
for(i=;i<n;i++)
{
ans=;
printf("%d ",QuerySum(Tree,,n,i));
}
ans=;
printf("%d\n",QuerySum(Tree,,n,n));
}
return ;
}
hdu 1556 Color the ball (线段树做法)的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
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) T ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- hdu 1556 Color the ball (树状数组)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 1556 Color the ball(树状数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
随机推荐
- javascript:;与javascript:void(0)
如果想做一个链接点击后不做任何事情,或者响应点击而完成其他事情,可以设置其属性 href = "#",但是,这样会有一个问题,就是当页面有滚动条时,点击后会返回到页面顶端,用户体验 ...
- 十大Java人物
James Gosling : Java之父文/陶文 作 为Java之父,James Gosling的名字可谓是耳熟能详.当人们评论一种编程语言时,总喜欢捎带着把下蛋的母鸡一起带上.Java做为中国的 ...
- (转)android项目用到的公共类方法
/*** 直接下载图片并加载至控件(非异步加载)* * @param activity* @param urlpath* 图片下载路径* @param imageView* ...
- GOF23设计模式之建造者模式(builder)
一.建造者模式概述 建造者模式的本质: 1.分离了对象子组件的单独构造(由Builder负责)和装配(由Director负责).从而可以构造出复杂的对象.这个模式适用于:某个对象的过程复杂的情况下使用 ...
- juc线程池原理(二):ThreadPoolExecutor的成员变量介绍
概要 线程池的实现类是ThreadPoolExecutor类.本章,我们通过分析ThreadPoolExecutor类,来了解线程池的原理. ThreadPoolExecutor数据结构 Thread ...
- Rest之路 - Rest架构中的重要概念
资源 在Rest的架构之内,讲一切内容都是为资源.每一个资源都被定义为一个URI. 格式: <protocol>://<service-name>/<ResourceTy ...
- C# 异常内存信息导出Dump文件
背景:很多情况下程序崩溃我们只能看到程序抛出来的异常信息,但是有时候异常信息不清不楚我们处理异常还是一头雾水,这种情况下我们就很希望能有种办法获取程序运行时的内存进行调试,查看其中的变量.参数.方法执 ...
- AJAX中的跨域问题:什么是跨域?如何解决跨域问题?
域不一样的,即为跨域,包括(协议,域名,端口号) 1. 指定允许其他域名访问 header('Access-Control-Allow-Origin:*'); 2.使用jsonp
- nodejs中的util.inspect.js
util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出.它至少接受一个参数 object,即要 ...
- a标签的四个伪类是什么?如何排序?为什么?
爱恨分明原则: l v h a 注释:为了产生预期的效果,在 CSS 定义中,a:hover 必须位于 a:link 和 a:visited 之后 ! 注释:为了产生预期的效果,在 CSS 定义中,a ...