Color the ball 此题这个用的是线段树
来源:互联网 发布:淘宝 手机 描述 模板 编辑:程序博客网 时间:2024/06/17 02:11
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 Input31 12 23 331 11 21 30Sample Output1 1 13 2 1
#include<iostream>#include<stdio.h>using namespace std;bool flag;struct node{ int l,r,mark;}tree[100001*4];void build(int l,int r,int i){ tree[i].l=l; tree[i].r=r; tree[i].mark=0; if(l==r) { return; } int mid=(l+r)/2; build(l,mid,i*2); build(mid+1,r,i*2+1); return;}void down_mark(const int& i){ tree[i*2].mark+=tree[i].mark; tree[i*2+1].mark+=tree[i].mark; tree[i].mark=0;}void update(int l,int r,int i){ if(tree[i].l==l&&tree[i].r==r) { tree[i].mark++; return; } if(tree[i].mark) { down_mark(i); } int mid=(tree[i].l+tree[i].r)/2; if(mid>=r) { update(l,r,i*2); } else if(mid<l) { update(l,r,i*2+1); } else { update(l,mid,i*2); update(mid+1,r,i*2+1); }}void query(int l,int r,int i){ if(l==r) { if(flag==0) printf("%d",tree[i].mark); else printf(" %d",tree[i].mark); flag=1; return; } if(tree[i].mark) down_mark(i); int mid=(l+r)/2; query(l,mid,i*2); query(mid+1,r,i*2+1);}int n,a,b; int main (){ while(~scanf("%d",&n),n) { flag=0; build(1,n,1); for(int i=0;i<n;i++) { scanf("%d%d",&a,&b); update(a,b,1); } query(1,n,1); printf("\n"); }}
阅读全文
1 0
- Color the ball 此题这个用的是线段树
- Color the ball (线段树)
- Color the ball(线段树)
- Color the ball(线段树)
- hdu 1556 Color the ball(线段树的另一种应用)
- HDU 1556 Color the ball 线段树入门题
- HDU 1556 Color the ball 线段树模板题
- Color the ball (线段树,区间更新模板题)
- hdu 1566 Color the ball---线段树
- HDU 1556 Color the ball 线段树
- HDU1556 Color the ball(线段树)
- HDU 1199 Color the Ball (线段树)
- HDU 1556 Color the ball (线段树)
- HDU 1556 Color the ball 线段树
- hdu 1556 Color the ball(线段树)
- hdu 1556 Color the ball 线段树
- hdoj 1556 Color the ball 【线段树】
- HDU 1556 Color the ball 线段树
- C程序设计语言(四)结构
- request,response传入线程值会变为null
- 欢迎使用CSDN-markdown编辑器
- 你必须知道的261个Java语言问题笔记- Java编程基础2
- SpringMVC接受页面参数和传参到jsp的几种方法
- Color the ball 此题这个用的是线段树
- listview展示网络新闻类条目包括图片
- 分析一个通用的rtsp server实现过程发送模块
- 【python】pickle模块(泡菜)
- form表单中select组件使用for循环赋值
- javascript 精度问题
- 多个$(document).ready()的执行顺序实例分析
- 字符串的粘贴以及裁剪(即sprintf和sscanf)
- USACO-Section2.1 Sorting a Three-Valued Sequence