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个气球总共被涂色的次数。

#include<stdio.h>
#pragma comment(linker,"/STACk:1024000000,1024000000")
struct CNode
{int L,R;//int nSum;int Inc;CNode *pLeft,*pRight;
};
CNode Tree[1000050];
int nCount;
int Mid(CNode *pRoot)
{return (pRoot->L+pRoot->R)/2;
}
void BuildTree(CNode *pRoot,int L,int R)
{pRoot->L=L;pRoot->R=R;//pRoot->nSum=0;pRoot->Inc=0;if(L==R)return ;nCount++;pRoot->pLeft=Tree+nCount;nCount++;pRoot->pRight=Tree+nCount;BuildTree(pRoot->pLeft,L,(L+R)/2);BuildTree(pRoot->pRight,(L+R)/2+1,R);
}
/*void Insert(CNode *pRoot,int i,int v)
{if(pRoot->L==i&&pRoot->R==i){pRoot->nSum=v;return ;}pRoot->nSum+=v;if(i<=Mid(pRoot))Insert(pRoot->L,i,v);elseInsert(pRoot->R,i,v);
}*/
void Add(CNode *pRoot,int a,int b)
{if(pRoot->L==a&&pRoot->R==b){pRoot->Inc++;return ;}//pRoot->nSum+=c*(b-a+1);if(b<=Mid(pRoot))Add(pRoot->pLeft,a,b);else if(a>=Mid(pRoot)+1)Add(pRoot->pRight,a,b);else{Add(pRoot->pLeft,a,Mid(pRoot));Add(pRoot->pRight,Mid(pRoot)+1,b);}
}
int ans;
int QuerySum(CNode *pRoot,int a,int b,int i)
{ans+=pRoot->Inc;if(pRoot->L==i&&pRoot->R==i)return ans;//pRoot->nSum+(pRoot->R-pRoot->L+1)*pRoot->//pRoot->nSum+=(pRoot->R-pRoot->L)*pRoot->Inc;//Add(pRoot->pLeft,pRoot->L,Mid(pRoot),pRoot->Inc);//Add(pRoot->pRight,Mid(pRoot)+1,pRoot->R,pRoot->Inc);//pRoot->Inc=0;if(i<=Mid(pRoot))return QuerySum(pRoot->pLeft,a,b,i);else //(i>=Mid(pRoot)+1)return QuerySum(pRoot->pRight,a,b,i);/*else{return QuerySum(pRoot->pLeft,a,Mid(pRoot),i)+QuerySum(pRoot->pRight,Mid(pRoot)+1,b,i);}*/
}
int main()
{int n,a,b,i;while(scanf("%d",&n)!=EOF&&n){nCount=0;BuildTree(Tree,1,n);for(i=0;i<n;i++){scanf("%d%d",&a,&b);Add(Tree,a,b);}for(i=1;i<n;i++){ans=0;printf("%d ",QuerySum(Tree,1,n,i));}ans=0;printf("%d\n",QuerySum(Tree,1,n,n));}return 0;
}

转载于:https://www.cnblogs.com/XDJjy/p/3283287.html

hdu 1556 Color the ball (线段树做法)相关推荐

  1. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  2. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  3. HDU - 1556 Color the ball(树状数组)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  4. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  6. hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. 解题报告:hdu 1556 Color the ball(区间修改,单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...

  9. HDU 1556 Color the ball

    题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...

  10. HDU 1556 Color the ball - from lanshui_Yang

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...

最新文章

  1. qpsk调制解调matlab_本科毕业设计课题—QPSK相干解调的MATLAB仿真(3)
  2. iphone4s解锁_苹果手机忘记密码怎么办?iPhone忘记密码解锁恢复方法
  3. Django配置开发环境和生产环境以及配置Jinja2模板引擎
  4. 推荐一款基于web的Unix系统管理工具webmin
  5. poi获取有效单元格个数_EXCEL利用SUM函数和FREQUENCY函数数组公式统计不同区间数据个数...
  6. RxSwift PriorityQueue 优先级队列的实现
  7. pandas处理mysql 展现wpf_Pandas DataFrame使用多列聚合函数
  8. 通信原理 | 信道特性及其数学模型
  9. c语言 最大子段和,最大子段和 C语言源码
  10. 反向题在测试问卷信效度_检验问卷的信度和效度
  11. ArcGIS勘测定界三调版发布
  12. AI口罩“督查官”诞生记
  13. RobotStudio 项目移植问题——无法创建系统
  14. 推荐几个适合上班摸鱼的神操作!
  15. 云服务器如何共享文件夹,云服务器如何设置共享文件夹
  16. 高分一号数据的预处理
  17. Win10安装程序报错2503和2502错误解决方案
  18. netdev_features_t和ip_summed说明
  19. 谷歌查看html地址_104篇CVPR 2019论文,追踪谷歌、Facebook、英伟达研究课题
  20. USACO 2018 January Contest Platinum A: Lifeguards 题解

热门文章

  1. matlab符号运算实验,数学实验6--Matlab符号运算.pptx
  2. Flutter学习 — 从新页面返回数据给上一个页面
  3. L1-004 计算摄氏温度 (5 分)—团体程序设计天梯赛
  4. AT24C02跨页写数据
  5. 设计模式(三) 工厂模式
  6. 事件绑定,事件捕获,事件冒泡以及事件委托,兼容IE
  7. ArrayList排序Sort()方法(转)
  8. Jquery查找界面Html元素的方法(持续更新)
  9. Windows 8实用窍门系列:1.使用Xaml+C#开发第一个Metro Style应用程序
  10. H3C DHCP中继实验