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类初探

    class human: is_alive=True is_man=True def __init__(self,age): print('this is __init__() method, whi ...

  2. CSS3实现3D木块旋转动画

    CSS3实现3D木块旋转动画,css3特效,旋转动画,3D,立体效果,CSS3实现3D木块旋转动画是一款迷人的HTML5+CSS3实现的3D旋转动画. 代码下载:http://www.huiyi8.c ...

  3. BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1680 题意: 在接下来的n周内,第i周生产一吨酸奶的成本为c[i],订单为y[i]吨酸奶. ...

  4. Call to unavailable function 'system': not available on iOS

    使用Xcode 9 导入cocos2d-x 项目,报错 Call to unavailable function 'system': not available on iOS 原因很简单,就是ios ...

  5. AtCoder Grand Contest 002 F:Leftmost Ball

    题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...

  6. WPF TextBox PreviewTextInput handle IME (chinese)

    今天调试自己写的WPF的Behavior, 是关于TextBox只能输入数据或者小数点的. 发现有个问题, 就是英文IME下字母等等都能过滤, 但是一旦切换到中文输入法, 就会发现在OnPreview ...

  7. POJ3660(foyld闭包问题)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8794   Accepted: 4948 Descr ...

  8. 【win10激活问题】 从【win10专业工作站版】转为 数字许可证的【win10专业版】

    今天安装了 win10 1903 (10.0.18362 暂缺 Build 18362) 安装时 选的 是[win10 专业工作站版] 却无法激活, (因为当初是从win7升级上win10的,只有关联 ...

  9. fastIO模板

    freadIO整理 namespace fastIO{ #define BUF_SIZE 100000 ; inline char nc() { static char buf[BUF_SIZE],* ...

  10. CF-839B

    B. Game of the Rows time limit per test 1 second memory limit per test 256 megabytes input standard ...