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]之间的气球 ...
随机推荐
- [干货]兼容HTML5的Placeholder属性-更新版v0.10102013
HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等.Placeholder是HTML5新增的另一个属性,当input或者textarea设置了 ...
- hashlib加密
一.hashlib的基本组成: 1.hashlib库是python3的标准库,主要用于数据的加密,以下是hashlib的方法及属性. >>> import hashlib>&g ...
- CF 438 E & bzoj 3625 小朋友和二叉树 —— 多项式开方
题目:http://codeforces.com/contest/438/problem/E https://www.lydsy.com/JudgeOnline/problem.php?id=3625 ...
- Idea无法加载主类
今天重装了下电脑,运行idea发现各种问题. 直接进主题哈, 遇到三种情况 第一种: 首先查看这里是否有多个,只保留当前需要用工程路径.点击P右边的删除即可 删除后 然后运行是否能运行. 如果没有的话 ...
- DAL 层引用 System.Net.Http ,引发的一阵心慌
快下班的时候 代码data 数据层编译失败,引起整个解决方案全部失败:其他同事虽然vs 版本不同,但是都能编译通过:考虑到今天更改过vs 的设置,把今天更改的设置全部都恢复,结果还是不行.最后直接恢复 ...
- ICU 是一种说不出的痛啊
USE [Nursing] GO /****** Object: StoredProcedure [dbo].[P_GetICUVitualSign] Script Date: 05/21/2015 ...
- Spring:JdbcTemplate使用指南
Spring:JdbcTemplate使用指南 Spring:JdbcTemplate使用指南 前言: 本文指在介绍Spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转 ...
- 《Java多线程编程核心技术》读后感(六)
多线程的死锁 package Second; public class DealThread implements Runnable { public String username; public ...
- CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 使用Axis2方式发布webService的三种方式
1.Axis2的下载和安装 首先可以下载如下两个zip包:axis2-1.6.1-bin.zipaxis2-1.6.1-war.zip其中 axis2-1.6.1-bin.zip文件中包含了Axis2 ...