首页 > 代码库 > HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】

HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8272    Accepted Submission(s): 4239


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

Sample Input
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
 

Sample Output
1 1 1 3 2 1
 

Author
8600

普通方法毫无意外的超时:

109510712014-07-06 16:14:52Time Limit Exceeded15563000MS584K400 BG++长木

#include <stdio.h>
#include <string.h>
#define maxn 100000 + 2

int arr[maxn];

int main()
{
	int n, a, b;
	while(scanf("%d", &n), n){
		memset(arr, 0, sizeof(arr));
		
		for(int i = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			while(a <= b) ++arr[a++];			
		}
		
		for(int i = 1; i <= n; ++i)
			if(i == n) printf("%d\n", arr[i]);
			else printf("%d ", arr[i]);
	}
	return 0;
}

线段树:

110202362014-07-13 21:31:02Accepted1556984MS1772K1045 BG++

#include <stdio.h>
#include <string.h>
#define maxn 100002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

int tree[maxn << 2], n;

void pushDown(int rt)
{
	tree[rt << 1] += tree[rt];
	tree[rt << 1 | 1] += tree[rt];
	tree[rt] = 0;
}

void update(int left, int right, int l, int r, int rt)
{
	if(left == l && right == r){
		++tree[rt]; return;
	}
	
	int mid = (l + r) >> 1;
	if(right <= mid) update(left, right, lson);
	else if(left > mid) update(left, right, rson);
	else{
		update(left, mid, lson);
		update(mid + 1, right, rson);
	}
}

void query(int l, int r, int rt)
{
	if(l == r){
		if(l != n) printf("%d ", tree[rt]);
		else printf("%d\n", tree[rt]);
		return;
	}
	
	if(tree[rt]) pushDown(rt);
	
	int mid = (l + r) >> 1;
	query(lson);
	query(rson);
}

int main()
{
	int a, b, i;
	while(scanf("%d", &n), n){
		memset(tree, 0, sizeof(tree));
		
		for(i = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			update(a, b, 1, n, 1);
		}
		
		query(1, n, 1);
	}
	return 0;
}



树状数组:(模式二:区间修改,单点查询,方向与模式一正好相反)

110256672014-07-14 16:04:19Accepted1556703MS596K677 BG++

#include <stdio.h>
#include <string.h>
#define maxn 100002

int tree[maxn], n;

int lowBit(int x){ return x & (-x); }

void update(int pos, int val)
{
	while(pos > 0){
		tree[pos] += val;
		pos -= lowBit(pos);
	}
}

int getSum(int pos)
{
	int sum = 0;
	while(pos <= n){
		sum += tree[pos];
		pos += lowBit(pos);
	}
	return sum;
}

int main()
{
	int a, b, i;
	while(scanf("%d", &n), n){
		memset(tree, 0, sizeof(tree));
		for(i = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			update(a - 1, -1);
			update(b, 1);
		}
		
		for(i = 1; i <= n; ++i)
			if(i != n) printf("%d ", getSum(i));
			else printf("%d\n", getSum(i));
	}
	return 0;
}


另外在网上看到了一个巧妙的方法:这个方法主要是对自增自减区间进行标记,这样能减少大量遍历自增的时间。

109512652014-07-06 16:35:21Accepted1556593MS596K430 BG++长木

#include <stdio.h>
#include <string.h>
#define maxn 100000 + 2

int arr[maxn];

int main()
{
	int n, a, b;
	while(scanf("%d", &n), n){
		memset(arr, 0, sizeof(arr));
		
		for(int i = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			++arr[a]; --arr[b + 1];
		}
		
		for(int i = 1, ans = 0; i <= n; ++i){
			ans += arr[i];
			
			if(i != n) printf("%d ", ans);
			else printf("%d\n", ans);
		}		
	}
	return 0;
}