HDU 1556树状数组求解
2014-04-07 14:44
295 查看
F - Color the ball
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1556
Description
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
题意:中文的 ,就不多说了
现在我们模拟气球的状态
当a= 3,b=7时:
1 2 3 4 5 6 7 8 9 N
O O O O O O O O O .............O
1 0 0 0 0 -1 (分别是data[3]到data[8]的值)第i个珠子被染色的次数是sum[i]即i的前缀和,故只对a,b-1操作即可
现给出AC代码
#include<cstdio>
#include<string.h>
using namespace std;
int N,a,b,M;
const int MAX=100000+10;
int data[MAX];
int s[MAX];
int lowbit(int i)
{
return i&(-i);
}
void modify(int x,int delta)
{
for(int i = x; i <= M; i+=lowbit(i))
{
data[i] += delta;
}
}
int sum(int x)
{
int ans = 0;
for(int i = x; i > 0; i-=lowbit(i))
{
ans +=data[i];
}
return ans;
}
int main()
{
while(scanf("%d",&N)&&N)
{
M=N;
memset(data,0,sizeof(data));
while(N--)
{
scanf("%d%d",&a,&b);
modify(a,1);
modify(b+1,-1);
}
for(int i = 1; i < M; i++)
{
printf("%d ",sum(i));
}
printf("%d\n",sum(M));
}
return 0;
}
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1556
Description
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
题意:中文的 ,就不多说了
现在我们模拟气球的状态
当a= 3,b=7时:
1 2 3 4 5 6 7 8 9 N
O O O O O O O O O .............O
1 0 0 0 0 -1 (分别是data[3]到data[8]的值)第i个珠子被染色的次数是sum[i]即i的前缀和,故只对a,b-1操作即可
现给出AC代码
#include<cstdio>
#include<string.h>
using namespace std;
int N,a,b,M;
const int MAX=100000+10;
int data[MAX];
int s[MAX];
int lowbit(int i)
{
return i&(-i);
}
void modify(int x,int delta)
{
for(int i = x; i <= M; i+=lowbit(i))
{
data[i] += delta;
}
}
int sum(int x)
{
int ans = 0;
for(int i = x; i > 0; i-=lowbit(i))
{
ans +=data[i];
}
return ans;
}
int main()
{
while(scanf("%d",&N)&&N)
{
M=N;
memset(data,0,sizeof(data));
while(N--)
{
scanf("%d%d",&a,&b);
modify(a,1);
modify(b+1,-1);
}
for(int i = 1; i < M; i++)
{
printf("%d ",sum(i));
}
printf("%d\n",sum(M));
}
return 0;
}
相关文章推荐
- HDU 1166 敌兵布阵 典型的树状数组求解
- hdu 1556(树状数组)
- HDU 1556 Color the ball【线段树及lazy思想】【树状数组】
- hdu 1556 Color the ball(树状数组)
- hdu 1556 Color the ball 【树状数组:区间修改、单点求值】
- HDU--1556 -- Color the ball [树状数组] [线段树]
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
- HDU - 1556 Color the ball(树状数组 or 线段树)
- 树状数组-HDU-1556-Color the ball
- HDU 1556 Color the ball【树状数组】
- HDU 1556 Color the the ball(树状数组)
- hdu-1556-Color the ball(树状数组)
- Hdu--1556--Color the ball(树状数组最简单运用)
- hdu 1556 树状数组 成段更新 单点求值
- HDU 1556(树状数组 区域更新,单点求值)
- HDU 1556 Color the ball【树状数组||前缀和】
- hdu 1556 树状数组
- [HDU - 1556] Color the ball(树状数组)
- hdu 1556 Color the ball(树状数组)
- hdu-1556 Color the ball(树状数组)