hdu 1556 Color the ball (线段树+代码详解)
Color the ball
当N = 0,输入结束。
1 1
2 2
3 3
3
1 1
1 2
1 3
0
3 2 1
一开始写这个题的时候我是用了线段树,但是a了之后看其他大牛的博客,发现了另一种思路,并且时间空间都要比线段树少很多,那么现在就来归纳一下这两种方法。
方法1:
#include<stdio.h>
struct data{
int l,r,v;
}line[400005];
void bulid (int l,int r,int x) //构建二叉树;
{
line[x].l=l;
line[x].r=r;
line[x].v=0; //这里需要讲所有节点标记为零;
if(l==r){
return ;
}
int m=(r+l)/2;
bulid(l,m,x*2);
bulid(m+1,r,x*2+1);
}
void updata (int l, int r ,int x , int a , int b ) //更新二叉树;
{
if(l<=a&&b<=r){ //如果节点x在l和r区间范围之内,则这个区间标记的值加1;
line[x].v++;
return ;
}
int m=(a+b)/2;
if(m<l){ //这里需要注意符号
updata(l,r,x*2+1,m+1,b);
}else if(r<=m){
updata(l,r,x*2,a,m);
}else {
updata(l,m,x*2,a,m);
updata(m+1,r,x*2+1,m+1,b);
}
}
int query (int i,int x,int l,int r,int sum) //查询,其中sum记录涂颜色的次数;
{
if(i==l&&i==r){
return sum+line[x].v;
}
int m=(l+r)/2;
sum+=line[x].v;
if(i<=m){
return query(i,x*2,l,m,sum);
}else {
return query(i,x*2+1,m+1,r,sum);
}
}
int main ()
{
int i,j,n,m,a,b;
while(scanf("%d",&n),n!=0){
bulid(1,n,1);
for(i=0;i<n;i++){
scanf("%d %d", &a, &b);
updata(a,b,1,1,n);
}
for(i=1;i<=n;i++){
if(i==1)
printf("%d",query(i,1,1,n,0));
else printf(" %d",query(i,1,1,n,0));
}
printf("\n");
}
return 0;
}
方法二:
#include<stdio.h>
#include<string.h>
int main ()
{
int line[100010];
int n,a,b,i,sum;
while(scanf("%d",&n),n){
memset(line,0,sizeof(line));
for(i=0;i<n;i++){
scanf("%d %d",&a,&b);
line[a]++;
line[++b]--;
}
sum=0;
for(i=1;i<=n;i++){
sum+=line[i];
if(i==1)
printf("%d",sum);
else printf(" %d",sum);
}
printf("\n");
}
return 0;
}
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 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 ...
随机推荐
- http协议与url简介(转)
一 知识简介 HTTP:(Hypertext transfer protocol)超文本传输协议,是用于从万维网(WWW:World Wide Web)服务器传输超文本到本地浏览器的传送协议. URL ...
- 【TCP/IP详解 卷一:协议】第十二章 广播和多播
建议参考:广播和多播 IGMP 12.1 引言 IP地址知识点回顾: IP地址分为三种:(1)单播地址 (2)广播地址 (3)多播地址 另外一种是,IP地址一般划分成五类:A-E类. 单播 考虑 类似 ...
- 又见链表 --- 另一种Creat方式与反转
链表 作为一种数据结构,链表以其方便的增删查改功能,实现了无数经典有用的程序. 在之前的帖子里,我构建链表的方式是建立一个不储存数据的head节点,然后通过一边输入数据一边建立结点的方式构建整个链表. ...
- React Native 之轮播图swiper组件
注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...
- selenium-chrome-headless
#coding=utf-8 from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() ...
- Jmeter测试API接口,用Jmeter自动化之检查DB数据
如上: 注册接口,会新增数据,要怎么自动化检查DB中生成的数据呢? 很简单,只需要以下几个配置元件 JSON截取器或者正则表达式截取器:目的在于取出返回消息体中的数据aa JDBC后置处理器:目的在于 ...
- [原][OSG][osgBullet][osgworks][bullet]编译osgBullet尝试物理引擎
相关网址: 类似文章:http://blog.csdn.net/lh1162810317/article/details/17475297 osgBullet官网:http://osgbullet.v ...
- 《剑指offer》第七题(重要!重建二叉树)
文件一:main.cpp // 面试题:重建二叉树 // 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输 // 入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历 ...
- C语言位域解析&符号位扩展规则
从一个例子说起: int main(void){ union{ int i; struct{ ; ; ; }bits; }num; printf("Input an integer for ...
- Codeforces 899E - Segments Removal
899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...