HDU 1556 Color the ball (线段树)
来源:互联网 发布:javascript 清空元素 编辑:程序博客网 时间:2024/05/16 07:50
/**********************************************
题目大意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?原题
题目解析:
错误分析:
***********************************************/
#include <stdio.h>#include <iostream>using namespace std;struct SegTree{ int left,right,cover;//left表示区间的左端点,right表示区间的右端点,cover表示当前区间覆盖次数}tree[300005];//线段树结点void creat(int t,int s,int e)//此时t为1,从根结点开始创建//创建线段树 { int temp=t<<1;//temp是一种标记,标记树结点的下标; if(s==e) //s=e表示此时为叶子结点,结束; return ; tree[t].cover=0;// tree[t].left=s; tree[t].right=e; if(s+1==e) return ; creat(temp,s,(s+e)>>1);//递归创建左子树 creat(temp+1,(s+e)>>1,e);//递归创建右子树}void insert(int t,int s,int e)//此时t为1,因为每次插入或更新区间结点,都从根结点开始查询区间是否一致;{ int mid=(tree[t].left+tree[t].right)>>1;//>>表示二进制数左移,相当与/2; int temp=t<<1; if(tree[t].left==s&&tree[t].right==e)//要插入的区间和当前结点的区间相同,即查找到了 tree[t].cover++; else if(e<=mid)//若区间终点小于中间值,则在左子树遍历查询 insert(temp,s,e); else if(s>=mid)//若区间开始点大于中间值,则在右子树遍历查询 insert(temp+1,s,e); else//若中间值在区间内,则用二分法递归插入 insert(temp+1,mid,e),insert(temp,s,mid);}int count(int t,int s,int e)//t的作用同上;s-e表示要求和的区间 { int mid=(tree[t].left+tree[t].right)>>1; int temp=t<<1; if(tree[t].left==s&&tree[t].right==e) //若要求区间等于当前结点区间,直接返回当前值 return tree[t].cover; int sum=0; if(s>=tree[t].left&&e<=tree[t].right); //此结点在所求结点内 { sum+=tree[t].cover; if(e<=mid) sum+=count(temp,s,e); else if(s>=mid) sum+=count(temp+1,s,e); } return sum;}int main(){ int n,a,b,i; while(scanf("%d",&n)!=EOF&&n) { creat(1,1,n+1);//表示第i线段的为[i,i+1),所以为n+1; for(i=0;i<n;i++) { scanf("%d%d",&a,&b); insert(1,a,b+1); } for(i=1;i<n;i++) printf("%d ",count(1,i,i+1)); printf("%d\n",count(1,i,i+1)); } return 0;}
- 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 线段树
- HDU 1556 Color the ball 线段树
- HDU 1556 Color the ball [线段树]
- HDU 1556 Color the ball (线段树)
- hdu - 1556 (Color the ball 线段树)
- hdu 1566 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(线段树)
- HDU--1556 -- Color the ball [树状数组] [线段树]
- HDU -1556 Color the ball -线段树-成段更新
- HDU--杭电--1556--Color the ball--线段树
- 打开方式无法添加程序的解决办法
- DB2的七种武器
- 12个有趣的C语言问答
- 记录下, sql server下面语句的一些简单用法
- xcode4中使用第三方静态库
- HDU 1556 Color the ball (线段树)
- 设计模式之九:迭代器模式(Iterator Pattern)
- missing file警告
- jvm原理
- K-均值算法
- 内核模块程序结构
- [转]Python 常用包收集
- 【差分约束系统】Candies POJ3159
- linux下新建一个用户