zoj 1610 Count the Colors 线段树

本地跑不起来交上去能过..

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;

const int N = 10000;

struct node{
	int l, r;
	int x;
}tr[N];

int col[N], ans[N];

void build( int l, int r, int rt )
{
	tr[rt].l = l;
	tr[rt].r = r;
	tr[rt].x = -1;
	if( l == r )
		return;
	int mid = ( l + r ) >> 1;
	build( lson );
	build( rson );
}

void update( int l, int r, int rt, int x )
{
	if( l <= tr[rt].l && tr[rt].r <= r )
	{
		tr[rt].x = x;
		return;
	}
	if( tr[rt].x != -1 )
	{
		tr[rt<<1].x = tr[rt<<1|1].x = tr[rt].x;
		tr[rt].x = -1;
	}
	int mid = ( tr[rt].l + tr[rt].r ) >> 1;
	if( r <= mid )
		update( l, r, rt << 1, x );
	else if( l > mid )
		update( l, r, rt << 1 | 1, x );
	else
	{
		update( lson, x );
		update( rson, x );
	}
}

void query( int l, int r, int rt )
{
	if( tr[rt].l == tr[rt].r )
	{
		col[l] = tr[rt].x;
		return;
	}
	int mid = ( l + r ) >> 1;
	if( tr[rt].x != -1 )
	{
		tr[rt<<1].x = tr[rt<<1|1].x = tr[rt].x;
		tr[rt].x = -1;
	}
	query( lson );
	query( rson );
}

int main()
{
	int l, r, c;
	int n;
	while(~scanf("%d", &n))
	{
		build( 1, 8001, 1 );
		for( int i = 1; i <= n; i++ )
		{
			scanf("%d%d%d", &l, &r, &c);
			if( l > r )
				swap( l, r );
			l++;
			update( l, r, 1, c );
		}
		
		memset( ans, 0, sizeof( ans ) );
		memset( col, 0, sizeof( col ) );
		query( 1, 8001, 1 );
		col[0] = -1;
		for( int i = 1; i <= 8001; i++ )
		{
			if( col[i] != -1 && col[i] != col[i-1] )
				ans[ col[i] ]++;
		}
		for( int i = 0; i <= 8001; i++ )
		{
			if( ans[i] > 0 )
				printf("%d %d\n", i, ans[i]);
		}
		puts("");
		
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值