HDU1556:Color the ball(线段树区间更新)
来源:互联网 发布:电子商务就业前景知乎 编辑:程序博客网 时间:2024/05/14 03:02
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,输入结束。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
31 12 23 331 11 21 30
Sample Output
1 1 13 2 1
最裸的一道区间更新线段树了,直接套用模板即可
#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;}
- 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【树状数组】【区间更新】
- Eclipse的代码提示功能
- EL在JSP的应用。
- POJ 2533 Longest Ordered Subsequence
- 版本控制发展及git和github的使用
- Introdution to 3D Game Programming With DirectX11 第4章 习题解答
- HDU1556:Color the ball(线段树区间更新)
- SharedPreferences应用间读取,修改后读取问题的解决
- WS_CLIPCHILDREN和WS_CLIPSIBLINGS的理解
- Qt编程之信号和槽机制
- C++ STL — 第6章 STL容器(一)vector
- 二分法
- eclipse 下没有android SDK and AVD Manager选项
- 生产者消费者程序
- CSS3 选择器