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 ...
随机推荐
- Large Division(大数)题解
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- How can I list all foreign keys referencing a given table in SQL Server?
How can I list all foreign keys referencing a given table in SQL Server? how to check if columns in ...
- Adobe Reader 2019 Offline Installer, Free Download - Best PDF Reader
https://ridnt-b.blogspot.com/2018/01/adobe-reader-2018-free-download.html http://ardownload.adobe.co ...
- hdu 2586 How far away ? 倍增求LCA
倍增求LCA LCA函数返回(u,v)两点的最近公共祖先 #include <bits/stdc++.h> using namespace std; *; struct node { in ...
- C语言通过枚举网卡,API接口可查看man 7 netdevice--获取接口IP地址
/*代码来自StackOverflow: http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-c ...
- HTTP错误 404.17 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
Errore HTTP 404.2 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理 出现这种情况的原因通常是因为先安装了Framewo ...
- Ubuntu下配置JDK
1. 首先你需要到oracle官网下载最新版本的JDK.跑到oracle官网,自己到Download下面找找吧 2.转到下载路径,对下载后的文件解压缩,比如我下载的文件名为jdk-7u7-linux- ...
- 常规css,js引入
php // css,js用 $this->assign('MODULE_NAME',MODULE_NAME); $this->assign('ACTION_NAME',ACTION_NA ...
- 使用 dplyr 管道操作处理数据框
关于数据操作的另一个流行的包是dplyr,它发明了一种数据操作语法.dplyr 扩展包并没有使用构建子集函数([ ]),而是定义了一系列基础的变形函数作为数据操作模块,并且引入了一个管道操作符,利用管 ...
- Image():强制让图片缓存起来
Image 对象 Image 对象代表嵌入的图像. <img> 标签每出现一次,一个 Image 对象就会被创建. 创建一个Image对象:var a=new Image(); 定义 ...