HDU-1556-Color the ball (线段树和差分数组两种解法)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
题解:
线段树版:
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 50005
using namespace std;
int lazy[1000005],N;
struct node
{
int l,r,sum;
}tr[1000005];
void build(int m,int l,int r)
{
tr[m].l=l;
tr[m].r=r;
tr[m].sum=0;
if(l!=r)
{
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
}
}
void insert(int m,int l,int r)
{
if(tr[m].l==l&&tr[m].r==r)
{
tr[m].sum++;
}
else
{
int mid=(tr[m].l+tr[m].r)>>1;
if(r<=mid)
insert(m<<1,l,r);
else if(l>mid)
{
insert(m<<1|1,l,r);
}
else
{
insert(m<<1,l,mid);
insert(m<<1|1,mid+1,r);
}
}
}
void add(int x)
{
for(int i=tr[x].l;i<=tr[x].r;i++)
{
lazy[i]+=tr[x].sum;
}
if(tr[x].l==tr[x].r)
return;
add(x<<1);
add(x<<1|1);
}
int main()
{
while(~scanf("%d",&N),N)
{
build(1,1,N);
for(int t=1;t<=N;t++)
{
int a,b;
scanf("%d%d",&a,&b);
insert(1,a,b);
}
memset(lazy,0,sizeof(lazy));
add(1);
printf("%d",lazy[1]);
for(int t=2;t<=N;t++)
{
printf(" %d",lazy[t]);
}
cout<<endl;
}
return 0;
}
差分数组
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int f[100005]={0};
int a[100005]={0};
int n;
while(cin>>n)
{
if(n==0)
break;
memset(f,0,sizeof(f));
memset(a,0,sizeof(a));
int l,r;
for(int t=1;t<=n;t++)
{
scanf("%d%d",&l,&r);
f[l]++;
f[r+1]--;
}
for(int t=1;t<=n;t++)
{
a[t]=a[t-1]+f[t];
}
for(int t=1;t<n;t++)
{
printf("%d ",a[t]);
}
printf("%d\n",a[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 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]之间的气球 ...
随机推荐
- 带动画效果的jQuery手风琴
带动画效果的jQuery特效手风琴是一款带动画效果的手风琴作品,非常实用,可以用在新闻列表.FAQ等模块,默认的是打开第一个选项,查看其它的时候直接点击加号按钮就展开. 源码地址:http://www ...
- ios图文混编瀑布流
ios图文混编瀑布流,利用UICollectionView 实现图文混编的瀑布流,支持section内容伸缩 http://www.huiyi8.com/pubuliu/
- redis实现分布式锁——核心 setx+pipe watch监控key变化-事务
如何设计一把分布式锁 我们用 redis 来实现这把分布式的锁,redis 速度快.支持事务.可持久化的特点非常适合创建分布式锁. 分布式环境中如何消除网络延迟对锁获取的影响 锁,简单来说就是存于 r ...
- CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...
- ACM学习历程—HDU 1276 士兵队列训练问题(队列)
Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠 拢,再从头开始进行一至三报数,凡 ...
- configured to save RDB snapshots, but is currently not able to persist o...
Redis问题 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d ...
- hdu 4609 3-idiots —— FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 算不合法的比较方便: 枚举最大的边,每种情况算了2次,而全排列算了6次,所以还要乘3: 注意枚举最大 ...
- 《TCP/IP详解卷一:协议》数据链路层(一)
版权声明:本文为博主原创文章,转载请标注转载链接,谢谢. 目录(?)[+] 引言 在TCP/IP协议族中,链路层主要有三个目的: 为IP模块发送和接收IP数据报. 为ARP模块 ...
- wamp + wordpress 安装
WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过程中直接下一步 ...
- raid0和raid5的 实验过程
raid:独立的磁盘冗余阵列 创建raid0: 环境准备:准备三块大小相同的磁盘或分区,此处要特别注意:红色字体 [root@localhost6 home]#fdisk /dev/sdd ##对/d ...