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 ...
随机推荐
- jquery 如何使用innerHTML
$("#responsediv") 是个Jquery对象,它Val()是对Value属性赋值对它无意义,Jquery没有innerHTML这个属性,应该这样写$("#re ...
- 当 PHP 遇上 MongoDB
FROM:http://www.cstor.cn/textdetail_7995.html 之前笔者出了一篇文章是教大家在 Linux 下安装 MongoDB,并且透过 Mongo Client 操作 ...
- 参数化防SQL注入
private void AddStudent(){ string strName =txtName.Text.Trim(); string strPwd = txtPwd.Text.Trim(); ...
- switch控件的使用
java中: public class MainActivity extends Activity implements OnCheckedChangeListener{ private Switch ...
- Instantiate实例化的注意事项
_obj= Resources.Load("xxx") as GameObject;Instantiate(_obj); 这里的_obj对象和 _obj= Instantiate( ...
- 源码安装ceph后使用测试集群的方法
标签(空格分隔): ceph,ceph实验,ceph源码 通过博客 源码编译安装ceph(aarch64架构) 成功安装ceph之后,之后可以运行一个测试集群进行实验 1,进入安装构建目录: [roo ...
- C#简单操作XML
类文件: class OperatorXML { /// <summary> /// 确定资源文件路径,Resource为自己创建的目录 /// </summary> priv ...
- MFC学习(一) MFC基础类及其层次结构
从类CCmdTarget派生出绝大多数MFC中的类,其层次结构如下图: 从根类Cobject层层派生出绝大多数MFC中的类,层次结构如下图: MFC中重点类: CObject类是MFC的绝大部分类的基 ...
- 执行make出现“Warning: File `xxx.c' has modification time 2.6e+04 s in the future“警告的解决方法
错误描述: 执行make命令时出现"make[2]: Warning: File `xxx.c' has modification time 1.6e+05 s in the future ...
- Keepalived+LVS(dr)高可用负载均衡集群的实现
一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+lvs双主高可用负载均衡集群及LAMP应用keepalived-1.2 ...