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 ...
随机推荐
- 百度分享和bshare
社会法社交分享组件bshare http://www.bshare.cn/ 百度share也不错
- 服务端获得客户端ip
/** * 获取客户端ip地址(可以穿透代理) * * @param request * @return */ public static String getRemoteAddr(HttpServl ...
- WS-* 协议
Web服务作为实现SOA中服务的最主要手段.跟Web Service相关的标准,它们大多以“WS-”作为名字的前缀,所以统称WS-*. Web服务最基本的协议包括UDDI,WSDL和SOAP,通过它们 ...
- zabbix 3.0.2自定义脚本
http://blog.51cto.com/xiao987334176/1769766 有一个通知队列,如果超过了一定的值,就需要报警一下 查询接口可以返回队列的数量,格式是json,data后面的数 ...
- Android开发SQLite数据库的创建
package com.example.db; import android.content.Context; import android.database.sqlite.SQLiteDatabas ...
- 如何查看自己的laravel版本
方法1: 使用php artisan --version ,只要能看懂这个命令的人一定已经具有初步的Laravel知识.再介绍一种不需要命令,直接去文件中去查看的方法. 方法2: 在项目文件中找ven ...
- java多线程实现每隔500毫秒输出一个数字
总结:主要是利用多线程来进行控制它输出的速度,而且这里要处理异常,这个异常我是这样处理的 1.首先写完一个for循环后,写这个:Thread.currnetThread().sleep(500); 然 ...
- 分布式缓存系统 Memcached slab和item的主要操作
上节在分析slab内存管理机制时分析Memcached整个Item存储系统的初始化过程slabs_init()函数:分配slabclass数组空间,到最后将各slab划分为各种级别大小的空闲item并 ...
- Linux - apache 服务
暂时关闭 iptables 防火墙 [root@sch01ar ~]# systemctl stop iptables.service 暂时关闭 firewall 防火墙 [root@sch01ar ...
- 2014.8.8 CAD系统连接
CDA数据库连接生产库.研发库.临时库对应3个连接名 cad = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = backupserver)(POR ...