HDU 1556 Color the ball(线段树)
来源:互联网 发布:linux vsftpd pid 太多 编辑:程序博客网 时间:2024/05/18 02:22
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12851 Accepted Submission(s): 6426
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<iostream>#include<set>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;struct node{ int l, r, sum, mark;} s[100010<<2];void pushup(int cnt){ s[cnt].sum = s[cnt<<1].sum + s[cnt<<1|1].sum;}void pushdown(int cnt, int len){ if(s[cnt].mark) { s[cnt<<1].mark += s[cnt].mark; s[cnt<<1|1].mark += s[cnt].mark; s[cnt<<1].sum += s[cnt].mark * (len - (len>>1)); s[cnt<<1|1].sum += s[cnt].mark * (len>>1); s[cnt].mark = 0; }}void build_tree(int l, int r, int cnt){ s[cnt].l = l; s[cnt].r = r; s[cnt].sum = 0; s[cnt].mark = 0; if(l==r) return ; int mid = (l + r)>>1; build_tree(l, mid, cnt<<1); build_tree(mid+1, r, cnt<<1|1); pushup(cnt);}void update(int l, int r, int cnt){ if(s[cnt].l >= l && s[cnt].r <= r) { s[cnt].sum += (s[cnt].r - s[cnt].l + 1); s[cnt].mark++; return ; } pushdown(cnt, s[cnt].r - s[cnt].l + 1); int mid = (s[cnt].r + s[cnt].l) >> 1; if(l<=mid) update(l, r, cnt<<1); if(r>mid) update(l, r, cnt<<1|1); pushup(cnt);}int query(int l, int r, int cnt){ if(s[cnt].l >= l && s[cnt].r <= r) { return s[cnt].sum; } pushdown(cnt, s[cnt].r - s[cnt].l + 1); int ret = 0, mid = (s[cnt].r + s[cnt].l) >> 1; if(l <= mid) ret += query(l, r, cnt<<1); if(r > mid) ret += query(l, r, cnt<<1|1); return ret;}int main(){ int n, a, b; while(cin>>n && n) { int temp = n; build_tree(1, n, 1); while(temp--) { scanf("%d%d", &a, &b); update(a, b, 1); } for(int i = 1; i<=n; i++) { printf("%d", query(i, i, 1)); if(i!=n) printf(" "); } printf("\n"); } return 0;}树状数组:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cmath>using namespace std;int cnt[100010], n;int lowbit(int x){ return x & (-x);}void add(int x, int num){ while(x<=n) { cnt[x]+=num; x+=lowbit(x); }}int sum(int x){ int res = 0; while(x>=1) { res += cnt[x]; x-=lowbit(x); } return res;}int main(){ int temp; while(cin>>n && n) { temp = n; memset(cnt, 0, sizeof(cnt)); while(temp--) { int s, e; scanf("%d%d", &s, &e); add(s, 1); add(e+1, -1); } // for(int i = 1; i<=n; i++) // printf("%d ", cnt[i]); // printf("\n"); printf("%d", sum(1)); for(int i = 2; i<=n; i++) printf(" %d", sum(i)); printf("\n"); }}
0 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 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(线段树)
- spring quartz定时
- tomcat6.0 安装版和解压版的内存设置
- Java中getBytes()的用法
- 【Rochchip 定制】添加自动重启功能
- Python post中文参数传递出错的一种解决办法
- HDU 1556 Color the ball(线段树)
- Ceil函数
- bootstrap 垂直导航 PS:一个很实用的~~布局吧~~词穷
- tar 解压缩命令详解
- LPTSTR、LPCSTR、LPCTSTR、LPSTR的来源及意义
- 通过代码自定义cell(cell的高度不一致)
- 弹窗之背景模糊效果
- 《C++编程思想》 第十四章 多态和虚函数 (原书代码+习题+讲解)
- 使用重写的$.ajax()统一错误处理