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 ...
随机推荐
- 2.1 一个简单的Web工程例子
一个简单的Web工程例子 开发环境: Eclipse: Neon Release (4.6.0) JDK:1.8.0_92 Tomcat:8.5.9 Maven:3.3.9 1. 在Eclipse中创 ...
- haproxy中两个常用的宏
list_for_each(pos, head) pos.head 都为list类型 一般来说,head是list的虚拟头,依次取list上的各个节点 container_of(ptr, type, ...
- Android 杂记
Android Studio 报错:sdk location should not contain whitespace as this can cause problems with the ndk ...
- PCB 封装中的 公差符号形位公差位置度
PCB 封装中的 公差符号形位公差位置度 0.08 旁边的 十字加圆就是位置度的形位公差.
- 冒泡排序的JavaScript实现
1. 普通冒泡 思想 假设有n个数,按从小到大排序: 进行n-1次外循环,每次外循环会排好当前处理的数中的最大数,即进行第一次外循环排好所有数中的最大数,进行第二次外循环排好所有数中的次大数....直 ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...
- 杂项-公司:Netflix百科-un
ylbtech-杂项-公司:Netflix百科-un Netflix(Nasdaq NFLX) 成立于1997年,是一家在线影片租赁提供商,主要提供Netflix超大数量的DVD并免费递送,总部位于美 ...
- [置顶]
TIM_GetCounter与TIM_GetCapture1的区别
/** * @brief Gets the TIMx Input Capture 1 value. * @param TIMx: where x can be 1 to 17 except 6 and ...
- win10系统怎么关闭自动更新
现在win10已经很普遍了,对于win10 现在还不是很完美,比如自动更新功能,现在的选项中没有关闭自动更新的选项了,这是一个bug,微软要强制更新.我就忍受不了自动更新,会拉取网络,影响我们的上网体 ...
- C#字符串Split方法的误区
string s = "aaa1bbb2ccc1ddd"; string[] ss = s.Split("12".ToCharArray()); ...