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 ...
随机推荐
- JavaScript 时间格式
方法1: Date.prototype.Format = function (fmt) { var o = { , //月份 "d+": this.getDate(), //日 = ...
- 动态规划模板2|LCS最长公共子序列
LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...
- POJ 1740 A New Stone Game(博弈)题解
题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...
- 试着用React写项目-利用react-router解决跳转路由等问题(三)
转载请注明出处:王亟亟的大牛之路 本来想一下子把路由的接下来的内容都写完的,但是今天白天开了会,传了些代码打了个包,就被耽搁了 这一篇来讲一下 IndexLink和 onlyActiveOnIndex ...
- IDEA,RubyMine等JetBrains系列软件通用破解教程
此教程不光适用于IDEA,还可以在RubyMine等JetBrains系列软件使用,亲测可用. (1)下载安装你需要的JetBrains系列软件,安装完最好不要打开,直接finish,断开网络. (2 ...
- Luogu P1533 可怜的狗狗
题目链接:https://www.luogu.org/problemnew/show/P1533 没人写$fhq\ treap$做法,那我就补一篇qwq 看到这题第一时间想主席树,然后发现我还没学主席 ...
- BZOJ5189: [Usaco2018 Jan]Cow at Large 贪心+LCA
BZOJ没有题面QAQ,题目链接 洛谷有:题目链接 这题首先要读懂题..(洛谷的翻译有点迷 就是指定根节点,然后可以在叶子结点放个人,然后奶牛在根,问最少要在叶子结点放多少人才能让奶牛走不到叶子结点( ...
- 关于ES6的箭头函数的详解
ok 坑比函数~~箭头函数~~不自己动手写看懂也不行~~~ 当然你也可以一点一点的把函数复制到Babel里面去将ES6转换成ES5 (斗笔行为) 老谢写的笔记教程就是深入(通俗易懂)哈哈~~~ 第 ...
- python 递增递减数列
def is_arithmetic(l): delta = l[] - l[] ): ] - l[index] == delta): return False return True print(is ...
- 大数据学习:storm流式计算
Storm是一个分布式的.高容错的实时计算系统.Storm适用的场景: 1.Storm可以用来用来处理源源不断的消息,并将处理之后的结果保存到持久化介质中. 2.由于Storm的处理组件都是分布式的, ...