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 ...
随机推荐
- PyCharm/IDEA 使用技巧总结
基本概念 IDEA 没有类似 Eclipse 的工作空间的概念(workspace),最大单元就是 Project.这里可以把 Project 理解为 Eclipse 中的 workspace.Mod ...
- react 为元素添加自定义事件监听器
https://zhenyong.github.io/react/tips/dom-event-listeners.html class MovieItem extends React.Compone ...
- 第四章 消息摘要算法--SHA
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 4.1.SHA 原理:消息摘要长度(可以定量为加密后的字符串的长度)越长,安全性越高 ...
- Python 逗号的几种作用
转自http://blog.csdn.net/liuzx32/article/details/7831247 最近研究Python 遇到个逗号的问题 一直没弄明白 今天总算搞清楚了 1.逗号在参数传 ...
- Ubuntu 14.04 删除软件附加依赖
参考:FlowVisor的学习笔记 eg.mininet $ sudo apt-get remove mininet $ sudo apt-get remove --auto-remove minin ...
- 【Coursera】Technology :Fifth Week(1)
Store and Forward Networking 在长途连接中,路由的数量 取决于 最优路径(花费最少).也就是说被地理条件所限制. 所以 Store and Forward Networki ...
- navicat Window . MAC版常用快捷键
navicat 结合快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shi ...
- shell 无限循环输出时间
#!/bin/bash while(true) do date >> /home/k/a.log sleep done 查看 tail -f /home/k/a.log
- angular惰性加载拓展剖析
最近把一个比较旧的业余项目重新升级了下,将主文件进行了剥离,增加了些惰性加载的配置,将过程中一些零散的知识点做个总结,同时尽量深入原理实现层面. 项目环境: 前端框架:angular2.0.0-bet ...
- vuejs2点滴
在Vue定义一个不被添加getter setter 的属性: https://github.com/vuejs/vue/issues/1988 博客: 0.vux的x-input源码分析. http: ...