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]之间的气球 ...
随机推荐
- BZOJ 3709 [PA2014]Bohater:贪心【反过来考虑】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3709 题意: 在一款电脑游戏中,你需要打败n只怪物(从1到n编号). 为了打败第i只怪物, ...
- linux应用之apache的源码安装(centos)
第一部分:前期准备 需要下载的东西 下载 Apache 源码包 下载地址: http://httpd.apache.org/download.cgi ...
- hdu 6103(Kirinriki)
题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...
- Spring MVC 中/和/*的区别
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...
- python 之闭包,装饰器,生成器
一.可迭代的对象(也就是可以用for循环,generator 是生成器的意思) 怎么判断是否可迭代呢?通俗的方法建立一个for循环 高大上的方法是: In [40]: from collections ...
- @Autowired的作用,自动装配,省去写get/set方法
这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="userDao" clas ...
- 【转】Pro Android学习笔记(三):了解Android资源(上)
在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...
- Modbus通讯协议学习 - 认识篇
转自:http://www.cnblogs.com/luomingui/archive/2013/06/14/Modbus.html 什么是Modbus? Modbus 协议是应用于电子控制器上的一种 ...
- python3中,pycharm中怎么连接数据库
因为python3现在还不能直接连接数据库,所有如果想连接,就只能通过以下方法: 在APP中的,__init__.py中,添加以下代码就可以: import pymysql pymysql.insta ...
- Hibernate 的HQL和sql有什么区别
转自:https://blog.csdn.net/haozhugogo/article/details/54575802sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 ...