HDU1556:Color the ball(线段树区间更新)
来源:互联网 发布:海尔电视电视直播软件 编辑:程序博客网 时间:2024/05/24 04:24
题目描述:
Problem 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
思路分析:
线段树~
ac代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int ans[1000000],n;struct node{ int l,r,n;} a[1000000];void init(int l,int r,int i){ a[i].l = l; a[i].r = r; a[i].n = 0; if(l!=r) { int mid = (l+r)>>1; init(l,mid,2*i); init(mid+1,r,2*i+1); }}void insert(int i,int x,int y){ if(a[i].l == x && a[i].r == y)//找到要刷的气球区间,更新其被刷的次数+1 a[i].n++; else { int mid = (a[i].l+a[i].r)>>1; if(y<=mid) insert(2*i,x,y); else if(x>mid) insert(2*i+1,x,y); else { insert(2*i,x,mid); insert(2*i+1,mid+1,y); } }}void add(int x){ int i; for(i = a[x].l; i<=a[x].r; i++)//该区间所有编号都被刷了一次 ans[i]+=a[x].n; if(a[x].l == a[x].r) return; add(2*x); add(2*x+1);}int main(){ int x,y,i; while(~scanf("%d",&n),n) { init(1,n,1); for(i = 1; i<=n; i++) { scanf("%d%d",&x,&y); insert(1,x,y); } memset(ans,0,sizeof(ans)); add(1); printf("%d",ans[1]); for(i = 2; i<=n; i++) printf(" %d",ans[i]); printf("\n"); } return 0;}
0 0
- HDU1556:Color the ball(线段树区间更新)
- hdu1556 Color the ball(线段树区间更新)
- hdu1556 Color the ball(线段树区间更新)
- HDU1556 Color the ball(线段树区间更新)
- HDU1556:Color the ball(线段树区间更新)
- hdu1556 Color the ball 线段树区间染色问题
- hdu1556 Color the ball(线段树区间修改)
- Color the ball(hdu1556)(hash)或(线段树,区间更新)
- hdu1556-Color the ball (线段树区间更新)(延迟标记)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- 线段树区间更新 Color the ball
- HDU1556 Color the ball(线段树)
- hdu1556-color the ball 线段树
- hdu1556 Color the ball 线段树
- hdu1556 Color the ball 线段树
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- HDU1556 Color the ball【树状数组】【区间更新】
- Altium Desinger 的基本使用(2)
- 9.2.3
- css 如何实现导航border-bottom 有阴影的感觉
- POJ 2773 Happy 2006 【gcd的性质(也可用欧拉函数)】
- Jenkins 升级到2.42 Junit test report 出现错误的解决
- HDU1556:Color the ball(线段树区间更新)
- java基础六对象
- 【清明】莫队算法
- 1051. Pop Sequence
- 圆的运动-原生JS详解
- 神经网络进阶(连载1)为机器人提供知觉
- CCF201312-1 出现次数最多的数(100分)
- 搭建mm网站
- hdl 简单设计举例