HDU 1566 Color the ball
来源:互联网 发布:matplotlib python 编辑:程序博客网 时间:2024/06/03 18:52
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18042 Accepted Submission(s): 8998Problem DescriptionN个气球排成一排,从左到右依次编号为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 1Author8600SourceHDU 2006-12 Programming Contest
解法①
非常巧妙的解法…
a[i]表示 以i作为起点 对i-n的气球全部上色的次数
那对(start,end) ++a[start]
--a[end+1]
抵消掉end+1 - n 的部分
那问题就转化为求a的前缀和
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int N = 100000;int a[N];int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); int n,s,e; while(scanf("%d",&n),n){ fill(a,a+n+1,0); for(int i=0;i<n;++i){ scanf("%d%d",&s,&e); ++a[s],--a[e+1]; } printf("%d",a[1]); for(int i=2;i<=n;++i){ a[i]+=a[i-1]; printf(" %d",a[i]); } putchar('\n'); } return 0;}
解法②
线段树区间更新
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int N = 100000+5;struct{ int l,r,n;}rmq[4*N];void init_rmq(int l,int r,int k){ rmq[k]={l,r,0}; if(l!=r){ int mid = (l + r)/2; init_rmq(l,mid,2*k); init_rmq(mid + 1,r,2*k + 1); }}void update(int s,int e,int k,int x){ if(s==rmq[k].l&&e==rmq[k].r){ rmq[k].n+=x; return; } int mid = (rmq[k].l + rmq[k].r)/2; if(s<=mid){//左子数[l,mid] update(s,min(e,mid),2*k,x); } if(mid+1<=e){//右子树[mid+1,r] update(max(s,mid+1),e,2*k+1,x); }}int ans[N];void caulate_ans(int k){ if(rmq[k].n>0){ for(int i=rmq[k].l;i<=rmq[k].r;++i){ ans[i]+=rmq[k].n; } } if(rmq[k].l<rmq[k].r){ caulate_ans(2*k); caulate_ans(2*k+1); }}int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); int n,s,e; while(scanf("%d",&n),n){ init_rmq(1,n,1); for(int i=0;i<n;++i){ scanf("%d%d",&s,&e); update(s,e,1,1); } fill(ans,ans+n+1,0); caulate_ans(1); printf("%d",ans[1]); for(int i=2;i<=n;++i){ printf(" %d",ans[i]); } putchar('\n'); } return 0;}
0 0
- HDU 1566 Color the ball
- HDU 1566 Color the ball
- HDU 1566 Color the ball
- Color the ball HDU
- Color the ball HDU
- Color the Ball HDU
- hdu 1566 Color the ball---线段树
- hdu 1556 color the ball
- hdu 1556Color the ball
- HDU 1556 - Color the ball
- hdu 1199 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
- JAVA 简单的计算器
- android基本架构
- 基于windows的Ubuntu双系统安装
- 微信小程序传值
- Linux Apache2.2配置CGI
- HDU 1566 Color the ball
- 数据库范式
- sql之left join、right join、inner join的区别
- 创建快捷方式 (并添加到快速启动栏 、桌面、开始菜单)
- nc65用友uap开发节点通过按钮调用打开另外一个节点并传数据跳编辑态(推单)
- Bmob+融云,node.js获取token
- Intent详解
- iphone分辨率中的scale参数
- intellij idea 导入maven编译好的spark源码