HDU 1556 Color the ball 线段树入门题
来源:互联网 发布:曹云金 郭德纲 知乎 编辑:程序博客网 时间:2024/05/16 14:31
典型的线段树,区间的更新,统计区间内元素的和。
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7031 Accepted Submission(s): 3668
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 <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000#define LL __int64//#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 1000100;using namespace std;struct node{ int a, b; int cnum;}f[maxn*4];int num[maxn];void Bulid(int l, int r, int site){ if(l == r) { f[site].a = l; f[site].b = l; f[site].cnum = 0; return; } int mid = (l+r)>>1; Bulid(l, mid, site<<1); Bulid(mid+1, r, site<<1|1); f[site].a = l; f[site].b = r; f[site].cnum = 0;}void Search(int l, int r, int site){ if(f[site].a == l && f[site].b == r) { f[site].cnum++; return; } else { int mid = (f[site].a+f[site].b)>>1; if(r <= mid) { Search(l, r, site<<1); } else if(l > mid) { Search(l, r, site<<1|1); } else { Search(l, mid, site<<1); Search(mid+1, r, site<<1|1); } }}void Find(int x){ int i; for(i = f[x].a; i<=f[x].b; i++)//该区间所有编号都被刷了一次 num[i]+=f[x].cnum; if(f[x].a == f[x].b) return; Find(x<<1); Find(x<<1|1);}int main(){ int n; int a, b; while(scanf("%d",&n) && n != 0) { Bulid(1, n, 1); for(int i = 1; i <= n; i++) { scanf("%d %d",&a, &b); Search(a, b, 1); } memset(num, 0 , sizeof(num)); Find(1); for(int i = 1; i <= n-1; i++) printf("%d ",num[i]); printf("%d\n",num[n]); } return 0;}
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 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 [树状数组] [线段树]
- vps 登录加二次验证(authy)
- ubuntu13.04下载 编译android 4.0 (一)
- Linux必学60个命令
- 为什么鼠标光标向左倾斜,而不是垂直的?
- Hadoop中MapReduce的原理
- HDU 1556 Color the ball 线段树入门题
- Extjs 中id与itemId的区别
- python maketrans and translate
- Android-设置Button不可点击
- PLSQL中文乱码问题
- Elevator
- grep ^VERSION /usr/bin/repo
- python中maketrans和translate的用法
- java内存泄露例子