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 (线段树和差分数组两种解法)的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  3. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. hdu 1556 Color the ball 线段树

    题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...

  6. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  7. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  8. hdu 1556 Color the ball (树状数组)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. hdu 1556 Color the ball(树状数组)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...

随机推荐

  1. python读写txt大文件

    直接上代码: import easygui import os path = easygui.fileopenbox()#path是打开的文件的全路径 if path:#如果选择打开文件,没有选择取消 ...

  2. CI框架上传csv文件

    今天遇到在用CI框架上传csv文件时报错问题: The filetype you are attempting to upload is not allowed. 是类型不允许,想到CI框架的conf ...

  3. canvas练习单个矩形形变

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. html5--1.20 课程小结与若干点补充

    html5--1.20 课程小结与若干点补充 学习要点: 1.第一章HTML5基础知识做一个小结2.对本章课程中部分内容做几点补充 课程小结 对本章的知识点做一个简单的回顾,并对其中个别知识点做若干补 ...

  5. 用python 实现录入学生作业情况的小程序

    写一个录入学生作业情况的一个程序 1.查看学生作业情况 2.录入学生作业情况 3.可以让输入3次,需要为空的情况 homeworks = { ‘张流量’: {‘2018.3.22’:”未交”,’201 ...

  6. Smack编程库进行代码功能调试

    http://www.tuicool.com/articles/U3Afiy 使用Smack编程库进行代码功能调试 关于Smack编程库,前面我们提到,它是面向Java端的api,主要在PC上使用,利 ...

  7. listen 77

    Your Dog Wants YOUR Food Does your puppy turn his nose up at his own chow—because he wants some of w ...

  8. linux 进程学习笔记-等待子进程结束

    <!--[if !supportLists]-->Ÿ <!--[endif]-->等待子进程结束 pid_t waitpid(pid_t pid, int *stat_loc, ...

  9. myeclipse 2017破解安装教程+开发环境部署(jdk+tomcat)

    点击安装包,进入安装界面,点击next 选择接受协议,点击next 选择安装目录,点击next 格局自己电脑的机型选择32bit或64bit,点击next 安装完成后不要运行MyEclipse,将 & ...

  10. 【转】Pro Android学习笔记(六):了解Content Provider(中)

    Content Provider的架构 Authority类似web中的域名,每个content provider会通过AndroidManifest.xml向系统注册authority,如下.其中n ...