zoj 1610 Count the Colors

线段树的经典题。。。我拿离散化过掉了。。。哇咔咔。。。数据不够黑啊。这个算是O(N^2)的。

之前写过矩形并,我就想到这题了,今天又想起来了,写了试试,线段的并比矩形并还简单哈。

2574678 2011-07-10 21:24:28 Accepted 1610 C++ 170 656 小媛在努力@SOWHAT

等回校学线段树再来A一次~真不知道这个算啥类型。。还归到计算几何好了。


#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

const int MAX = 2*10010;
struct LL{ int x,y,c;};
LL b[MAX];
int a[MAX];
int len[MAX];
int col[MAX];
void solve(int n,int cnt)
{
	for(int i=0; i<n; i++)
	{
		int x = lower_bound(a,a+cnt,b[i].x) - a;
		int y = lower_bound(a,a+cnt,b[i].y) - a;
		for(int k=x; k<y; k++)
			col[k] = b[i].c;
	}
	for(int i=0; i<MAX; i++)
		if( col[i+1] != col[i] && col[i] != -1 )
			len[col[i]]++;
}

int main()
{
	int n,cnt;
	
	while( ~scanf("%d",&n) )
	{
		memset(len,0,sizeof(len));
		memset(col,-1,sizeof(col));
		cnt = 0;
		for(int i=0; i<n; i++)
		{
			scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].c);
			a[cnt++] = b[i].x; a[cnt++] = b[i].y;
		}
		sort(a,a+cnt);
		solve(n,cnt);
		for(int i=0; i<MAX; i++)
			if( len[i] )
				printf("%d %d\n",i,len[i]);
		printf("\n");
	}

return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值